简体   繁体   English

Tkinter 通过检查 Checkbuttons 运行多个功能

[英]Tkinter run multiple functions by checking the Checkbuttons

I'm building a script to make a GUI window presenting some functions I made before.我正在构建一个脚本来制作一个显示我之前制作的一些功能的 GUI 窗口。

I would like to tick the buttons which I want to run the functions.我想勾选要运行功能的按钮。 So far I can run a function by checking the checkbox.到目前为止,我可以通过选中复选框来运行一个函数。 But only one.但只有一个。

button1 = ttk.Checkbutton(window,
    command = function1
    )

But I have several check buttons and at the end 'Run' button to run the all functions checked above.但是我有几个检查按钮,最后是“运行”按钮来运行上面检查的所有功能。

button1 = ttk.Checkbutton(window,
    )
button2 = ttk.Checkbutton(window,
    )
button3 = ttk.Checkbutton(window,
    )

run_button = ttk.Button(window,
    text = 'run',
    command = proper command to run the functions ticked above
    )

Is there any way to make it possible?有没有办法让它成为可能?

  • And plus I would like to close the GUI window once I hit the run button, but couldn't find a solution yet.另外,我想在点击运行按钮后关闭 GUI 窗口,但还没有找到解决方案。

Thanks in advance!!提前致谢!!

Please check this snippet that performs hardcoded add,subtract,multiply,delete functions.请检查这个执行硬编码加、减、乘、删除功能的代码段。

  1. As you tick the checkbox the respective function is triggered.当您勾选复选框时,将触发相应的功能。
  2. As you click the run button, all the functions will be triggered.当您单击运行按钮时,将触发所有功能。
  3. After clicking run button the output will be printed as well as the tkinter window will be closed.单击运行按钮后,将打印输出并关闭 tkinter 窗口。
from tkinter import *
master = Tk()

def run_all():
    var1.set(1)
    var2.set(1)
    var3.set(1)
    var4.set(1)
    ad()
    sub()
    mul()
    div()
    master.destroy()

def ad():
    if(var1.get()==1):
        print(5+5)
def sub():
    if(var2.get()==1):
        print(5-5)
def mul():
    if(var3.get()==1):
        print(5*5)
def div():
    if(var4.get()==1):
        print(5/5)
Label(master, text="Operations:").grid(row=0, sticky=W)
var1 = IntVar()
Checkbutton(master, text="Add", variable=var1,command=ad).grid(row=1, sticky=W)
var2 = IntVar()
Checkbutton(master, text="Subtract", variable=var2,command=sub).grid(row=2, sticky=W)
var3 = IntVar()
Checkbutton(master, text="Multiply", variable=var3,command=mul).grid(row=3, sticky=W)
var4 = IntVar()
Checkbutton(master, text="Divide", variable=var4,command=div).grid(row=4, sticky=W)
Button(master, text='Run', command=run_all).grid(row=5, sticky=W, pady=4)
mainloop()

Edit: Based on the comment, now all functions will run only when you press the run button编辑:根据评论,现在所有功能只有在您按下运行按钮时才会运行

Label(master, text="Operations:").grid(row=0, sticky=W)
var1 = IntVar()
Checkbutton(master, text="Add", variable=var1).grid(row=1, sticky=W)
var2 = IntVar()
Checkbutton(master, text="Subtract", variable=var2).grid(row=2, sticky=W)
var3 = IntVar()
Checkbutton(master, text="Multiply", variable=var3).grid(row=3, sticky=W)
var4 = IntVar()
Checkbutton(master, text="Divide", variable=var4).grid(row=4, sticky=W)
Button(master, text='Run', command=run_all).grid(row=5, sticky=W, pady=4)
mainloop()

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

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