I am developing a GUI which will allow a user to select an item from the dropdown list, get that item and use it elsewhere in the code. This is not working quite well.
from tkinter import *
from tkinter import ttk
class Root(Tk):
def __init__(self):
super(Root, self).__init__()
self.title("Vibration Certificate Generator")
self.minsize(640, 400)
#self.wm_iconbitmap('icon.ico')
self.AddMenu()
self.selectDUT()
def AddMenu(self):
self.dropMenu = Menu()
self.config(menu=self.dropMenu)
self.fileMenu = Menu(self.dropMenu, tearoff=0) #file drop down menu
self.dropMenu.add_cascade(label="File", menu=self.fileMenu)
self.fileMenu.add_command(label="New Project...",
command=self.doNothing)
self.fileMenu.add_command(label="Now...", command=self.doNothing)
self.fileMenu.add_separator() #adds line
self.fileMenu.add_command(label="Exit", command=self.doNothing)
self.editMenu = Menu(self.dropMenu, tearoff=0) #edit drop down menu
self.dropMenu.add_cascade(label="Edit", menu=self.editMenu)
self.editMenu.add_command(label="Undo", command=self.doNothing)
self.editMenu.add_command(label="Redo", command=self.doNothing)
self.editMenu.add_separator() #adds line
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_command(label="Select All", command=self.doNothing)
#self.statusBar = ttk.Label(self, text="Generating a certificate for...")
self.statusBar = ttk.Label(self, text="Generating a certificate for...")
self.statusBar.grid(column=0,row=4)
def clickedDUT(self):
self.statusBar.configure(text="You Have Selected " + self.SelectedDUT.get())
print(self.SelectedDUT.get())
def doNothing(self):
print("ok ok i won't...")
def selectDUT(self):
self.SelectedDUT = StringVar()
#print(self.SelectedDUT.get())
self.SelectDeviceUnderTest = ttk.Combobox(self, width=30, textvariable=self.SelectedDUT.get())
self.SelectDeviceUnderTest['values'] = ("ACCELEROMETER", "VELOCITY TRANSDUCER",
"IMPEDANCE HEAD", "CONDTIONING AMPLIFIER", "VIBRATION ANALYSER",
"VIBRATION METER", "TRIAXIAL ACCELEROMETER")
self.SelectDeviceUnderTest.grid(column=1, row=0)
self.SelectDeviceUnderTest.current(0)
self.label = ttk.Label(self, text="Select your DUT:")
self.label.grid(column=0, row=0)
self.buttonDUT = ttk.Button(self, text="OK", command=self.clickedDUT())
self.buttonDUT.grid(column=2,row=0)
root = Root()
root.mainloop()
I want the clickedDUT()
to update with the value from SelectedDUT.get()
obtained from the Combobox list option, after clicking OK.
There are two small parts that are wrong in your code. The first one is how you set the textvariable
for your Combobox
. You should pass the variable directly but not the get
method:
def selectDUT(self):
self.SelectedDUT = StringVar()
self.SelectDeviceUnderTest = ttk.Combobox(self, width=30, textvariable=self.SelectedDUT)
...
The second one is a common mistake that you executed the command self.clickedDUT
instead of passing a reference in self.buttonDUT
. Change to this:
self.buttonDUT = ttk.Button(self, text="OK", command=self.clickedDUT) #drop the trailing ()
If you want self.statusBar
to appear on the bottom, you have to set a weight to every row:
def AddMenu(self):
...
for i in range(1,10):
self.grid_rowconfigure(i, weight=1)
self.statusBar = ttk.Label(self, text="Generating a certificate for...")
self.statusBar.grid(column=0,row=10,columnspan=2,sticky="w")