Using Button Function In Python GUI Application

Intro 

In this blog, I am going to create button clicking function in Python GUI application. When user click that Click Me button the button action will perform to change label text and color in that Python GUI application.

Software requirement

Python 3.5 and IDLE (Python 3.5)

Programming code

  1. import tkinter as tk  
  2. from tkinter  
  3. import ttk  
  4. win = tk.Tk()  
  5. win.title("Python GUI App")# Label  
  6. Lbl = ttk.Label(win, text = "Button Not Click ")  
  7. Lbl.pack()# Click event  
  8. def click(): action.configure(text = "Clicked")  
  9. Lbl.configure(foreground = 'red')  
  10. Lbl.configure(text = 'Button Clicked')# Adding Button  
  11. action = ttk.Button(win, text = "Click Me", command = click)  
  12. action.pack()  
  13. win.mainloop()  
About the code

 

First, I am Import the tkinter module, Next assign a class and variables and create application title and label in following code.after label creation next button creation code next button clicking function code and finally I am started the windows event loop by calling the mainloop method then execute the code the python GUI application will appear on screen.

Output

Before Click 

 
 
 After Click

 
Ebook Download
View all
Learn
View all