Python Forum
Tkinter Gui Menu Addition Error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter Gui Menu Addition Error
#1
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
Reply
#2
what is the 'Menu Addition Error'? You mention but don't identify the issue.
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 5,614 Mar-10-2021, 04:21 PM
Last Post: Sir
  Tkinter menu font size -method to change tonycat 2 7,805 Oct-11-2020, 02:43 AM
Last Post: tonycat
  access subitem in tkinter Menu Phraya 2 2,968 Apr-04-2020, 12:11 PM
Last Post: Phraya
  Nesting menu buttons tkinter for python Mocap 1 2,713 Jul-18-2019, 11:46 AM
Last Post: metulburr
  [Tkinter] Tkinter optionmenu child menu position showing 0,0 thatguy14 2 4,638 Jun-15-2018, 10:42 AM
Last Post: thatguy14
  printing option menu variable in label in Tkinter SmokerX 1 6,615 Jan-18-2018, 07:36 PM
Last Post: SmokerX

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020