Python Forum

Full Version: Tkinter Gui Menu Addition Error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys, I am having an issue with my Python 3.71 Tkinter GUI class that I have been writing. I am relatively new to Python and the present code is an amalgam of online tutorials found at the top ten links found on google through "Python Tkinter GUI tutorial" google search.

I have a link to the Stackoverflow post for the code and history of the question, but I would very much appreciate any of your help, feedback, and critique.

https://stackoverflow.com/questions/5403...perational



from tkinter import *
from tkinter import ttk
import tkinter.scrolledtext as tkst
import os
import tkinter as tk
from functools import partial
from PIL import Image, ImageTk

class UserGui(tk.Tk):

	def __init__(self,parent):
		self.parent=parent
		self.widgets()

	def widgets(self):
		self.parent.configure(bg='white')
		self.frame1_style = ttk.Style()
		self.frame1_style.configure('My.TFrame', background='white')
		self.frame2_style = ttk.Style()
		self.frame2_style.configure('My2.TFrame',background='white')
		self.parent.title("TGUI")
	

		self.frame1 = ttk.Frame(self.parent, style='My.TFrame') #Creating Total Window Frame 1
		self.frame1.grid(row=0, column=0, sticky=(N, S, E, W))

		self.frame2 = ttk.Frame(self.parent, width=100, height=20, style='My2.TFrame')
		self.frame2.grid(row=0, column=6, padx=20, pady=5)

		self.newText = tkst.ScrolledText(master=self.frame2, wrap=tk.WORD, width=50, height=10)
		self.newText.grid(row=0, column=6)

		self.firstNameLabel = ttk.Label(self.frame1, text="First Name")
		self.firstName = ttk.Entry(self.frame1)
		self.lastNameLabel = ttk.Label(self.frame1, text="Last Name")
		self.lastName = ttk.Entry(self.frame1)

		self.optList1 = [
			"1",
			"2",
			"3"
			]
		self.optList2 = [
			"1",
			"2",
			"3", 
			"4"
			]
		self.var1 = StringVar(self.parent)
		self.var1.set(self.optList1[0]) #first value default

		self.var2 = StringVar(self.parent)
		self.var1.set(self.optList2[0]) #first value default

		self.optMenu1 = ttk.OptionMenu(self.frame1, self.var1, self.optList1[0], *self.optList1)
		self.optMenu2 = ttk.OptionMenu(self.frame1, self.var2, self.optList2[0], *self.optList2)

		self.rConfigNameLabel = ttk.Label(self.frame1, text="Sel1: ")
		self.rModeNameLabel = ttk.Label(self.frame1, text="Sel2: ")

		self.saveButton_style = ttk.Style()
		self.saveButton_style.configure('newButton.TButton', background='#404040')
		self.saveButton = ttk.Button(self.frame1, text="Save and Quit", command=self.parent.quit, style='newButton.TButton')
		self.showButton = ttk.Button(self.frame1, text="Show Entries", command=partial(self.showEntryFields), style='newButton.TButton')
		#------------------------------------------------------------------------------------
		#Grid Call Collection
		self.firstNameLabel.grid(row=0, column=0, sticky=W, padx=5)
		self.firstName.grid(row=0, column=1, sticky=(N, E, W), padx=5, pady=5)
		self.lastNameLabel.grid(row=1, column=0, sticky=W, padx=5)
		self.lastName.grid(row=1, column=1, sticky=(N, E, W), padx=5, pady=5)
		self.optMenu1.grid(row=2, column=1, padx=5,pady=5)
		self.optMenu2.grid(row=3, column=1, pady=5)
		self.rConfigNameLabel.grid(row=2, column=0, sticky=W, padx=5)
		self.rModeNameLabel.grid(row=3, column=0, sticky=W, padx=5)
		self.saveButton.grid(row=4, column=1, sticky=W, padx=5, pady=20)
		self.showButton.grid(row=4, column=0, sticky=W, padx=5, pady=20)
		#------------------------------------------------------------------------------------
		#Menu Creation
		self.menu1 = tk.Menu(self.parent, tearoff=0)
		self.parent.config(menu=self.menu1)
		self.fileMenu = tk.Menu(self.menu1, tearoff=0)
		self.fileMenu.add_command(label="Open", command=self.donothing)
		self.fileMenu.add_command(label="Save", command=self.donothing)

		self.fileMenu.add_separator()

		self.fileMenu.add_command(label="Exit", command=self.parent.quit)
		self.fileMenu.add_cascade(label="File", menu=self.menu1)

		self.editMenu = tk.Menu(self.menu1, tearoff=0)
		self.editMenu.add_command(label="Cut", command=self.donothing)
		self.editMenu.add_command(label="Copy", command=self.donothing)
		self.editMenu.add_command(label="Paste", command=self.donothing)
		self.editMenu.add_cascade(label="Edit", menu=self.menu1)	

	def showEntryFields(self):
		if __name__ == "__main__":
			print("First Name: %s\nLast Name: %s" % (self.firstName.get(), self.lastName.get()))
			print("Sel1: %s\nSel2: %s" % (self.var1.get(),self.var2.get()))

	def donothing(self):
		filewin = Toplevel(self.parent)
		button = Button(filewin, text="Do nothing button")
		button.pack()

			

def main():
	root=tk.Tk()
	ug=UserGui(root)
	root.mainloop()

if __name__ == '__main__':
	main()
Thank you for your time. Smile
what is the 'Menu Addition Error'? You mention but don't identify the issue.
Apologies, it is not an actual error reported by tcl or Python in a terminal. It was an error that was hidden throughout dependency issues. My code
self.editMenu.add_cascade(label="Edit",menu=self.menu1)
was adding
self.menu1
to
self.editMenu
, which is the opposite ordering of what I wanted to accomplish with the
add_cascade
call.

After I switched the ordering to
self.menu1.add_cascade(label="Edit",menu=self.editMenu)
I was properly attributing the editMenu to menu1 and it showed without error. This fix was listed as one of the comments in the StackOverflow post.