Can we nest two OptionMenu widgets in tkinter? - python

Just like the famous example of having Continent names in one dropdown and display country names related to selected Continent. How to achieve this using Tkinter?
I have Continent list in first dropdown and list of all countries related to continents in the list. I would like to display country_11,country_12 when continent_1 is selected, similarly for other continents.
Here is the piece of code am working on -
import tkinter as tk
from tkinter import ttk
from tkinter import *
root = tk.Tk()
root.geometry('500x500')
#Label to Continent
label_1 = tk.Label(root, text="Select the Continent", font = (8), bg = '#ffe1c4')
label_1.place(x = 120, y = 220)
# Continent selection - drop down
optionList1 = ["Continent1", "Continent2","Continent3"]
dropVar1 = StringVar()
dropMenu1 = ttk.OptionMenu(root, dropVar1 , *optionList1)
dropMenu1.place(x = 300, y = 220)
#Label to Select Country
label_2 = tk.Label(root, text="Select the Country ", font = (8), bg = '#ffe1c4')
label_2.place(x = 120, y = 250)
# Country name selection - drop down
optionList2 = ["Country_11", "Country_12", "Country_21","Country_22","Country_31","Country_32"]
dropVar2 = StringVar()
dropMenu2 = ttk.OptionMenu(root, dropVar2, *optionList2)
dropMenu2.place(x = 300, y = 250)
root.mainloop()
Would be great to have a solution to this as am not aware of all the attributes OptionMenu can have in Tkinter.
Thanks in advance!!

If your mean is to create two OptionMenu and it will show the different value when select different value in the first dropdown menu.
You can try this:
import tkinter as tk
from tkinter import ttk
from tkinter import *
def func(selected_value): # the selected_value is the value you selected in the first drop down menu.
dropMenu2.set_menu(*optionList2.get(selected_value))
root = tk.Tk()
root.geometry('500x500')
#Label to Continent
label_1 = tk.Label(root, text="Select the Continent", font = (8), bg = '#ffe1c4')
label_1.place(x = 120, y = 220)
# Continent selection - drop down
optionList1 = ["-","Continent1", "Continent2","Continent3"]
dropVar1 = StringVar()
dropMenu1 = ttk.OptionMenu(root, dropVar1 , *optionList1,command=func) # bind a command for the first dropmenu
dropMenu1.place(x = 300, y = 220)
#Label to Select Country
label_2 = tk.Label(root, text="Select the Country ", font = (8), bg = '#ffe1c4')
label_2.place(x = 120, y = 250)
# Country name selection - drop down
optionList2 = { # when select different value,show the list.
"Continent1": ["Country_11", "Country_12"],
"Continent2": ["Country_21", "Country_22"],
"Continent3": ["Country_31", "Country_32"]
}
dropVar2 = StringVar()
dropMenu2 = ttk.OptionMenu(root, dropVar2, "-")
dropMenu2.place(x = 300, y = 250)
root.mainloop()
Now it is:
When select another value:
(A suggestion:ttk.Combobox is prettier than OptionMenu,and using from tkinter import * is not a good practice.)

If you mean menu inside a menu then it is possible and very simple to do because the menu used in OptionMenu() is a tkinter Menu(), See the documentation of tkinter Menu.
We can access the Menu like so
Op = OptionMenu(root, var, 'Hello', 'HI', 'YOO')
# Op_Menu is the Menu() class used for OptionMenu
Op_Menu = Op['menu']
Here is a small example of nested menus in an option menu. When you select any of the countries inside any continent the text of optionmenu won't change so to fix that I used command argument and in each command argument of country I'm changing the value of the StringVar that is assigned to the optionmenu.
import tkinter as tk
root = tk.Tk()
svar = tk.StringVar()
svar.set('Antarctica')
Op = tk.OptionMenu(root, svar, svar.get())
OpMenu = Op['menu']
Op.pack()
Menu1 = tk.Menu(OpMenu)
OpMenu.add_cascade(label='Africa', menu= Menu1)
Menu1.add_command(label='Algeria', command=lambda: svar.set('Africa - Algeria'))
Menu1.add_command(label='Benin', command=lambda: svar.set('Africa - Benin'))
Menu2 = tk.Menu(Op['menu'])
OpMenu.add_cascade(label='Asia', menu= Menu2)
Menu2.add_command(label='China', command=lambda: svar.set('Asia - China'))
Menu2.add_command(label='India', command=lambda: svar.set('Asia - India'))
root.mainloop()
Hope you find this helpful.

Related

Text Field that appears if user choose Yes in the combo box in tkinter

