Multiple operations from a single button tkinter

后端 未结 2 1278
轮回少年
轮回少年 2021-01-24 13:07

I have been writing a program for a GUI based plotter using matplotlib and tkinter. I have added a toplevel window for some options. I want to execute a function and quit the to

2条回答
  •  旧时难觅i
    2021-01-24 13:25

    Buttons are designed for exactly this. Typically you would define a function or method that does whatever you want, then assign that method to the command attribute of the button:

    import Tkinter as tk
    import tkMessageBox
    
    class SampleApp(tk.Tk):
        def __init__(self):
            tk.Tk.__init__(self)
            button = tk.Button(text="Press me!", command=self.on_button)
            button.pack()
        def on_button(self):
            tkMessageBox.showinfo(message="Good-bye!")
            self.destroy()
    
    app = SampleApp()
    app.mainloop()
    

提交回复
热议问题