Okay, so today's problem is a code I decided to build to practice a range of skills for python. So the code is going to be a Tkinter version of conversion(s). It will eventually have a combo box with each potential conversion; hopefully, with minimal assistance, I'll figure most of it out with a few questions to redirect me to the resolution!
So currently I have 24 lines in which the root window is created. The size of the window should depend entirely on the screen dimensions(line 7). A combobox currently with only 1 item listed "length".
So this is where many/most of the for seeable code will be having an issue. Line 16-22 is my attempt of figuring out how to write this. (I probably need a better name than combo_0 but I was running short on time I'm thinking a better option as I add more potential conversions would be something a little longer but more explained such as combo_Length0) any suggestions on naming are appreciated as well.
from tkinter import *
from tkinter.ttk import *
#root window
root = Tk()
root.title("Conversion")
root.geometry(f'600x400 {int(root.winfo_screenwidth()/2-600/2)} {int(root.winfo_screenheight()/2-400/2)}')
#drop down widget "combo"
combo = Combobox(root)
combo['values']= ("Length")
combo.current(0) #set the selected item 'default'
combo.grid(column=0, row=0)
#widget check
if combo.get() == 0:
combo_0 = Combobox(root)
combo['values'] = ("Millimeter", "Centimeter", "meter"
"Kilometer", "Inch", "Foot", "Yard",
"Mile", "Nautical Mile")
combo_0.grid(column=1, row=1)
root.mainloop()
What I'm trying to do is based on the first Combobox currently the only option is "length" is when selected it changes to have the appropriate entry/combo boxes. so for example when "length" it would need 2 comboboxes both with same options ("Millimeter", "Centimeter", "meter", "Kilometer", "Inch", "Foot", "Yard", "Mile", "Nautical Mile") and a entry box. So you'd select inches to feet in the respective combobox and then enter the number to convert. It would also need a way to return the result to the screen so the user can see the conversion.
CodePudding user response:
What you are looking for is <<ComboboxSelected>>
.
Refer here
Here is how you would be coding it:
.
.
#widget check
def displayNextComboxes(event):
if combo.get() == "Length":
comboValues = ("Millimeter", "Centimeter", "meter"
"Kilometer", "Inch", "Foot", "Yard",
"Mile", "Nautical Mile")
combo_0 = Combobox(root)
combo_0['values'] = comboValues
combo_0.grid(column=0, row=1)
Label(text="TO ").grid(column=1, row=1)
combo_1 = Combobox(root)
combo_1['values'] = comboValues
combo_1.grid(column=2, row=1)
userValue = StringVar()
valueEntry = Entry(root, textvariable = userValue)
valueEntry.grid(column=1, row=2)
convertButton = Button(root,text="Convert", command=Convert)
convertButton.grid(column=1, row=3)
#Binded the combobox's selection with the function `displayNextComboxes`:
combo.bind("<<ComboboxSelected>>", displayNextComboxes)
def Convert():
pass #write here the code to check,
#convert, and display the value entered by the user to convert.
root.mainloop()