Creating a Simple GUI using Python tkinter 🐍

Posted by


In this tutorial, we will learn how to set up a basic GUI using Python’s built-in tkinter library. Tkinter is the standard GUI toolkit for Python, and it is simple to use and easy to learn. We will create a simple window with a label and a button that changes the label’s text when clicked.

Step 1: Install tkinter
Tkinter is included with Python, so you do not need to install anything extra. If you are using Python 3, you can import tkinter using the following line of code:

from tkinter import *

Step 2: Create the main window
To create the main window, we first need to create an instance of the Tk class. This will create a window that we can add widgets to. You can do this by adding the following lines of code:

root = Tk()
root.title("Basic GUI")

Step 3: Create a label
Now that we have our main window, we can add a label to it. Labels are used to display text or images. To create a label, you can use the following code:

label = Label(root, text="Hello, World!")
label.pack()

The label is added to the main window using the pack() method, which automatically arranges the widgets in a sensible way.

Step 4: Create a button
Next, let’s add a button to our GUI. Buttons are used to trigger actions when clicked. To create a button, you can use the following code:

def change_text():
    label.config(text="Button was clicked!")

button = Button(root, text="Click me", command=change_text)
button.pack()

Here, we define a function change_text() that changes the text of the label when called. We create a button with the text "Click me" that calls this function when clicked.

Step 5: Run the main loop
Finally, we need to run the main loop of our GUI. This infinite loop listens for events such as button clicks and updates the GUI accordingly. You can run the main loop with the following code:

root.mainloop()

That’s it! You have now created a simple GUI using tkinter in Python. You should see a window with a label displaying "Hello, World!" and a button that changes the label’s text when clicked. Feel free to experiment with different widgets and layouts to create your own unique GUIs. Happy coding! 🐍🎨

0 0 votes
Article Rating

Leave a Reply

49 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@BroCodez
5 hours ago

from tkinter import *

window = Tk() #instantiate an instance of a window

window.geometry("420×420")

window.title("Bro Code first GUI program")

icon = PhotoImage(file='logo.png')

window.iconphoto(True,icon)

window.config(background="#5cfcff")

window.mainloop() #place window on computer screen, listen for events

@ishmaelnaga8628
5 hours ago

Your the best @Bro Code 💪💪

@majik725
5 hours ago

Bro, this bro is the bro

@MrExplorer0
5 hours ago

Underrated channel. Nice explanation, and free code.

@iron4537
5 hours ago

i almost had a stroke because of how you pronounced GUI

@martinguarneros7222
5 hours ago

😇

@moo_HU
5 hours ago

Great style, also great explanation.Thx.

@Sheharyar69420
5 hours ago

Wow!

@corey-h2k
5 hours ago

what do u use to write this

@kazansergei
5 hours ago

nice work!!

@prdus
5 hours ago

"gooey"

@pianomaster212
5 hours ago

My code isn't working. Here's my code:

from tkinter import *

window = Tk()

window.geometry("420×420")

window.mainloop()

My error says that "Tcl" probably wasn't installed properly. Does anyone know what I did wrong?

@haaruncaderrr
5 hours ago

superrrr coolll

@madhavjoshi9040
5 hours ago

Wow! Lucid explanation!! Thank you!!!

@IThinkItsMe
5 hours ago

Brilliant video. Who's learning in 2024? Where's my 2024 family?

@joyomanda375
5 hours ago

Nice & easy explanation!

@mateoreardon9868
5 hours ago

bros actually helpful

@nidhidhanyadaneshdevadarsa533
5 hours ago

best tutorial ever i love it😃😃😃😃😃😃

@frankbeats733
5 hours ago

this is just amazing thank you so much

@rubenc4696
5 hours ago

Thanks

49
0
Would love your thoughts, please comment.x
()
x