Create Graphical User Interfaces With Python And TKinter

Codemy.com
10 Jan 201911:56

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

00:00

💻 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.

05:01

🛠 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.

10:02

🔍 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)

Graphical User Interfaces, or GUIs, are visual representations of the interactive system that allow users to interact with electronic devices through graphical icons and visual indicators. In the video, the main theme revolves around creating GUIs with Python, demonstrating how to build a simple 'Hello World' window using the tkinter module, which is a built-in Python library for GUI development.

💡Python

Python is a high-level, interpreted programming language known for its readability and efficiency. The video script introduces the use of Python for GUI development, specifically mentioning its built-in module tkinter, which simplifies the process of creating GUIs.

💡tkinter

Tkinter, sometimes stylized as kinder, is the standard GUI toolkit for Python. It is used in the script to demonstrate how to create a basic GUI application. The term is used to refer to the module that allows the creation of graphical elements within a Python program.

💡Widgets

In the context of GUI development, widgets are the visual elements of the interface, such as buttons, text fields, and frames. The script explains that everything in tkinter is a widget, and the first widget created is the 'root widget', which represents the main window of the application.

💡Root Widget

The root widget is the top-level window in a tkinter application. It serves as the container for all other widgets. In the script, the root widget is created first and is essential for any tkinter application.

💡Label Widget

A label widget in tkinter is used to display text or images in a GUI. The script demonstrates creating a label widget to display 'Hello World' in the application window, illustrating the basic use of this widget type.

💡Event Loop

An event loop is a programming construct that waits for and dispatches events or messages in a GUI program. In the script, the creation of the main loop with the root widget is explained as the final step to start the event loop, which keeps the GUI application running and responsive to user interactions.

💡Pack

In tkinter, 'pack' is a geometry manager that automatically arranges widgets in the available space within the parent widget. The script uses 'pack' to place the label widget into the root widget, demonstrating a simple way to add widgets to the GUI.

💡Coding Course

The script mentions coding courses available on the website 'codingbe.com', which are designed to teach coding skills. The promotion of these courses is a secondary theme in the video, offering viewers a resource to learn more about programming.

💡Sublime Text Editor

Sublime Text is a popular text editor among programmers for its speed, ease of use, and extensive feature set. The script mentions it as the preferred text editor of the presenter for writing Python code, including the GUI application being demonstrated.

💡Main Loop

The main loop, as mentioned in the script, refers to the event loop of the tkinter application. It is named 'mainloop' and is responsible for running the application, waiting for events, and updating the GUI accordingly. The script shows that the main loop is started with the root widget's 'mainloop()' method.

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

play00:00

what's going on guys my name is John

play00:01

elder Franco to me calm and in this

play00:03

video I'm going to start to teach you

play00:05

how to create graphical user interfaces

play00:06

with Python

play00:15

all right welcome to the video my name

play00:17

is John elder from Cody me calm and in

play00:19

this video we're gonna start to talk

play00:21

about graphical user interfaces with

play00:23

Python specifically using kinter or

play00:25

tkinter as its I guess called and that's

play00:29

a module that comes built in with Python

play00:31

that allows you to create gooeys

play00:32

graphical user interfaces relatively

play00:35

easy and very very quickly so we're

play00:37

gonna start to look into that in this

play00:39

video show you the very very basics this

play00:41

is going to be a series of videos so you

play00:43

can follow along and and learn all about

play00:46

it so before we get started if you like

play00:48

this video and want to see more be sure

play00:50

to smash the like button below subscribe

play00:52

to the channel and be sure to check out

play00:53

Cody be calm or I have dozens of courses

play00:55

with hundreds of videos to teach you to

play00:57

code use coupon code youtube to get $22

play01:00

off membership that's all my courses

play01:02

videos and books for a one-time fee of

play01:04

just $27 which is pretty insane hahaha

play01:09

but check that out if you're interested

play01:10

so let's get right into it

play01:12

like I said python comes built in with

play01:15

tkinter or kinder as some people call it

play01:18

I'm probably gonna call it kinder I kind

play01:20

of drop the T it's silent if I call it

play01:22

tkinter

play01:23

you know what I'm talking about so I'm

play01:25

gonna assume you already have Python

play01:26

installed on your machine now this

play01:28

should work on Windows Linux or Mac I'm

play01:30

on a Windows computer so just sort of

play01:32

keep that in mind but I'm gonna seem you

play01:35

already have Python installed if you

play01:36

don't you can go find another video to

play01:38

install it I'm gonna assume you already

play01:39

sort of know how to use Python so you

play01:41

can run a script and make it work

play01:44

without me having to show you how to do

play01:46

that so let's just jump right in here

play01:48

here I have the sublime text editor you

play01:50

can use absolutely any text editor that

play01:52

you want i like sublime it's free it's

play01:55

pretty cool so I've just got an unsaved

play01:58

file open right now so to start we need

play02:00

to import kinter and it's really easy we

play02:03

just go from lower case tki nter tkinter

play02:08

and then we want to import everything

play02:11

