I am trying to make a GUI program using tkinter and python3. When I try to create a submenu for a menu in the menubar, I get a 3D arrow unlike the arrow I usually get in other programs.
from tkinter import *
MainWindow = Tk()
MainWindow.title('MenuBar')
MainWindow.geometry(str(MainWindow.winfo_screenwidth()) + 'x' + str(MainWindow.winfo_screenheight()))
#Menu_Bar object
menubar = Menu(MainWindow, bd=0, activeborderwidth=0)
#File Menu Object
file_menu = Menu(menubar, tearoff = 0, bd=0, activeborderwidth=0)
file_menu.add_command(label =' New', command = None)
file_menu.add_separator()
file_menu.add_command(label =' Open', command = None)
#Create a sub-menu for Open-Recent
Open_Recent = Menu(file_menu, tearoff=0, bd=0,activeborderwidth=0)
Open_Recent.add_command(label = 'file1', command=None)
Open_Recent.add_command(label = 'file2', command=None)
Open_Recent.add_command(label = 'file3', command=None)
file_menu.add_cascade(label =' Open Recent', command = None, menu=Open_Recent)
file_menu.add_separator()
file_menu.add_command(label =' Save', command = None)
file_menu.add_command(label =' Save as', command = None)
file_menu.add_separator()
file_menu.add_command(label =' Import FIle as Module', command = None)
file_menu.add_separator()
file_menu.add_command(label =' Exit', command = MainWindow.destroy)
menubar.add_cascade(label ='Menu', menu = file_menu)
#Edit Menu Object
edit_menu = Menu(menubar, tearoff = 0, bd=0, activeborderwidth=0)
edit_menu.add_command(label =' Undo', command = None)
edit_menu.add_command(label =' Redo', command = None)
edit_menu.add_separator()
edit_menu.add_command(label =' UI', command = None)
edit_menu.add_command(label =' System', command = None)
edit_menu.add_separator()
edit_menu.add_command(label =' Rename', command = None)
edit_menu.add_command(label =' Batch Rename', command = None)
menubar.add_cascade(label ='Edit', menu = edit_menu)
#Assets Menu Object
Asset_menu = Menu(menubar, tearoff = 0, bd=0, activeborderwidth=0)
Asset_menu.add_command(label =' Set Folder', command = None)
Asset_menu.add_command(label =' Export as Asset', command = None)
Asset_menu.add_command(label =' Import Asset', command = None)
Asset_menu.add_command(label =' Browse Assets', command = None)
Asset_menu.add_command(label =' Set Icon', command = None)
Asset_menu.add_command(label =' Remove Icon', command = None)
menubar.add_cascade(label =' Assets', menu = Asset_menu)
#Adding Menubar to the window
MainWindow.config(menu=menubar)
mainloop()
Here is a screenshow from VSCode(the look I am going for)
Image1
And here is what I am getting
Image2
I just added bg = "black", fg = "white",activebackground="white" to each menu. I'm not sure if it's the right answer, but the result looks the exactly like image 1.
EDIT:
full code with the small changes.
from tkinter import *
MainWindow = Tk()
MainWindow.title('MenuBar')
MainWindow.geometry(str(MainWindow.winfo_screenwidth()) + 'x' + str(MainWindow.winfo_screenheight()))
#Menu_Bar object
menubar = Menu(MainWindow, bd=0, activeborderwidth=0, bg = "black", fg = "white",activebackground="white")
#File Menu Object
file_menu = Menu(menubar, tearoff = 0, bd=0, activeborderwidth=0, bg = "black", fg = "white",activebackground="white")
file_menu.add_command(label =' New', command = None)
file_menu.add_separator()
file_menu.add_command(label =' Open', command = None)
#Create a sub-menu for Open-Recent
Open_Recent = Menu(file_menu, tearoff=0, bd=0,activeborderwidth=0, bg = "black", fg = "white",activebackground="white")
Open_Recent.add_command(label = 'file1', command=None)
Open_Recent.add_command(label = 'file2', command=None)
Open_Recent.add_command(label = 'file3', command=None)
file_menu.add_cascade(label =' Open Recent', command = None, menu=Open_Recent)
file_menu.add_separator()
file_menu.add_command(label =' Save', command = None)
file_menu.add_command(label =' Save as', command = None)
file_menu.add_separator()
file_menu.add_command(label =' Import FIle as Module', command = None)
file_menu.add_separator()
file_menu.add_command(label =' Exit', command = MainWindow.destroy)
menubar.add_cascade(label ='Menu', menu = file_menu)
#Edit Menu Object
edit_menu = Menu(menubar, tearoff = 0, bd=0, activeborderwidth=0,bg = "black", fg = "white",activebackground="white")
edit_menu.add_command(label =' Undo', command = None)
edit_menu.add_command(label =' Redo', command = None)
edit_menu.add_separator()
edit_menu.add_command(label =' UI', command = None)
edit_menu.add_command(label =' System', command = None)
edit_menu.add_separator()
edit_menu.add_command(label =' Rename', command = None)
edit_menu.add_command(label =' Batch Rename', command = None)
menubar.add_cascade(label ='Edit', menu = edit_menu)
#Assets Menu Object
Asset_menu = Menu(menubar, tearoff = 0, bd=0, activeborderwidth=0,bg = "black", fg = "white",activebackground="white")
Asset_menu.add_command(label =' Set Folder', command = None)
Asset_menu.add_command(label =' Export as Asset', command = None)
Asset_menu.add_command(label =' Import Asset', command = None)
Asset_menu.add_command(label =' Browse Assets', command = None)
Asset_menu.add_command(label =' Set Icon', command = None)
Asset_menu.add_command(label =' Remove Icon', command = None)
menubar.add_cascade(label =' Assets', menu = Asset_menu)
#Adding Menubar to the window
MainWindow.config(menu=menubar)
mainloop()
Related
i know that i can create menu bar with add_cascade
like this code:
from tkinter import *
from tkinter.ttk import *
from time import strftime
# creating tkinter window
root = Tk()
root.title('Menu Demonstration')
# Creating Menubar
menubar = Menu(root)
# Adding File Menu and commands
file = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='File', menu = file)
file.add_command(label ='New File', command = None)
file.add_command(label ='Open...', command = None)
file.add_command(label ='Save', command = None)
file.add_separator()
file.add_command(label ='Exit', command = root.destroy)
# Adding Edit Menu and commands
edit = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Edit', menu = edit)
edit.add_command(label ='Cut', command = None)
edit.add_command(label ='Copy', command = None)
edit.add_command(label ='Paste', command = None)
edit.add_command(label ='Select All', command = None)
edit.add_separator()
edit.add_command(label ='Find...', command = None)
edit.add_command(label ='Find again', command = None)
# Adding Help Menu
help_ = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Help', menu = help_)
help_.add_command(label ='Tk Help', command = None)
help_.add_command(label ='Demo', command = None)
help_.add_separator()
help_.add_command(label ='About Tk', command = None)
# display Menu
root.config(menu = menubar)
mainloop()
But how can i remove them?
You can use delete to delete a menubar
menubar.delete ( 'name' )
My first post, so I apologize as I learn to use this. Also, I am sure my code is very jumbled and rudimentary, but I am not here to get someone to re-write my code. I have a question about how a widget is behaving and an error code I am getting.
I currently have two frames in a mainWindow, and when you click a button labeled "New", a small window (newWindow) pops up that has a registration form, and a button labeled "Register" to write the form data to a .txt file.
The problem I am having is that if I do not define where the button should go, it appears in the mainWindow. If I specify for it to appear in the newWindow, it does not appear and throws me the following error:
File
"/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/init.py",
line 1507, in nametowidget
w = w.children[n] KeyError: '!button2'
Here is my code (get ready for a mess!):
from tkinter import *
from tkinter import ttk
from time import strftime
from tkmacosx import SFrame, Button
import tkinter as tk
mainWindow = Tk()
mainWindow.title('BATCO Main')
def createWindow():
#When createWindow() is called, the following code is executed, starting with creating the window object itself, which is set to the Tk() widget caller
newWindow = Tk()
# Add title of window
newWindow.title('Patron Entry')
# Set from and geometry of the Button (widthxheight+XPOS+YPOS)
newWindow.geometry('500x350+100+200')
# THIS PART CREATES AND APPENDS THE FILE FOR STORING NAMES:
def saveInfo():
# Defines Variables(3) for the "get" Method that will take from the StringVar Variables(2) above:
fname_info = fname.get()
mname_info = mname.get()
lname_info = lname.get()
suffix_info = suffix.get()
addr1_info = addr1.get()
addr2_info = addr2.get()
city_info = city.get()
state_info = state.get()
zipcode_info = zipcode.get()
print(fname_info, mname_info, lname_info, suffix_info, addr1_info, addr2_info, city_info, state_info, zipcode_info)
# This starts by opening the file 'patrons.txt' and assigns it to the file Variable. It uses the Arg 'a' which stands for Append, so it will add to the file.
file = open('patrons.txt', 'a')
# Once the file is open, write to it using the write method on the Variable file. The Write method accepts one Arg, so I have to use the + Operator to Concatenate text.
file.write (fname_info + ',')
file.write (mname_info + ',')
file.write (lname_info + ',')
file.write (suffix_info + ',')
file.write (addr1_info + ',')
file.write (addr2_info + ',')
file.write (city_info + ',')
file.write (state_info + ',')
file.write (zipcode_info + ';\n')
file.close ()
print (fname_info, 'has been registered')
# Clears the fields in the form
fname_entry.delete(0, END)
mname_entry.delete(0, END)
lname_entry.delete(0, END)
suffix_entry.delete(0, END)
addr1_entry.delete(0, END)
addr2_entry.delete(0, END)
city_entry.delete(0, END)
state_entry.delete(0, END)
zipcode_entry.delete(0, END)
# This code closes the window after entry
newWindow.destroy()
# End of saveInfo function
# Creating basic text that is formatted to be a header at the top of the window
lbl = Label(newWindow, text = 'Create Record', bg = 'light gray', fg = 'gray', width = '50', font = ('Arial', 17))
lbl.place(x=0, y=0)
# Defining Variables(1) for the text box labels:
fname_text = Label(newWindow, text = 'First Name', fg = 'gray', font = ('Arial', 12))
mname_text = Label(newWindow, text = 'Middle name', fg = 'gray', font = ('Arial', 12))
lname_text = Label(newWindow, text = 'Last Name', fg = 'gray', font = ('Arial', 12))
suffix_text = Label(newWindow, text = 'Suffix', fg = 'gray', font = ('Arial', 12))
addr1_text = Label(newWindow, text = 'Address 1', fg = 'gray', font = ('Arial', 12))
addr2_text = Label(newWindow, text = 'Address 2', fg = 'gray', font = ('Arial', 12))
city_text = Label(newWindow, text = 'City', fg = 'gray', font = ('Arial', 12))
state_text = Label(newWindow, text = 'State', fg = 'gray', font = ('Arial', 12))
zip_text = Label(newWindow, text = 'Zip', fg = 'gray', font = ('Arial', 12))
# Placing text box labels:
fname_text.place(x = 15, y = 50)
mname_text.place(x = 155, y = 50)
lname_text.place(x = 295, y = 50)
suffix_text.place(x = 435, y = 50)
addr1_text.place(x = 15, y = 100)
addr2_text.place(x = 15, y = 150)
city_text.place(x = 15, y = 200)
state_text.place(x = 318, y = 200)
zip_text.place(x = 367, y = 200)
# Defining and formatting Variables(2) for the data entered INTO the Entry boxes:
fname = StringVar()
mname = StringVar()
lname = StringVar()
suffix = StringVar()
addr1 = StringVar()
addr2 = StringVar()
city = StringVar()
state = StringVar()
zipcode = StringVar()
# Defining the Variables(3) for the Entry boxes:
fname_entry = Entry(newWindow, textvariable = fname, width = 14)
mname_entry = Entry(newWindow, textvariable = mname, width = 14)
lname_entry = Entry(newWindow, textvariable = lname, width = 14)
suffix_entry = Entry(newWindow, textvariable = suffix, width = 5)
addr1_entry = Entry(newWindow, textvariable = addr1, width = 51)
addr2_entry = Entry(newWindow, textvariable = addr2, width = 51)
city_entry = Entry(newWindow, textvariable = city, width = 32)
state_entry = Entry(newWindow, textvariable = state, width = 3)
zipcode_entry = Entry(newWindow, textvariable = zipcode, width = 13)
`enter code here`# Placing the Entry boxes:
fname_entry.place(x=15, y = 69)
mname_entry.place(x=155, y = 69)
lname_entry.place(x=295, y = 69)
suffix_entry.place(x=435, y = 69)
addr1_entry.place(x=15, y = 119)
addr2_entry.place(x=15, y = 169)
city_entry.place(x=15, y = 219)
state_entry.place(x=318, y = 219)
zipcode_entry.place(x=360, y = 219)
# Creates a Button and assigns it to the Variable: register. It is a gray, borderless button that when clicked, calls the saveInfo function.
register = Button(text = "REGISTER", bordercolor = 'gray', padx = 10, pady = 5, background='#BDBDBD', borderless = 'true', relief = 'groove', command = saveInfo)
# Places the Button
register.place (in_=newWindow, x = 200, y = 275)
newWindow.mainloop()
# End of createWindow function
style1 = ttk.Style()
style2 = ttk.Style()
style3 = ttk.Style()
style1.configure('topFrame.TFrame', background='green', borderwidth=4, relief='raised')
style2.configure('bottomFrame.TFrame', background = 'blue', borderwidth=6, relief = 'raised')
style3.configure('SrchLabel.TLabel')
eFrame = ttk.Frame(mainWindow, width=1060, height=200, style='topFrame.TFrame').grid()
rFrame = ttk.Frame(mainWindow, width=1060, height=600, style='bottomFrame.TFrame').grid()
# eFrame: Title text shown before the search terms entry box
srchFrame = ttk.Label(mainWindow, style = 'SrchLabel.TLabel', text = 'Patron Search')
srchFrame.place (x='30', y='40')
# eFrame: Search terms entry box
srchVar = StringVar()
srchEntry = tk.Entry(eFrame, bd = '1', width = '35')
srchEntry.place (x='130', y='37')
# eFrame: Button to execute Search
srchButton = Button(eFrame, text = 'Search', height = 30, width = 60, bd = '1')
srchButton.place(x = '470', y = '35')
# eFrame: Button to create New Record
newButton = Button(eFrame, text = 'New', height = 30, width = 60, bd = '1', command = createWindow)
newButton.place(x = '550', y = '35')
# rFrame: TAB Window Frame = from ttk Notebook
tabControl = ttk.Notebook(rFrame, width = 995, height = 528, style = 'rTabs.TNotebook')
# rFrame: TAB Elements
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
tab3 = ttk.Frame(tabControl)
tab1=Frame(tabControl, background="#ffffff")
tab1.pack()
tab2=Frame(tabControl, background="#ffffff")
tab2.pack()
tab3=Frame(tabControl, background="#ffffff")
tab3.pack()
# rFrame: TAB Titles and Window placement
tabControl.add(tab1, text ='Patron')
tabControl.add(tab2, text ='Orders')
tabControl.add(tab3, text = 'Donations')
tabControl.place(x='5', y='205')
# TAB 1 Content
ttk.Label(tab1, text ='Patron Data').grid(column = 0, row = 0, padx = 30, pady = 30)
# TAB 2 Content
ttk.Label(tab2, text ='Order History and Info').grid(column = 0, row = 0, padx = 30, pady = 30)
# TAB 3 Content
ttk.Label(tab3, text ='Donation Info').grid(column = 0, row = 0, padx = 30, pady = 30)
# mainWindow: MENUBAR
menubar = Menu(mainWindow)
# mainWindow: Filemenu
file = Menu(menubar, tearoff = 1)
menubar.add_cascade(label ='File', menu = file)
file.add_command(label ='New File', command = None)
file.add_command(label ='Open...', command = None)
file.add_command(label ='Save', command = None)
file.add_separator()
file.add_command(label ='Exit', command = mainWindow.destroy)
# mainWindow: Editmenu
edit = Menu(menubar, tearoff = 1)
menubar.add_cascade(label ='Edit', menu = edit)
edit.add_command(label ='Cut', command = None)
edit.add_command(label ='Copy', command = None)
edit.add_command(label ='Paste', command = None)
edit.add_command(label ='Select All', command = None)
edit.add_separator()
edit.add_command(label ='Find...', command = None)
edit.add_command(label ='Find again', command = None)
# mainWindow: Helpmenu
help_ = Menu(menubar, tearoff = 1)
menubar.add_cascade(label ='Help', menu = help_)
help_.add_command(label ='Tk Help', command = None)
help_.add_command(label ='Demo', command = None)
help_.add_separator()
help_.add_command(label ='About Tk', command = None)
mainWindow.config(menu = menubar)
mainWindow.mainloop()
Again, I am not looking for advice on how to write better code, as I am just learning (2 weeks in). However any help you can provide in letting me know what I am doing wrong and where I can find an answer would be greatly appreciated!
Thanks in advance!
So I have this PDF printer code (the code runs from console perfectly) and I wanted to make it executable .exe.
I tried with Python 3.8.2, cx_freeze, by issuing the cxfreeze-quickstart.exe command. Filling basic data, and selecting (G) as GUI option. All went well and got the program pdfprinter.exe, but when I try to run it on Windows 10, i get error message "no module named win32".
Can someone help how to make an executable from this code?
Error message picture
import win32api
import win32print
import traceback
from tkinter.filedialog import askopenfilename
from tkinter import *
from tkinter import font # * doesn't import font or messagebox
from tkinter import messagebox
root = Tk()
root.title("Python Printer")
root.geometry("410x310")
root.resizable(False, False)
root.tk.call('encoding', 'system', 'utf-8')
def font_size(fs):
return font.Font(family='Helvetica', size=fs, weight='bold')
# Add a grid
mainframe = Frame(root)
#mainframe.grid(column=0,row=0, sticky=(N,W,E,S) )
mainframe.grid(column=0,row=0, sticky=(N) )
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)
mainframe.pack(pady = 10, padx = 0)
# Create a _printer variable
_printer = StringVar(root)
# Create a _color variable
_color = StringVar(root)
_filename = ""
# on change dropdown value
def sel_printer(*args):
print( _printer.get() )
# link function to change dropdown
_printer.trace('w', sel_printer)
def sel_color(*args):
print( _color.get() )
# link function to change dropdown
_color.trace('w', sel_color)
def UploadAction(event=None):
global _filename
_filename = filedialog.askopenfilename()
#print('Selected:', _filename)
def PrintAction(event=None):
PRINTER_DEFAULTS = {"DesiredAccess":win32print.PRINTER_ALL_ACCESS}
pHandle = win32print.OpenPrinter(_printer.get(), PRINTER_DEFAULTS)
properties = win32print.GetPrinter(pHandle, 2)
properties['pDevMode'].Color = 1 if str(_color.get()) == "Color" else 2
properties['pDevMode'].Copies = 1
win32print.SetPrinter(pHandle, 2, properties, 0)
if not _filename:
messagebox.showerror("Error", "No File Selected")
return
elif not _printer.get():
messagebox.showerror("Error", "No Printer Selected")
return
try:
#win32print.SetDefaultPrinter(_printer.get())
win32api.ShellExecute(0, "print", _filename, None, ".", 0)
win32print.ClosePrinter(pHandle)
except:
pass
messagebox.showerror("Error", "There was an error printing the file :(")
choices = [printer[2] for printer in win32print.EnumPrinters(2)]
_printer.set(win32print.GetDefaultPrinter()) # set the default option
popupMenu = OptionMenu(mainframe, _printer, *choices)
popupMenu['font'] = font_size(12)
Label(mainframe, text="SELECT PRINTER").grid(row = 1, column = 1)
popupMenu.grid(row = 2, column =1)
# Dictionary with options
choices = ["COLOR", "MONOCHROME"]
_color.set("COLOR") # set the default option
popupMenu2 = OptionMenu(mainframe, _color, *choices)
popupMenu2['font'] = font_size(12)
Label(mainframe, text="COLOR MODE").grid(row = 3, column = 1)
popupMenu2.grid(row = 4, column =1)
Label(mainframe, text="SELECT FILE").grid(row = 5, column = 1)
button = Button(mainframe, text=u"\uD83D\uDCC1" ' BROWSE', command=UploadAction)
button['font'] = font_size(12)
button.grid(row = 6, column =1)
_copies = IntVar()
_copies.set(1)
def copies_increase(event=None):
_copies.set(_copies.get() + 1)
def copies_decrease(event=None):
_copies.set(_copies.get() - 1)
if _copies.get() < 1 :
_copies.set(1)
Label(mainframe, textvariable=_copies).grid(columnspan=2)
button_frame = Frame(mainframe)
button_frame.grid(columnspan=2)
dec_button = Button(button_frame, text=u"\u2212", command=copies_decrease, fg="dark green", bg = "white", height=1, width=3 )
dec_button['font'] = font_size(10)
inc_button = Button(button_frame, text=u"\uFF0B", command=copies_increase, fg="dark green", bg = "white", height=1, width=3 )
inc_button['font'] = font_size(10)
button_frame.columnconfigure(0, weight=1)
button_frame.columnconfigure(1, weight=1)
dec_button.grid(row=0, column=0, sticky=W+E)
inc_button.grid(row=0, column=1, sticky=W+E)
Label(mainframe).grid(row = 10, column = 1)
p_button = Button(mainframe, text=u'\uD83D\uDDB6' + " PRINT", command=PrintAction, fg="dark green", bg = "white")
p_button['font'] = font_size(18)
p_button.grid(row = 11, column =1)
root.mainloop()
I am new to this tkinter menu. I am trying to upload and display an excel file by using the 'filemenu' in the menubar and 'btnNew' in the toolbar menu.
The application did run but it does not display my excel file after I browse the file. The application only display the datatypes of each variable in the excel file.
import pandas as pd
import xlrd
import tkinter as tk
from tkinter import Frame, Menu, Button, Label, Canvas
from tkinter import LEFT, RIGHT, TOP, BOTTOM, X, FLAT, RAISED
from tkinter import filedialog
from tkinter import ttk
root = tk.Tk() #main method 1 - create main window (parent window)
root.title("Data Visualisation")
width = 1000
height = 500
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width / 2) - (width / 2)
y = (screen_height / 2) - (height / 2)
root.geometry('%dx%d+%d+%d' % (width, height, x, y))
root.resizable(1,1)
def browseFile():
global workbook, copyWorkbook, excel_file, sheetName, worksheet
fileName = filedialog.askopenfilename(initialdir = '/', title = 'New File', filetypes = (('excel file', '.xlsx'), ('excel file', '.xls'), ('all files', '*.*')))
excel_file = pd.ExcelFile(fileName)
workbook = xlrd.open_workbook(fileName)
sheetCount = workbook.nsheets
#Create tabs
sheetName = []
tab = []
for x in range(workbook.nsheets):
tab.append(ttk.Frame(tabControl))
sheetName = workbook.sheet_names()
tabControl.add(tab[x], text = sheetName[x])
df_table = excel_file.parse(sheetName[x])
print(df_table.dtypes)
lblTable = Label(tab[x], text = df_table.to_string(index = False)).grid()
btnGraph = Button(tab[x], text = "Graph").grid(sticky = 'w', column = 1, row = 5)
##MENU BAR
menubar = Menu(root)
root.config(menu = menubar)
#FILE MENU
filemenu = Menu(menubar, bg = '#BFBFBF', tearoff = 0)
menubar.add_cascade(label = 'File', menu = filemenu)
filemenu.add_command(label = 'New', compound = LEFT, command = browseFile)
filemenu.add_command(label = 'Open...', compound = LEFT)
filemenu.add_separator()
filemenu.add_command(label = 'Quit', compound = LEFT, command = root.quit)
#SEARCH MENU
searchmenu = Menu(menubar, bg = '#BFBFBF')
menubar.add_cascade(label = 'Search', menu = searchmenu)
searchmenu.add_command(label = 'Find...', compound = LEFT)
searchmenu.add_command(label = 'Replace...', compound = LEFT)
#HELP MENU
helpmenu = Menu(menubar, bg = '#BFBFBF')
menubar.add_cascade(label = 'Help', menu = helpmenu)
helpmenu.add_command(label = 'About', compound = LEFT)
##TOOLBAR MENU
toolbar = Frame(root, bd = 1, relief = RAISED)
#To browse excel file
btnNew = Button(toolbar, text = 'New', compound = TOP, relief = FLAT, activebackground = '#ADD8E6', padx = 20, pady = 2, command = browseFile).pack(side = LEFT)
btnOpen = Button(toolbar, text = 'Open', compound = TOP, relief = FLAT, activebackground = '#ADD8E6', padx = 10, pady = 2).pack(side = LEFT)
btnFind = Button(toolbar, text = 'Find', compound = TOP, relief = FLAT, activebackground = '#ADD8E6', padx = 10, pady = 2).pack(side = LEFT)
btnReplace = Button(toolbar, text = 'Replace', compound = TOP, relief = FLAT, activebackground = '#ADD8E6', padx = 10, pady = 2).pack(side = LEFT)
btnQuit = Button(toolbar, text = 'Quit', compound = TOP, relief = FLAT, activebackground = '#ADD8E6', padx = 10, pady = 2, command = root.quit).pack(side = RIGHT)
toolbar.pack(side = TOP, fill = X)
###Tab Widget
tabControl = ttk.Notebook(menubar)
tabHome = ttk.Frame(tabControl)
tabControl.add(tabHome, text = "Home")
#All the automation Tab is pack here
tabControl.pack(expand = 1, fill = 'both', side = LEFT)
root.mainloop() #main method 2 - run the application
The application should display tabs and in each tab, it should contain each sheet from the excel file. All the tabs must be display after the toolbar menu.
I'm not sure what is the problem as is does not specify if there is any error in my code.
All feedback is welcomed as I am trying to learn how to make a better interface using python. Thank you for your help :D
You set the wrong parent to the frame tabControl.
Change:
tabControl = ttk.Notebook(menubar)
to:
tabControl = ttk.Notebook(root)
I am having trouble figuring out why my Entry() is not showing up in my program window. I have looked around the internet and I can not figure out what I am doing wrong.
I am trying to place my Entry() field above my main text filed in the program but at this point I would settle for it showing up at all in the program.
The code works fine except for the EntryWidget I am tying to add in.
from tkinter import *
from tkinter.ttk import *
import subprocess as sub
import tkinter.messagebox
#use the doNothing function as a holder for anything that needs to call a function you have not made yet
def doNothing():
print("Do lots of nothing?")
#~~~~~~~~~~~< Message Box >~~~~~~~~~~~
def ihnb():
tkinter.messagebox.showinfo("This is an example!", "Icecream Has No Bones!")
answer = tkinter.messagebox.askquestion("This is not a real question", "Are you trying to become a programer?")
if answer == "yes":
a1 = "Then be prepared to spend countless hours hating life!"
root.text.insert(tkinter.END, a1)
root.text.see(tkinter.END)
else:
a2= "Smart move. Now go away!"
root.text.insert(tkinter.END, a2)
root.text.see(tkinter.END)
#create the window
root = Tk()
#modify root window
root.title("MINT: Mobile Information & Note-taking Tool")
root.geometry("800x600")
root.config(bg = 'Orange')
#~~~~~~~~~~~< Entry Widget >~~~~~~~~~~~
keywordEntry = Entry(root)
keywordEntry.grid(row=3, column=1)
keywordEntry.delete(0, END)
keywordEntry.insert(END, 'default text')
#~~~~~~~~~~~< Menu >~~~~~~~~~~~
menu = Menu(root)
root.config(menu=menu)
fileMenu = Menu(menu)
menu.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="Save", command=doNothing)
fileMenu.add_command(label="Save As", command=doNothing)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", command= doNothing)
helpMenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpMenu)
helpMenu.add_command(label="Info", command=doNothing)
#~~~~~~~~~~~< Toolbar >~~~~~~~~~~~
spacer10 = " "
toolbar = Frame(root)
somethingButt = tkinter.Button(toolbar, fg = 'Black', bg = 'Orange', text = "Do Python?", command = ihnb)
somethingButt.grid(row = 0, column = 0, padx = 1, pady = 1, sticky = W)
buttonSpacer0_1 = tkinter.Button(toolbar, fg = 'Black', bg = 'Black', text = spacer10)
buttonSpacer0_1.grid(row = 0, column = 1, padx = 1, pady = 1, sticky = W)
somethingButt2 = tkinter.Button(toolbar, fg = 'White', bg = 'Black', text = "Do something?", command = doNothing)
somethingButt2.grid(row = 0, column = 2, padx = 1, pady = 1, sticky = W)
buttonSpacer0_1 = tkinter.Button(toolbar, fg = 'Black', bg = 'Black', text = spacer10)
buttonSpacer0_1.grid(row = 0, column = 3, padx = 1, pady = 1, sticky = W)
somethingButt3 = tkinter.Button(toolbar, fg = 'White', bg = 'Black', text = "Do something?", command = doNothing)
somethingButt3.grid(row = 0, column = 4, padx = 1, pady = 1, sticky = W)
toolbar.grid(row = 0, sticky = W)
#~~~~~~~~~~~< Toolbar >~~~~~~~~~~~
root.text = Text (root, width = 100, height = 10, fg='White', bg='Black', wrap = WORD)
root.text.grid(row = 10, padx=5, pady=5)
#~~~~~~~~~~~< Status Bar >~~~~~~~~~~~
status = Label(root, text = "Preparing to do nothing...", relief = SUNKEN, anchor = W)
status.grid(padx=5, pady=5, sticky = W)
root.mainloop()
I am trying to place my Entry() field above my main text filed
I think this is what you are looking for:
If so, then simply change:
keywordEntry.grid(row=3, column=1)
To:
keywordEntry.grid(row=1, column=0, sticky=W)
Nota Bene: This just resolves your problem, but there are other things to improve in your program.