无法更改 CheckButton 变量值
cannot change CheckButton variable value
我的 checkbtn 值有点问题。
这是我的代码:
def select_data():
select_window = Tk()
var_autotrader = IntVar()
check_sites_autotrader = Checkbutton(select_window, text = 'autotrader', variable = var_autotrader)
var_bestcarfinder = IntVar()
var_cardady = IntVar()
var_cars = IntVar()
var_car_gurus = IntVar()
var_iseecars = IntVar()
var_truecar = IntVar()
check_sites_bestcarfinder = Checkbutton(select_window, text = 'bestcarfinder', variable = var_bestcarfinder)
check_sites_cardaddy = Checkbutton(select_window, text = 'cardady', variable = var_cardady)
check_sites_cars = Checkbutton(select_window, text = 'cars.com', variable = var_cars)
check_sites_car_gurus = Checkbutton(select_window, text = 'car gurus', variable = var_car_gurus)
check_sites_iseecars = Checkbutton(select_window, text = 'iseecars', variable = var_iseecars)
check_sites_truecar = Checkbutton(select_window, text = 'truecar', variable = var_truecar)
check_sites_autotrader.grid(row = 0, column = 0, sticky = W)
check_sites_bestcarfinder.grid(row = 1, column = 0, sticky = W)
check_sites_cardaddy.grid(row = 2, column = 0, sticky = W)
check_sites_cars.grid(row = 3, column = 0, sticky = W)
check_sites_car_gurus.grid(row = 4, column = 0, sticky = W)
check_sites_iseecars.grid(row = 5, column = 0, sticky = W)
check_sites_truecar.grid(row = 6, column = 0, sticky = W)
def upload_selected_data():
print(var_autotrader.get())// This one does not give me 1 when check is on
btn = Button(select_window, text = 'go!', command = upload_selected_data)
btn.grid(row = 7, column = 0, sticky = W+E)
问题是当我勾选这个框时我的变量没有改变
非常感谢!
可能您使用 Tk()
创建了“主要 window”。在这种情况下,您应该使用 Toplevel()
(不是 Tk()
)创建另一个 window。
这意味着您应该将 select_window = Tk()
行更改为 select_window = Toplevel()
我的 checkbtn 值有点问题。
这是我的代码:
def select_data():
select_window = Tk()
var_autotrader = IntVar()
check_sites_autotrader = Checkbutton(select_window, text = 'autotrader', variable = var_autotrader)
var_bestcarfinder = IntVar()
var_cardady = IntVar()
var_cars = IntVar()
var_car_gurus = IntVar()
var_iseecars = IntVar()
var_truecar = IntVar()
check_sites_bestcarfinder = Checkbutton(select_window, text = 'bestcarfinder', variable = var_bestcarfinder)
check_sites_cardaddy = Checkbutton(select_window, text = 'cardady', variable = var_cardady)
check_sites_cars = Checkbutton(select_window, text = 'cars.com', variable = var_cars)
check_sites_car_gurus = Checkbutton(select_window, text = 'car gurus', variable = var_car_gurus)
check_sites_iseecars = Checkbutton(select_window, text = 'iseecars', variable = var_iseecars)
check_sites_truecar = Checkbutton(select_window, text = 'truecar', variable = var_truecar)
check_sites_autotrader.grid(row = 0, column = 0, sticky = W)
check_sites_bestcarfinder.grid(row = 1, column = 0, sticky = W)
check_sites_cardaddy.grid(row = 2, column = 0, sticky = W)
check_sites_cars.grid(row = 3, column = 0, sticky = W)
check_sites_car_gurus.grid(row = 4, column = 0, sticky = W)
check_sites_iseecars.grid(row = 5, column = 0, sticky = W)
check_sites_truecar.grid(row = 6, column = 0, sticky = W)
def upload_selected_data():
print(var_autotrader.get())// This one does not give me 1 when check is on
btn = Button(select_window, text = 'go!', command = upload_selected_data)
btn.grid(row = 7, column = 0, sticky = W+E)
问题是当我勾选这个框时我的变量没有改变
非常感谢!
可能您使用 Tk()
创建了“主要 window”。在这种情况下,您应该使用 Toplevel()
(不是 Tk()
)创建另一个 window。
这意味着您应该将 select_window = Tk()
行更改为 select_window = Toplevel()