I want to add a text field and input text if the user choose "Yes" in the combo box; other wise nothing will be added. Tried this but didn't work
Combo = ttk.Combobox(window, values = vlist)
Combo.set("Pick an Option")
Combo.pack()
if (Combo.get()=="Yes"):
tkinter.Label(window, text="Number of Workers", bg = "white",fg = "black", font=("Times",13,"bold")).place(x=155,y=240)
inputtxt3 = Text(window, height = 1, width = 25, bg = "light yellow")
inputtxt3.pack()
inputtxt3.place(x=325, y=238)
Does this help you?
from tkinter import Tk, StringVar
from tkinter.ttk import Label, Combobox, Button
root = Tk()
def check(): # For checking the value of ComboBox
combo_value = var.get()
if combo_value == "Yes":
lbl.config(text="You selected 'Yes'")
else:
lbl.config(text="You selected 'No'")
var = StringVar() # To store the value of the Combobox
Combo = Combobox(root, values=["Yes", "No"], textvariable=var)
Combo.pack(pady=20)
btn = Button(root, text="Confirm", command=check)
btn.pack(pady=20)
lbl = Label(root)
lbl.pack(pady=20)
root.mainloop()

How to enable a disabled Button after filling Entry widgets?

I have 2 Entrys and one button. I want to make that button's state disabled until the two Entrys are filled in. How can I achieve that?
howManyStocksLabel = Label(root, text = "How many stocks do you want to evaluate?")
howManyStocksLabel.grid(row = 1, column = 0)
howManyStocksEntry = Entry(root, borderwidth = 3)
howManyStocksEntry.grid(row = 1, column = 1)
riskLabel = Label(root, text = "Enter risk %")
riskLabel.grid(row = 2, column = 0, sticky = 'w')
riskEntry = Entry(root, borderwidth = 3)
riskEntry.grid(row = 2, column = 1)
nextButton = Button(root, text = "Next!", width = 20, height = 2,state = DISABLED,
fg = 'green', bg = 'white',
command= lambda: myClick(riskEntry, howManyStocksEntry, var))
nextButton.grid(row = 4, column = 1)
I tried to check whether the entries are filled in or not by:
if(riskEntry.get() != ""):
....................
but it just doesn't work.
You need to check if the value is there after the user inputs it. Also, you can use tk.StringVar() as a text variable and trace it.
Here is an example:
import tkinter as tk
def check_entry(*args):
if r1.get() and r2.get():
b1.config(state='normal')
else:
b1.config(state='disabled')
root = tk.Tk()
r1 = tk.StringVar(master=root)
r2 = tk.StringVar(master=root)
e1 = tk.Entry(root, textvariable=r1)
e1.pack()
e2 = tk.Entry(root, textvariable=r2)
e2.pack()
b1 = tk.Button(root, text='Click Me!', state='disabled')
b1.pack()
r1.trace('w', check_entry)
r2.trace('w', check_entry)
root.mainloop()
You will need to use a binding on your entry widgets to check whether the user has entered anything into the entry or not.
This code will fire the check_entry function every time the user types in one of the entry boxes:
riskEntry.bind('<KeyRelease>', check_entry)
howManyStocksEntry.bind('<KeyRelease>', check_entry)
Then your check_entry function might look like this:
def check_entry(event): #event is required for all functions that use a binding
if riskEntry.get() and howManyStocksEntry.get():
nextButton.config(state=NORMAL)
else:
nextButton.config(state=DISABLED)
One way to do it would be to utilize the ability to "validate" their contents that Entry widgets support — see adding validation to an Entry widget — but make it check the contents of multiple Entry widgets and change the state of a Button accordingly.
Below shows how to do this via a helper class that encapsulates most of the messy details needed to make doing it relatively painless. Any number of Entry widgets can be "watched", so it scales well to handle forms consisting of many more than merely two entries.
from functools import partial
import tkinter as tk
from tkinter.constants import *
class ButtonEnabler:
""" Enable/disable a Button depending on whether all specified Entry widgets
are non-empty (i.e. contain at least one character).
"""
def __init__(self, button, *entries):
self.button = button
self.entries = entries
for entry in self.entries:
func = root.register(partial(self.check_entries, entry))
entry.config(validate="key", validatecommand=(func, '%P'))
def check_entries(self, this_entry, new_value):
other_entries = (entry for entry in self.entries if entry is not this_entry)
all_others_filled = all(entry.get() for entry in other_entries)
combined = bool(new_value) and all_others_filled
self.button.config(state=NORMAL if combined else DISABLED)
return True
root = tk.Tk()
howManyStocksLabel = tk.Label(root, text="How many stocks do you want to evaluate?")
howManyStocksLabel.grid(row=1, column=0)
howManyStocksEntry = tk.Entry(root, borderwidth=3)
howManyStocksEntry.grid(row=1, column=1)
riskLabel = tk.Label(root, text="Enter risk %")
riskLabel.grid(row=2, column=0, sticky='w')
riskEntry = tk.Entry(root, borderwidth=3)
riskEntry.grid(row=2, column=1)
nextButton = tk.Button(root, text="Next!", width=20, height=2, state=DISABLED,
fg='green', bg='white', disabledforeground='light grey',
command=lambda: myClick(riskEntry, howManyStocksEntry, var))
nextButton.grid(row=4, column=1)
enabler = ButtonEnabler(nextButton, howManyStocksEntry, riskEntry)
root.mainloop()

