Tkinter,取消选中支票按钮?

时间:2016-12-09 10:52:05

标签: python tkinter

我对Tkinter很新,我制作了这个脚本,创建了一个新窗口,显示您在第一个窗口中选择的复选按钮。问题是,如果你选择一个,然后取消选择它,它仍然显示为已选中。什么能解决这个问题?感谢

import Tkinter
from Tkinter import *

global option1
global option2
option1 = 0
option2 = 0

master = Tk()

def getName():
    master.destroy()

def option1():
    global option1
    option1 = 1

def option2():
    global option2
    option2 = 1

def cb():
    global option1
    global option2
    window = Toplevel(master)
    if option1 == 1:
        titlelabel1 = Tkinter.Label(window, text="option 1", font =   ("calibri", 10, "bold"))
    if option2 == 1:
        titlelabel2 = Tkinter.Label(window, text="option 2", font = ("calibri", 10, "bold"))
    submit = Tkinter.Button(window, text ="Submit", command = getName, bg = "gray24", fg = "white")
    if option1 == 1:
        titlelabel1.pack()
    if option2 == 1:
        titlelabel2.pack()
    submit.pack()


titlelabel = Tkinter.Label(master, text="TICK BUTTONS", font = ("calibri", 18, "bold"))
option1 = Checkbutton(master, text="Option1", command = option1)
option2 = Checkbutton(master, text="Option2", command = option2)
submit = Tkinter.Button(master, text ="Submit",command = cb, bg = "gray28", fg = "white")
titlelabel.pack()
option1.pack()
option2.pack()
submit.pack()

mainloop()

工作解决方案 感谢帮助furas,我找到了一种方法,也许不是最简单的,但是,

oddarray = [1,3,5,7,9] 没有人会点击它超过9次? 将cb2中的部分从option2 = 1更改为,

option2 = option2 + 1
如果option1 == 1并且如果option2 == 1:to,则

更改, if option2 in oddary:

1 个答案:

答案 0 :(得分:0)

您的问题是,在您的功能中,您始终设置option1 = 1而永远不会option1 = 0

您可以使用False代替0并在功能中执行

option1 = not option1

False更改为True,将True更改为False

import tkinter as tk

# --- functions ---

def change_option1():
    # you need `global` to inform function
    # to use external variable instead of local
    # because you will assign value
    global var_option1 

    var_option1 = not var_option1

def change_option2():
    # you need `global` to inform function
    # to use external variable instead of local
    # because you will assign value
    global var_option2

    var_option2 = not var_option2


def subwindow():
    # you don't need `global` 
    # because you will not assign value
    #global var_option1
    #global var_option2

    window = tk.Toplevel(master)

    if var_option1:
        tk.Label(window, text="Option 1").pack()

    if var_option2:
        tk.Label(window, text="Option 2").pack()

    submit = tk.Button(window, text="Submit", command=window.destroy)
    submit.pack()

# --- main ---

# it creates global variable so you don't need `global`
var_option1 = False
var_option2 = False

master = tk.Tk()

lbl_title  = tk.Label(master, text="TICK BUTTONS")
cb_option1 = tk.Checkbutton(master, text="Option1", command=change_option1)
cb_option2 = tk.Checkbutton(master, text="Option2", command=change_option2)
btn_submit = tk.Button(master, text="Submit", command=subwindow)

lbl_title.pack()
cb_option1.pack()
cb_option2.pack()
btn_submit.pack()

master.mainloop()

或者您可以使用Tkinter特殊变量 - BooleanVar()IntVar()StringVar()然后您不需要command=但{{1} }}。

您必须使用variable=获取值,.get()来分配值

.set()