in this Python TKinter GUI article i want to show you Creating Buttons and also we are
going to handle click events for the Button.
Also you can read more articles on Python GUI Development
1: PyQt5 GUI Development Tutorials
2: Pyside2 GUI Development Tutorials
3: wxPython GUI Development Tutorials
4: Kivy GUI Development Tutorials
So this is the complete code for Python TKinter GUI Creating Buttons
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import tkinter as tk from tkinter import ttk window = tk.Tk() window.minsize(600,400) window.title("Tkinter Button") label = ttk.Label(window, text = "Hello Tkinter") label.grid(column = 1, row = 0) def clickMe(): label.configure(text = "The Text Changed") label.configure(foreground = 'green') button.configure(text = "The Button Has Been Clicked") button = ttk.Button(window, text = "Click Me", command =clickMe) button.grid(column =0, row = 0) window.mainloop() |
So first of all we need to import tkinter also we are going to import ttk. ttk stands for themed tk.
It improves our GUI’s look and feel. after that we create our window and also we set a minsize to
our window by adding window.minsize(600, 400) , basically we need 600 by 400 window also
we add a title to our Window. now we create a label, for more information about Labels In
TKinter you can see my TKinter Label article , so you can use ttk.Label() for creating label in the
constructor of the Label . we add some requirements the first one is the window that we want to
add our label and the second one is the label text also we are going to figure the label in a grid label
by using label.grid(coulmn = 1, row = 0) , and we add the column and row positions for our label.
and after that we have a method of clickMe(), this is the method that we want to make relationship
between our label and Button. so basically by clicking of the button we want to change the color and
the text of the label, for doing this functionality we use label.configure(), this is for the text
changing and label.configure() for the color change also we add a button.configure().so now this is
the time that we create our button, we are using ttk this gives the nice modern GUI design so you
can do ttk.Button() , and in the constructor we add the window, the text of the button and also the
important point we adding a command and we add the name of the method clickMe() that we have
before created.at the end we create the main loop for the window
Run the complete code and this will be the result
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email