Create Graphical User Interfaces With Python And TKinter
Summary
TLDRIn this tutorial video, John from Coding for All introduces viewers to creating graphical user interfaces (GUIs) with Python using the built-in tkinter module. He demonstrates the simplicity of the process, guiding through the creation of a basic 'Hello World' window. The video covers importing tkinter, defining widgets, and setting up an event loop. It's an introductory lesson aimed at beginners, promising a series for further exploration of GUI development with Python.
Takeaways
- 😀 The video is a tutorial on creating graphical user interfaces (GUIs) with Python using the tkinter module.
- 📝 The presenter is John, from codingbeauty.com, and he introduces the concept of GUIs in Python.
- 💻 It's assumed that viewers have Python installed on their machines and basic knowledge of running Python scripts.
- 📚 The tutorial will be a series of videos, starting with the basics of tkinter for beginners.
- 🛍️ Viewers are encouraged to subscribe and use a coupon code 'youtube' for a discount on courses at codingbeauty.com.
- 📝 The first step in creating a GUI with tkinter is to import the module using 'from tkinter import *'.
- 🏠 The 'root' widget is created first, which represents the main window of the GUI application.
- 📌 The 'label' widget is used to display text in the GUI, and it's created and packed into the root widget.
- 🔄 The 'pack' method is introduced as a way to place widgets in the GUI, with a simple packing strategy.
- 🔁 The 'mainloop' function is essential to start the event loop, which keeps the GUI application running and responsive.
- 🖼️ The video demonstrates how to run the simple GUI program and shows the resulting window with 'Hello World' text.
- 🔍 The video promises to cover more widgets and layout methods in upcoming videos, providing more control over GUI design.
Q & A
What is the main topic of the video?
-The main topic of the video is teaching how to create graphical user interfaces (GUIs) with Python using the tkinter module.
Who is the presenter of the video?
-The presenter of the video is John, from codingbe.com.
What is the purpose of the tkinter module in Python?
-The tkinter module in Python is used to create GUIs. It allows for the creation of windows, buttons, text fields, and other interface elements.
What is the first step in creating a GUI with tkinter according to the video?
-The first step in creating a GUI with tkinter is to import the tkinter module and use it to create the root widget, which represents the main window of the application.
What is a 'widget' in the context of tkinter?
-In the context of tkinter, a 'widget' is any GUI element such as a button, text field, frame, etc. Everything in tkinter is considered a widget.
How does the video suggest organizing the GUI elements on the screen?
-The video suggests using the 'pack' method for organizing GUI elements on the screen, which places them in the first available spot.
What is the purpose of the 'main loop' in a tkinter program?
-The 'main loop' in a tkinter program is responsible for keeping the program running and responding to user interactions such as mouse movements and button clicks.
What should you do if you want to see more videos like this one?
-To see more videos like this one, you should subscribe to the channel and click the Bell icon to get notifications for new uploads.
What is the special offer mentioned in the video for codingbe.com courses?
-The special offer mentioned in the video is a $22 discount on a membership to codingbe.com using the coupon code 'youtube', which gives access to all courses, videos, and books for a one-time fee of $27.
How can you run the Python script created in the video?
-You can run the Python script created in the video by opening a terminal or command prompt, navigating to the directory where the script is saved, and typing 'python filename.py'.
What is the significance of the 'root.mainloop()' method in the tkinter program?
-The 'root.mainloop()' method starts the event loop that allows the tkinter program to continuously run and respond to events, and it ends when the window is closed.
Outlines
💻 Introduction to GUI with Python and Tkinter
In this introductory video, John, from Coding for All, welcomes viewers and announces the topic: creating graphical user interfaces (GUIs) with Python using the Tkinter module. Tkinter is a built-in Python library that simplifies the creation of GUIs. John outlines the series of videos to teach the basics of GUI development. He encourages viewers to subscribe and check out Coding for All for a range of courses and offers a discount code. The video assumes viewers have Python installed and basic knowledge of running Python scripts. The first step in creating a GUI is importing the Tkinter module, which John demonstrates using Sublime Text editor. He guides viewers through saving the file and setting up the initial Tkinter environment.
🛠 Building a Basic GUI 'Hello World' with Tkinter
John proceeds to build a simple 'Hello World' GUI application using Tkinter. He explains that in Tkinter, all elements are considered widgets, including buttons, text fields, and frames. The first widget to create is the root widget, which represents the main window of the application. John demonstrates creating a label widget to display 'Hello World' text. He emphasizes the two-step process of defining and placing widgets in Tkinter. The 'pack' method is introduced for placing widgets in the GUI, which John uses to add the label to the window. He also discusses the importance of the event loop, which keeps the GUI responsive and running. John corrects a minor error in the code related to the 'mainloop' method and successfully runs the program, showing a basic window with the 'Hello World' label.
🔍 Exploring Further Tkinter Features and Conclusion
In the final paragraph, John wraps up the video by highlighting the simplicity of creating a GUI with Tkinter, noting that the 'Hello World' example required minimal code. He points out the basic functionality of the window, such as minimize, maximize, and close buttons, and mentions that future videos will cover more widgets and layout methods beyond 'pack'. John invites viewers to subscribe for more videos in the series and to visit Coding for All for a comprehensive learning experience. He reiterates the discount offer and thanks viewers for watching, signing off as John from Coding for All, looking forward to the next video.
Mindmap
Keywords
💡Graphical User Interfaces (GUIs)
💡Python
💡tkinter
💡Widgets
💡Root Widget
💡Label Widget
💡Event Loop
💡Pack
💡Coding Course
💡Sublime Text Editor
💡Main Loop
Highlights
Introduction to creating graphical user interfaces (GUIs) with Python using tkinter.
Tkinter is a built-in Python module that simplifies the creation of GUIs.
The video series will teach the basics of GUI development with Python.
A coupon code 'youtube' is offered for a discount on courses at codingbe.com.
Assuming viewers have Python installed and basic knowledge of running Python scripts.
Using Sublime Text as the preferred text editor for demonstration.
Importing tkinter is the first step in creating a GUI with Python.
Every element in tkinter is considered a widget, including buttons and text fields.
The root widget serves as the main window of the GUI application.
Creating a label widget to display 'Hello World' in the GUI.
Two-step process of defining and then placing widgets onto the screen.
Using the 'pack' method to place widgets in the first available spot.
Creating an event loop to handle user interactions within the GUI.
Demonstration of running the simple 'Hello World' GUI program.
The simplicity of creating a functional GUI with just a few lines of code.
Upcoming videos will cover more widgets and advanced layout options.
Encouragement to subscribe for notifications on new video releases.
Invitation to check out codingbe.com for more courses and resources.
Transcripts
what's going on guys my name is John
elder Franco to me calm and in this
video I'm going to start to teach you
how to create graphical user interfaces
with Python
all right welcome to the video my name
is John elder from Cody me calm and in
this video we're gonna start to talk
about graphical user interfaces with
Python specifically using kinter or
tkinter as its I guess called and that's
a module that comes built in with Python
that allows you to create gooeys
graphical user interfaces relatively
easy and very very quickly so we're
gonna start to look into that in this
video show you the very very basics this
is going to be a series of videos so you
can follow along and and learn all about
it so before we get started if you like
this video and want to see more be sure
to smash the like button below subscribe
to the channel and be sure to check out
Cody be calm or I have dozens of courses
with hundreds of videos to teach you to
code use coupon code youtube to get $22
off membership that's all my courses
videos and books for a one-time fee of
just $27 which is pretty insane hahaha
but check that out if you're interested
so let's get right into it
like I said python comes built in with
tkinter or kinder as some people call it
I'm probably gonna call it kinder I kind
of drop the T it's silent if I call it
tkinter
you know what I'm talking about so I'm
gonna assume you already have Python
installed on your machine now this
should work on Windows Linux or Mac I'm
on a Windows computer so just sort of
keep that in mind but I'm gonna seem you
already have Python installed if you
don't you can go find another video to
install it I'm gonna assume you already
sort of know how to use Python so you
can run a script and make it work
without me having to show you how to do
that so let's just jump right in here
here I have the sublime text editor you
can use absolutely any text editor that
you want i like sublime it's free it's
pretty cool so I've just got an unsaved
file open right now so to start we need
to import kinter and it's really easy we
just go from lower case tki nter tkinter
and then we want to import everything
alright
so right away let's go ahead and save
this file well let's first view I want
to say syntax we want this to be Python
okay so it pops up let's go ahead and
save this
and I'm gonna navigate to my si
directory you can save this anywhere you
want where every just you'll remember it
I created this GUI Gena graphical user
interface directory and this is where
I'm gonna save all this stuff so let's
just call this one hello and go ahead
and save it
okay so says hello dot PI is the PI file
Python file okay so pretty
straightforward we're just gonna import
this module tkinter import everything
now this allows us to use everything in
tkinter there's some other ways you can
import this but this is sort of the the
main way that you do it and I'm not
gonna talk about any other ways because
this is what you're gonna want to use
99.9 percent of the time so in kinter
everything is a widget there's button
widgets text widgets frame widgets
everything is a widget and the first
thing you create is sort of the route
widget it's think of it like the window
any graphical user interface program on
your computer has like a window if
you're on a Windows machine it's the
windows window if you're on a Mac or
Linux it's the same kind of you know
Boxey window thing so we need to create
that before we do anything else so we
call it the root it's the root widget
and we just set this equal to 80k and
that's it so this has to go first this
has to happen before anything else in
your program when you're working with
tkinter so just right off the bat just
make it the first thing that you always
do so for the rest this video we're
gonna create just a very very simple
window sort of a hello world window and
and run it so we want it to like say
some text right and the text will just
be whenever we want hello world or
whatever so we need to create a label
widget now throughout the course of this
series I'm gonna talk about all the
different widgets we're gonna talk about
them all in great detail and we'll get
into that in the future for this video
we're just gonna use the label widget
I'm not going to talk about it in great
depth because it's very simple I might
talk about it in more detail a little
bit later on but to create anything is
in kinter is really a two-step process
you have to define the thing and create
it and then you have to put it up on the
screen so it's two steps always so the
first step we're just gonna create a
label widget so I'm just going to call
this my label labor and then set this
equal to it's a label widget so we have
this label function right
and then we want this to go in our root
widget right and then for the text we
want it to go hello world if I could
type correctly right and that's it so
we've now created a label a label widget
now we have to put that widget into our
root widget into our root window right
so that's a two step process that I was
talking about now there's a couple of
different ways that you can put things
on the screen with tkinter and the first
one we're gonna look at is pack we're
just gonna pack it in there and when you
think of pack think of just like packing
you're just shoving it in there at the
first available spot right it's just
sort of it'll be the size that it is
it's very unsophisticated we're just
shoving it in there and that's why I'm
going to use it for this video because
we haven't learned anything else
primarily you're going to use the other
method to put stuff on the screen and
we'll talk about that later
probably in the next couple of videos
but for right now we're just going to
pack this thing in there so what we do
is we take our widget which is my label
and then dot pack that's it so here
we're creating a let's just call it a
text well it's a label called a label
widget and then here we are shoving it
onto the screen right so that's it so
now the last thing we need to do is
create an event loop and what an event
loop is when you have a graphical user
interface when you have a program
running it's always looping constantly
and that's how it figures out what's
going on so as it's looping you might
move your mouse cursor right since it's
looping it notices all the mouse is here
now it's here now it's here now it's
here because it's continually looping if
you go to click a button you know it
loops sees you moving towards that
button as its looping looping looping so
it's a constant loop so you have to
create that loop and normally when a
program ends is when that loop ends now
if you're familiar with programming of
any kind you're familiar with loops they
keep going until something happens
right and in this case they keep going
until the program is and then they end
so to do that we want to create a route
if this is our route widget and then we
want to call it main loop because this
is the main loop of the program right
and that's it so let's go ahead and save
this now this is ridiculously simple
we've just created a graphical user
interface an actual program that will
run and be visually you know cool well
it's not that cool we'll see in a second
hair but it works and it's like what is
this one two three four five lines of
code that's ridiculous that's very very
simple now granted this is a very
unsophisticated easy program but I think
you can see right away how simple it is
to use this cheek enter is no more
complicated than this right if you have
a bigger program that you want to create
sure that's going to be more lines of
code but everything is a widget and this
is how easy it is to create a widget you
just define it and then pack it in there
somehow that's it so it's not that hard
to use this tkinter which is really cool
so let's go ahead and run this and see
what we got I'm pulling up I have this
get bash terminal I downloaded this you
can just google git bash if you want to
use this when you absolutely don't you
can use any terminal that's on your
computer or any way that you already
used in the past to run Python programs
windows has a command prompt as the
power show installed you can use either
of those if you're on a Mac or Linux you
could use your regular terminal whatever
so I'm gonna change directory into C GUI
and just to make sure our hello file is
there it sure is so let's just run this
file so I type in Python and then hello
dot py
you know something happened object has
no attribute main loop what did we do
oh the L must be lowercase Joey alright
so go ahead and save this pull this back
up again run it again and here we have
it's on my other monitor so let me drag
it over and there we have it put it up
here so this is it so let's plug code
backup as well
okay so here says hello world because we
typed right here hello world and this is
the little tkinter icon it's a favicon
we can change that we'll talk about that
later you can see this program has a
minimize button that works it has a
maximize button that works and an X
button that works so
we click that it ended automatically so
we can run this again just for fun
and let's pull this over now we can
resize this and it kind of resizes
things automatically in the future video
I'm going to show you how to explicitly
resize it so when you started it it
doesn't start small like this but in
this case we use PAC and PAC says you
know pack this in just as as big as the
stuff inside is and our text widget is
only you know what is this 1012
characters so it's only that big a
widget it's only that big so the window
is only that big pretty cool so that's
it that's how easy it is and check it
out I mean this is a full on windows
window right has all the things you
would expect
now minimize maximize little X very cool
and see this X right here what you're
doing when you're closing this window is
you're disrupting this main loop this
root loop and that causes the program to
end so we'll look at all this in more
detail in the next few videos in fact in
the next video we'll start to look at
some other widgets and probably another
method besides PAC in order to arrange
things on the screen it gives you more
options PAC is good for simple things
but if you want more options if you want
more control over how things look and
exactly where you can position things
you're gonna want to use this other
method I'll start to look at that in the
next video so if you like this video be
sure to subscribe to this channel we'll
be releasing videos in this series every
couple of days or so so subscribe to the
channel so you get notified click that
little Bell thing at the bottom so that
YouTube notifies you every time I you
know put up a new video if you want to
and be sure to check out Co to be calm
we're use coupon code youtube at
checkout you get twenty two dollars off
so you get all of my courses I've got
almost 30 courses hundreds of videos
over fifty thousand students it's really
cool check it out
$27 I think you pay for all of my
courses one-time fee so if you like this
video you want to see more like it and
more videos in other programming
languages and things definitely check
that out so my name is John elder from
coding be calm and we'll see you next
time
5.0 / 5 (0 votes)