Bottom Page

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
Quote
#2
what is the 'Menu Addition Error'? You mention but don't identify the issue.
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Update value selected in option menu when select an item from text box klllmmm 2 136 Jun-06-2019, 04:51 AM
Last Post: klllmmm
  [Tkinter] Error in Tkinter moste 6 231 Apr-11-2019, 09:37 AM
Last Post: moste
  tkinter label error nonzzo 1 221 Mar-31-2019, 01:29 PM
Last Post: Larz60+
  Tkinter error for a scheduled event Ceegen 5 622 Jan-14-2019, 09:24 PM
Last Post: Ceegen
  Dropdown menu- Store variable aking76 1 465 Sep-11-2018, 01:30 PM
Last Post: aking76
  Menu bar not showing in gui window mlh 2 734 Sep-09-2018, 10:27 PM
Last Post: mlh
  [Tkinter] [python]Problem in creating menu[/python] yamoon 10 869 Aug-01-2018, 09:01 PM
Last Post: Larz60+
  [Tkinter] Tkinter optionmenu child menu position showing 0,0 thatguy14 2 821 Jun-15-2018, 10:42 AM
Last Post: thatguy14
  [Tkinter] [SOLVED] Create per button popup menu py2.7 AceScottie 5 1,193 May-31-2018, 12:39 AM
Last Post: AceScottie
  printing option menu variable in label in Tkinter SmokerX 1 1,907 Jan-18-2018, 07:36 PM
Last Post: SmokerX

Forum Jump:


Users browsing this thread: 1 Guest(s)