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! 🐍🎨
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
Your the best @Bro Code 💪💪
Bro, this bro is the bro
Underrated channel. Nice explanation, and free code.
i almost had a stroke because of how you pronounced GUI
😇
Great style, also great explanation.Thx.
Wow!
what do u use to write this
nice work!!
"gooey"
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?
superrrr coolll
Wow! Lucid explanation!! Thank you!!!
Brilliant video. Who's learning in 2024? Where's my 2024 family?
Nice & easy explanation!
bros actually helpful
best tutorial ever i love it😃😃😃😃😃😃
this is just amazing thank you so much
Thanks