简体   繁体   English

如何将 if function 添加到 tkinter 按钮

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

I have a tk window (root frame).我有一个 tk window(根框架)。 In this frame, there are three Checkbuttons, one Combobox and one Button.在这个框架中,有三个Checkbutton,一个Combobox和一个Button。 I want to create a function, which depends on two conditions:我想创建一个 function,这取决于两个条件:
- which Checkbutton is clicked - 单击了哪个 Checkbutton
- what item is choosed from the Combobox - 从 Combobox 中选择了什么项目
and than add this function to the button.然后将此 function 添加到按钮。 There are several frames, which one is gonna popup, depends on the two conditions.有几个框架,哪一个会弹出,取决于两个条件。

My code looks something like this: (just one example)我的代码看起来像这样:(只是一个例子)

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()

However, it does not work.但是,它不起作用。 I think I'm doing something wrong, can somebody help me.我想我做错了什么,有人可以帮助我吗?

Thanks a lot:)非常感谢:)

I couldn't understand your question but one thing that I noticed in your code is that you use a built-in keyword 'continue' for your function name.我无法理解您的问题,但我在您的代码中注意到的一件事是您对 function 名称使用了内置关键字“continue”。 Change that name to something else.将该名称更改为其他名称。 continue is a python keyword which you can use in loops to continue the loop and jump over the code. 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