Activate Entry Box from Listbox in Tkinter

I'm trying to create a GUI to collect three inputs from a user, where the first input has two options (based on a Listbox), like the image below (where the option "NACA" is selected):
The problem is with the Listbox. I guess the options are overlapping each other. For example, If I select the NACA option (image above), and then I select the .txt option, some part of the NACA label remains:
And of course, just the labels are appearing, not the entry_boxes to type inside (if I delete the part of the listBox, the entry_boxes of the last two input appers, so I really guess the problem is with the ListBox)
import tkinter as tk
root = tk.Tk()
root.geometry('400x300')
root.resizable(0, 0)
menu_inicial = tk.Canvas(root, width = 400, height = 300)
menu_inicial.pack()
def naca_box():
naca_entry_box = tk.Entry(menu_inicial)
menu_inicial.create_window(200, 30, window=naca_entry_box)
naca_label = tk.Label(root, text="Enter NACA:")
naca_label.pack()
naca_label.place(x=50, y = 50)
def txt_box():
txt_entry_box = tk.Entry(menu_inicial)
menu_inicial.create_window(200, 30, window=txt_entry_box)
txt_label = tk.Label(root, text="Enter .txt:")
txt_label.pack()
txt_label.place(x=50, y = 50)
def aoa_box():
aoa_entry_box = tk.Entry(root)
menu_inicial.create_window(200, 60, window=aoa_entry_box)
aoa_label = tk.Label(root, text="Enter AoA (º):")
aoa_label.pack()
aoa_label.place(x=50, y = 80)
def panel_box():
panel_entry_box = tk.Entry(root)
menu_inicial.create_window(200, 90, window=panel_entry_box)
panel_label = tk.Label(root, text="Enter Nº Panels:")
panel_label.pack()
panel_label.place(x=40, y = 110)
def update_box(*args):
selection = box_list.curselection()
lb_value.set(options[selection[0]] )
if selection[0] == 0:
naca_box()
else:
txt_box()
options = ['NACA', '.txt']
listCon = tk.StringVar(value=options)
box_list = tk.Listbox(menu_inicial, listvariable=listCon, width=10, height=2, selectmode=tk.SINGLE)
box_list.grid(row=0, column=0)
box_list.bind('<<ListboxSelect>>', update_box)
lb_value=tk.StringVar()
aoa_box()
panel_box()
root.mainloop()
How can I proceed with this? Is a problem with the "IF" statement to choose the options ? (I don't know if this is the best what to do this...)
The problem is that while you clicking button each time,
you are creating label each time,
if you want to change the text in the label you created,use 'config()'
change your code like this,
sample_label=tk.Label(root)
sample_label.place(x=50, y = 50)
def naca_box():
naca_entry_box = tk.Entry(menu_inicial)
menu_inicial.create_window(200, 30, window=naca_entry_box)
sample_label.config(text="Enter NACA:")
def txt_box():
txt_entry_box = tk.Entry(menu_inicial)
menu_inicial.create_window(200, 30, window=txt_entry_box)
sample_label.config(text="Enter .txt:")

tkinter: dynamically create and delete entry

I am using tkinter to create a small GUI for some Python scripts. For this GUI I need a Combobox named combo with three options:
"none" (the user wants to hand in no data)
"constant" (the user wants to hand in only one value)
"adjustable" (the user wants to hand in more than one value)
Depending on the choice done in combo, a different number of entrys should
appear. The first option should show no entry (and delete all "leftover"
entrys), the second one should show only one entry and the third one should show
two entrys. For me it's no problem to create these entrys but I don't know how
to make them disappear. Once they were created they stay until the GUI is
closed.
I tried something like this:
import tkinter as tk
master = tk.Tk()
var1 = tk.StringVar()
var2 = tk.StringVar()
def submit():
if choice.get() == "none": # all entry boxes schould disappear
entry1.destroy()
entry2.destroy()
if choice.get() == "constant": # only 1 entry box schould be visible
entry1 = tk.Entry(master, textvariable = var1)
entry1.grid(column = 0, row = 1)
entry2.destroy()
if choice.get() == "adjustable": # all entry boxes should be visible
entry1 = tk.Entry(master, textvariable = var1)
entry1.grid(column = 0, row = 1)
entry2 = tk.Entry(master, textvariable = var1)
entry2.grid(column = 0, row = 2)
choice = tk.StringVar(value = "none")
combo = ttk.Combobox(master, textvariable = choice, state = "readonly")
combo["values"] = ("none", "constant", "adjustable")
combo.grid(column = 0, row = 0)
action = tk.Button(master, text = "confirm", command = submit)
action.grid(column=1, row=0)
master.mainloop()
But as I said before, once a entry was created it did not disappear anymore. I
also tried entry.grid_forget() insted of entry.destroy() but this also
didn't work.
I also tryed to declare entry1 and entry1 outside of submit() but then I
don't know how to recreate these entrys once they were destroyed.
Thanks to the help of jasonharper I found a solution while working at another script. The code should look something like this:
import tkinter as tk
from tkinter import ttk
master = tk.Tk()
var1 = tk.StringVar()
var2 = tk.StringVar()
def submit():
if choice.get() == "none": # all entry boxes schould disappear
entry1.grid_remove()
entry2.grid_remove()
if choice.get() == "constant": # only 1 entry box schould be visible
entry1.grid(column = 0, row = 1)
entry2.grid_remove()
if choice.get() == "adjustable": # all entry boxes should be visible
entry1.grid(column = 0, row = 1)
entry2.grid(column = 0, row = 2)
choice = tk.StringVar(value = "none")
combo = ttk.Combobox(master, textvariable = choice, state = "readonly")
combo["values"] = ("none", "constant", "adjustable")
combo.grid(column = 0, row = 0)
entry1 = tk.Entry(master, textvariable = var1)
entry2 = tk.Entry(master, textvariable = var2)
action = tk.Button(master, text = "confirm", command = submit)
action.grid(column=1, row=0)
master.mainloop()
Now all entrys are deleted or created when wanted. To delete the text at the entrys You only have to add entry.delete(0,"end").
I hope this will also help others.

Need assistance Tkinter in Python 2.7

I am writing a subnetting program in Python and I have come across a problem.
So far everything is working minus one thing. I dont know how to change a label in a method. in the code below, SubnetM is the variable being used to show the subnet mask. It is set to 0 by default but when you select HOSTS and enter 6 as Quantity. The 0 does not change to 255.255.255.248. PLEASE HELP
from Tkinter import *
SubnetM = 0
def beenclicked():
radioValue = relStatus.get()
return
def changeLabel():
if radio1 == 'HOSTS':
if Quantity == 6:
SubnetM = "255.255.255.248"
return
app = Tk()
app.title("SUBNET MASK CALCULATOR")
app.geometry('400x450+200+200')
labelText = StringVar()
labelText.set("WELCOME!")
label1 = Label(app,textvariable=labelText, height=4)
label1.pack()
relStatus = StringVar()
relStatus.set(None)
radio1 = Radiobutton(app, text="HOSTS", value="HOSTS", variable=relStatus, command=beenclicked).pack()
radio1 = Radiobutton(app, text="NETWORKS", value="NETWORKS", variable=relStatus, command=beenclicked).pack()
label2Text = StringVar()
label2Text.set("~Quantity~")
label2 = Label(app, textvariable=label2Text, height=4)
label2.pack()
custname = IntVar(None)
Quantity = Entry(app,textvariable=custname)
Quantity.pack()
label3Text = StringVar()
label3Text.set("Your Subnet Mask is...")
label3 = Label(app, textvariable=label3Text, height=4)
label3.pack()
label4Text = StringVar()
label4Text.set(SubnetM)
label4 = Label(app, textvariable=label4Text, height=4)
label4.pack()
button1 = Button(app, text="GO!", width=20, command=changeLabel)
button1.pack(padx=15, pady=15)
app.mainloop()
To fix your problem, make changeLabel like this:
def changeLabel():
# Get the radiobutton's StringVar and see if it equals "HOSTS"
if relStatus.get() == 'HOSTS':
# Get the entrybox's IntVar and see if it equals 6
if custname.get() == 6:
# Set the label's StringVar to "255.255.255.248"
label4Text.set("255.255.255.248")
Also, the .pack method of a Tkinter widget returns None. So, you should make the part that defines the radiobuttons like this:
radio1 = Radiobutton(app, text="HOSTS", value="HOSTS", variable=relStatus, command=beenclicked)
radio1.pack()
radio2 = Radiobutton(app, text="NETWORKS", value="NETWORKS", variable=relStatus, command=beenclicked)
radio2.pack()

Categories