Python 3 Tkinter Button Tutorial to Bind OnClick Event and Runs Command Automatically in GUI Desktop App Full Project For Beginners

Python 3 Tkinter Button Tutorial to Bind OnClick Event and Runs Command Automatically in GUI Desktop App Full Project For Beginners

 

Welcome folks today in this blog post we will be looking at button tutorial to bind onclick event and runs command automatically in tkinter gui desktop app in python. All the full source code of the application is given below.

 

 

Get Started

 

 

In order to get started you need to install the below library using the pip command as shown below

 

pip install tkinter

 

After installing this library you need to make an app.py file and copy paste the following code

 

app.py

 

import tkinter as tk

class Window(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Hello Tkinter")

        self.label = tk.Label(self, text="Choose One")
        self.label.pack(fill=tk.BOTH, expand=1, padx=100, pady=30)

        hello_button = tk.Button(self, text="Say Hello", command=self.say_hello)
        hello_button.pack(side=tk.LEFT, padx=(20, 0), pady=(0, 20))

        goodbye_button = tk.Button(self, text="Say Goodbye", command=self.say_goodbye)
        goodbye_button.pack(side=tk.RIGHT, padx=(0, 20), pady=(0, 20))

    def say_hello(self):
        self.label.configure(text="Hello World!")

    def say_goodbye(self):
        self.label.configure(text="Goodbye! \n (Closing in 2 seconds)")
        self.after(2000, self.destroy)


if __name__ == "__main__":
    window = Window()
    window.mainloop()

 

READ  Python 3 PySpark Library Example to Read CSV Files Using Spark Library Full Tutorial For Beginners

 

If you execute the above python script by typing the below command

 

python app.py

 

 

 

Leave a Reply