alright

play02:12

so right away let's go ahead and save

play02:14

this file well let's first view I want

play02:18

to say syntax we want this to be Python

play02:20

okay so it pops up let's go ahead and

play02:22

save this

play02:24

and I'm gonna navigate to my si

play02:28

directory you can save this anywhere you

play02:30

want where every just you'll remember it

play02:32

I created this GUI Gena graphical user

play02:35

interface directory and this is where

play02:37

I'm gonna save all this stuff so let's

play02:38

just call this one hello and go ahead

play02:40

and save it

play02:42

okay so says hello dot PI is the PI file

play02:46

Python file okay so pretty

play02:49

straightforward we're just gonna import

play02:50

this module tkinter import everything

play02:52

now this allows us to use everything in

play02:54

tkinter there's some other ways you can

play02:56

import this but this is sort of the the

play02:58

main way that you do it and I'm not

play03:00

gonna talk about any other ways because

play03:01

this is what you're gonna want to use

play03:03

99.9 percent of the time so in kinter

play03:06

everything is a widget there's button

play03:08

widgets text widgets frame widgets

play03:11

everything is a widget and the first

play03:14

thing you create is sort of the route

play03:16

widget it's think of it like the window

play03:18

any graphical user interface program on

play03:22

your computer has like a window if

play03:23

you're on a Windows machine it's the

play03:25

windows window if you're on a Mac or

play03:27

Linux it's the same kind of you know

play03:28

Boxey window thing so we need to create

play03:30

that before we do anything else so we

play03:32

call it the root it's the root widget

play03:34

and we just set this equal to 80k and

play03:39

that's it so this has to go first this

play03:41

has to happen before anything else in

play03:43

your program when you're working with

play03:44

tkinter so just right off the bat just

play03:47

make it the first thing that you always

play03:48

do so for the rest this video we're

play03:50

gonna create just a very very simple

play03:52

window sort of a hello world window and

play03:55

and run it so we want it to like say

play03:59

some text right and the text will just

play04:02

be whenever we want hello world or

play04:03

whatever so we need to create a label

play04:05

widget now throughout the course of this

play04:08

series I'm gonna talk about all the

play04:10

different widgets we're gonna talk about

play04:11

them all in great detail and we'll get

play04:13

into that in the future for this video

play04:15

we're just gonna use the label widget

play04:16

I'm not going to talk about it in great

play04:18

depth because it's very simple I might

play04:20

talk about it in more detail a little

play04:21

bit later on but to create anything is

play04:24

in kinter is really a two-step process

play04:26

you have to define the thing and create

play04:29

it and then you have to put it up on the

play04:31

screen so it's two steps always so the

play04:34

first step we're just gonna create a

play04:35

label widget so I'm just going to call

play04:37

this my label labor and then set this

play04:42

equal to it's a label widget so we have

play04:45

this label function right

play04:48

and then we want this to go in our root

play04:51

widget right and then for the text we

play04:55

want it to go hello world if I could

play04:58

type correctly right and that's it so

play05:00

we've now created a label a label widget

play05:04

now we have to put that widget into our

play05:07

root widget into our root window right

play05:09

so that's a two step process that I was

play05:11

talking about now there's a couple of

play05:12

different ways that you can put things

play05:14

on the screen with tkinter and the first

play05:16

one we're gonna look at is pack we're

play05:18

just gonna pack it in there and when you

play05:20

think of pack think of just like packing

play05:22

you're just shoving it in there at the

play05:25

first available spot right it's just

play05:27

sort of it'll be the size that it is

play05:29

it's very unsophisticated we're just

play05:32

shoving it in there and that's why I'm

play05:34

going to use it for this video because

play05:35

we haven't learned anything else

play05:36

primarily you're going to use the other

play05:38

method to put stuff on the screen and

play05:40

we'll talk about that later

play05:41

probably in the next couple of videos

play05:43

but for right now we're just going to

play05:44

pack this thing in there so what we do

play05:46

is we take our widget which is my label

play05:49

and then dot pack that's it so here

play05:56

we're creating a let's just call it a

play06:01

text well it's a label called a label

play06:03

widget and then here we are shoving it

play06:10

onto the screen right so that's it so

play06:16

now the last thing we need to do is

play06:18

create an event loop and what an event

play06:22

loop is when you have a graphical user

play06:24

interface when you have a program

play06:26

running it's always looping constantly

play06:28

and that's how it figures out what's

play06:31

going on so as it's looping you might

play06:33

move your mouse cursor right since it's

play06:37

looping it notices all the mouse is here

play06:39

now it's here now it's here now it's

play06:41

here because it's continually looping if

play06:43

you go to click a button you know it

play06:45

loops sees you moving towards that

play06:47

button as its looping looping looping so

play06:49

it's a constant loop so you have to

play06:51

create that loop and normally when a

play06:54

program ends is when that loop ends now

play06:57

if you're familiar with programming of

play06:58

any kind you're familiar with loops they

play06:59

keep going until something happens

play07:01

right and in this case they keep going

play07:03

