简体   繁体   中英

How to add if function to a tkinter button

I have a tk window (root frame). In this frame, there are three Checkbuttons, one Combobox and one Button. I want to create a function, which depends on two conditions:
- which Checkbutton is clicked
- what item is choosed from the Combobox
and than add this function to the button. 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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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