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
49 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@BroCodez
1 month 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
1 month ago

Your the best @Bro Code šŸ’ŖšŸ’Ŗ

@majik725
1 month ago

Bro, this bro is the bro

@MrExplorer0
1 month ago

Underrated channel. Nice explanation, and free code.

@iron4537
1 month ago

i almost had a stroke because of how you pronounced GUI

@martinguarneros7222
1 month ago

šŸ˜‡

@moo_HU
1 month ago

Great style, also great explanation.Thx.

@Sheharyar69420
1 month ago

Wow!

@corey-h2k
1 month ago

what do u use to write this

@kazansergei
1 month ago

nice work!!

@prdus
1 month ago

"gooey"

@pianomaster212
1 month 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
1 month ago

superrrr coolll

@madhavjoshi9040
1 month ago

Wow! Lucid explanation!! Thank you!!!

@IThinkItsMe
1 month ago

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

@joyomanda375
1 month ago

Nice & easy explanation!

@mateoreardon9868
1 month ago

bros actually helpful

@nidhidhanyadaneshdevadarsa533
1 month ago

best tutorial ever i love itšŸ˜ƒšŸ˜ƒšŸ˜ƒšŸ˜ƒšŸ˜ƒšŸ˜ƒ

@frankbeats733
1 month ago

this is just amazing thank you so much

@rubenc4696
1 month ago

Thanks