until the program is and then they end

play07:05

so to do that we want to create a route

play07:07

if this is our route widget and then we

play07:10

want to call it main loop because this

play07:13

is the main loop of the program right

play07:15

and that's it so let's go ahead and save

play07:17

this now this is ridiculously simple

play07:19

we've just created a graphical user

play07:21

interface an actual program that will

play07:25

run and be visually you know cool well

play07:28

it's not that cool we'll see in a second

play07:30

hair but it works and it's like what is

play07:32

this one two three four five lines of

play07:35

code that's ridiculous that's very very

play07:37

simple now granted this is a very

play07:39

unsophisticated easy program but I think

play07:41

you can see right away how simple it is

play07:43

to use this cheek enter is no more

play07:46

complicated than this right if you have

play07:48

a bigger program that you want to create

play07:50

sure that's going to be more lines of

play07:51

code but everything is a widget and this

play07:54

is how easy it is to create a widget you

play07:56

just define it and then pack it in there

play07:58

somehow that's it so it's not that hard

play08:02

to use this tkinter which is really cool

play08:04

so let's go ahead and run this and see

play08:05

what we got I'm pulling up I have this

play08:07

get bash terminal I downloaded this you

play08:09

can just google git bash if you want to

play08:11

use this when you absolutely don't you

play08:13

can use any terminal that's on your

play08:15

computer or any way that you already

play08:17

used in the past to run Python programs

play08:19

windows has a command prompt as the

play08:21

power show installed you can use either

play08:23

of those if you're on a Mac or Linux you

play08:25

could use your regular terminal whatever

play08:27

so I'm gonna change directory into C GUI

play08:30

and just to make sure our hello file is

play08:35

there it sure is so let's just run this

play08:38

file so I type in Python and then hello

play08:41

dot py

play08:45

you know something happened object has

play08:48

no attribute main loop what did we do

play08:51

oh the L must be lowercase Joey alright

play08:59

so go ahead and save this pull this back

play09:00

up again run it again and here we have

play09:04

it's on my other monitor so let me drag

play09:08

it over and there we have it put it up

play09:11

here so this is it so let's plug code

play09:15

backup as well

play09:17

okay so here says hello world because we

play09:21

typed right here hello world and this is

play09:24

the little tkinter icon it's a favicon

play09:26

we can change that we'll talk about that

play09:28

later you can see this program has a

play09:30

minimize button that works it has a

play09:34

maximize button that works and an X

play09:39

button that works so

play09:41

we click that it ended automatically so

play09:43

we can run this again just for fun

play09:47

and let's pull this over now we can

play09:49

resize this and it kind of resizes

play09:52

things automatically in the future video

play09:55

I'm going to show you how to explicitly

play09:57

resize it so when you started it it

play10:00

doesn't start small like this but in

play10:02

this case we use PAC and PAC says you

play10:05

know pack this in just as as big as the

play10:09

stuff inside is and our text widget is

play10:12

only you know what is this 1012

play10:14

characters so it's only that big a

play10:16

widget it's only that big so the window

play10:18

is only that big pretty cool so that's

play10:21

it that's how easy it is and check it

play10:24

out I mean this is a full on windows

play10:26

window right has all the things you

play10:28

would expect

play10:29

now minimize maximize little X very cool

play10:33

and see this X right here what you're

play10:36

doing when you're closing this window is

play10:38

you're disrupting this main loop this

play10:40

root loop and that causes the program to

play10:44

end so we'll look at all this in more

play10:46

detail in the next few videos in fact in

play10:48

the next video we'll start to look at

play10:50

some other widgets and probably another

play10:53

method besides PAC in order to arrange

play10:55

things on the screen it gives you more

play10:57

options PAC is good for simple things

play10:59

but if you want more options if you want

play11:01

more control over how things look and

play11:04

exactly where you can position things

play11:06

you're gonna want to use this other

play11:07

method I'll start to look at that in the

play11:09

next video so if you like this video be

play11:11

sure to subscribe to this channel we'll

play11:12

be releasing videos in this series every

play11:16

couple of days or so so subscribe to the

play11:18

channel so you get notified click that

play11:20

little Bell thing at the bottom so that

play11:22

YouTube notifies you every time I you

play11:25

know put up a new video if you want to

play11:26

and be sure to check out Co to be calm

play11:28

we're use coupon code youtube at

play11:30

checkout you get twenty two dollars off

play11:32

so you get all of my courses I've got

play11:34

almost 30 courses hundreds of videos

play11:36

over fifty thousand students it's really

play11:40

cool check it out

play11:41

$27 I think you pay for all of my

play11:43

courses one-time fee so if you like this

play11:46

video you want to see more like it and

play11:47

more videos in other programming

play11:49

languages and things definitely check

play11:51

that out so my name is John elder from

play11:53

coding be calm and we'll see you next

play11:54

time

Rate This

5.0 / 5 (0 votes)

Related Tags
PythonTkinterGUICodingTutorialBeginnerProgrammingWidgetsEvent LoopCoding Tutorial