繁体   English   中英

如何将 if function 添加到 tkinter 按钮

[英]How to add if function to a tkinter button

我有一个 tk window(根框架)。 在这个框架中,有三个Checkbutton,一个Combobox和一个Button。 我想创建一个 function,这取决于两个条件:
- 单击了哪个 Checkbutton
- 从 Combobox 中选择了什么项目
然后将此 function 添加到按钮。 有几个框架,哪一个会弹出,取决于两个条件。

我的代码看起来像这样:(只是一个例子)

from tkinter import *

def continue():
    if cb1.get == 1 and cb2.get == 0 and cb3 == 0 and Combobox.get == "Frame1"
       createFrame1()
    elif cb2.get == 1 and cb1.get == 0 and cb3 == 0 and Combobox.get == "Frame2"
       createFrame2()
    elif cb3.get == 1 and cb1.get == 0 and cb2 == 0 and Combobox.get == "Frame3"
       createFrame3()

root = Tk()  
root.geometry("400x300")


itemsCombobox = ["Frame1", "Frame2", "Frame3"]   
var = StringVar()   
Combobox = ttk.Combobox(root, values = itemsCombobox)
Combobox.place(x=x, y=y)

cb1 = IntVar()   
cb2 = IntVar()   
cb3 = IntVar() 
Checkbutton1 = Checkbutton(Root, text="John", variable=cb1)   
Checkbutton2 = Checkbutton(Root, text="Peter", variable=cb2)
Checkbutton3 = Checkbutton(Root, text="Monica", variable=cb3)
Checkbutton1.place(x=x, y=y)
Checkbutton2.place(x=x, y=y)
Checkbutton3.place(x=x, y=y)

def createFrame1():
    Fr1 = tk.Toplevel(root)
    Fr.geometry("400x300")

def createFrame2():
    Fr2 = tk.Toplevel(root)
    Fr2.geometry("400x300")

def createFrame3():
    Fr3 = tk.Toplevel(root)
    Fr3.geometry("400x300")


Button = Button(root, text="Click me", command=continue)
Button.place(x=x, y=y)



root.mainloop()

但是,它不起作用。 我想我做错了什么,有人可以帮助我吗?

非常感谢:)

我无法理解您的问题,但我在您的代码中注意到的一件事是您对 function 名称使用了内置关键字“continue”。 将该名称更改为其他名称。 continue 是一个 python 关键字,您可以在循环中使用它来继续循环并跳过代码。

while True:
    print("I will print")
    continue
    print("I will never printed")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM