簡體   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