Hi I have comobox with some values (user select only one item) and I want acording to that select that the user will be allow to select multiselection from radiobutton. for example: comobox of elemnt- user select one element and after that select many crops grom radiobutton. I cant connect between those both. pls HELP my code:
def calibration_window():
global cb_elemnt,cb_crop
top = tkinter.Toplevel (window)
top.title ("Calibration")
top.geometry ("400x400")
ttk.Label (top, text="Select an element:",font = ("Segoe UI Light", 10)).grid (column=0, row=0)
cb_elemnt = ttk.Combobox (top, values=ele_list)
cb_elemnt.grid(column=0, row=1)
ttk.Label (top, text="Select a crop:",font = ("Segoe UI Light", 10)).grid (column=2, row=0)
# connect between element comobox to the func
cb_elemnt.bind ("<<ComboboxSelected>>",set_radio)
def set_radio(event):
i=0
radios=[]
for widget in radios:
widget.destroy ()
radios = []
if StringVar().get() !="N":
radio_values = pd.unique (df[df['elemnt'] == StringVar().get()]["crop"]) #take the suitable crop from DB
else:
radio_values = pd.unique (df_nir[df_nir['elemnt'] == StringVar().get()]["crop"])
for t in radio_values:
i = i + 1
b = Radiobutton (self, text=t, variable=IntVar(), value=t)
b.grid (row=i, column=0)
radios.append(b)
You should create your IntVar
and the list radio
outside of your function so they won't be garbage collected.
import tkinter as tk
from tkinter import ttk
import pandas as pd
import numpy as np
#sample data
df = pd.DataFrame({"element":np.random.choice(["Elem A","Elem B","Elem C"], 20),
"crops": [f"Crop {i}" for i in range(20)]})
root = tk.Tk()
r_var = tk.IntVar(value=0)
radios=[]
ttk.Label(root, text="Select an element:",font=("Segoe UI Light", 10)).grid(column=0, row=0)
cb_elemnt = ttk.Combobox(root, values=df["element"].unique().tolist())
cb_elemnt.grid(column=0, row=1)
ttk.Label(root, text="Select a crop:",font=("Segoe UI Light", 10)).grid(column=2, row=0)
def set_radio(event):
for widget in radios:
widget.destroy()
if cb_elemnt.get():
radio_values = df.loc[df["element"].eq(cb_elemnt.get()),"crops"]
for num, t in enumerate(radio_values, 1):
b = tk.Radiobutton(root, text=t, variable=r_var, value=t)
b.grid (row=num, column=1)
radios.append(b)
cb_elemnt.bind ("<<ComboboxSelected>>", set_radio)
root.mainloop()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.