Simple Tkinter Demo

Joshua Miguel Abis
1 Sept 202418:47

Summary

TLDRIn this instructional video, the presenter guides viewers through the basics of creating a simple GUI calculator using the Tkinter library in Python. The tutorial covers importing Tkinter, setting up the main window, configuring window properties, and adding UI elements such as an entry box for numbers and buttons for digits 0-9, as well as basic operations. The focus is on the layout and design, with the functionality to be addressed in a subsequent session.

Takeaways

  • πŸ˜€ The video is a tutorial on creating a simple GUI for a calculator using Tkinter, a Python frontend library.
  • πŸ› οΈ The prefix 'TK' is used to access Tkinter functions, though it can be customized.
  • πŸ–₯️ The 'main' window is configured first, setting up the basic structure for the GUI.
  • πŸ”’ An 'entry' widget is created for displaying calculator input and output.
  • πŸŽ›οΈ The 'grid' system is used to place widgets in rows and columns, similar to a spreadsheet.
  • πŸ”² Number buttons are created using the 'button' widget and assigned to the grid with specific row and column indices.
  • πŸ”„ The 'width' and 'height' attributes are used to size the buttons properly within the grid.
  • βž•βž–βœ–οΈβž— The basic arithmetic operation buttons (addition, subtraction, multiplication, and division) are added to the grid.
  • πŸ’‘ An 'equals' button and a 'clear' button are also included, using specific symbols and text.
  • πŸ”§ The tutorial emphasizes the importance of setting the correct attributes for each widget to ensure they display correctly.
  • πŸ“ The final GUI is non-functional at this stage; functionality will be added in a subsequent tutorial.

Q & A

  • What is the purpose of the script?

    -The purpose of the script is to demonstrate the basics of creating a simple GUI for a calculator app using the Tkinter library in Python.

  • What is Tkinter and what role does it play in the script?

    -Tkinter is a frontend library for Python that allows for the creation of graphical user interfaces. In the script, it is used to build the calculator's GUI.

  • What is the prefix used to access Tkinter functions in the script?

    -The prefix used to access Tkinter functions in the script is 'TK'.

  • How is the main window variable named in the script?

    -The main window variable in the script is named 'M'.

  • What is the significance of the 'geometry' attribute in the script?

    -The 'geometry' attribute in the script is used to determine the size of the initial window of the calculator app.

  • What is an 'entry' widget in the context of the script?

    -An 'entry' widget in the script is used to create a box where the digits and numbers will be displayed when the calculator buttons are pressed.

  • How are the calculator buttons created and configured in the script?

    -The calculator buttons are created using the 'button' widget and configured with attributes like 'text' for the button label, 'width' and 'height' for sizing, and 'grid' for positioning within the window.

  • What is the role of the 'grid' system in arranging the calculator's UI elements?

    -The 'grid' system in the script is used to position and arrange the calculator's UI elements, such as buttons and the entry box, in a structured layout similar to a spreadsheet.

  • How are the buttons for numbers 1 to 9 created in the script?

    -The buttons for numbers 1 to 9 are created using the 'button' widget with a prefix 'B' followed by the number, and then assigned to specific rows and columns within the grid.

  • What additional buttons are mentioned in the script for the calculator?

    -The additional buttons mentioned in the script for the calculator include buttons for addition (+), subtraction (-), multiplication (*), division (/), equals (=), and clear (C).

  • What is the next step after creating the calculator's UI as described in the script?

    -The next step after creating the calculator's UI is to make the calculator functional, which will be covered in the next session as mentioned in the script.

Outlines

00:00

πŸ’» Introduction to Tkinter for GUI Development

The speaker introduces the basics of Tkinter, a frontend library for Python, and sets the stage for creating a simple GUI for a calculator app. The tutorial focuses on importing Tkinter, setting up the main window, and configuring its properties such as title and geometry. The prefix 'TK' is used to access Tkinter's functions, and the main window is initialized and displayed using the 'TK' prefix. The speaker suggests following along to avoid confusion and ensures the main window is activated to appear when the program runs.

05:00

πŸ”’ Setting Up the Calculator's Entry Box

The speaker proceeds to explain how to create an entry box for the calculator where numbers will be displayed. This involves using the 'entry' widget and assigning it to the main window. The entry box is then placed within the grid system of the Tkinter GUI, which requires understanding the concept of rows and columns. The speaker sets the width of the entry box to fill the window and demonstrates how to adjust the attributes of the button widgets to ensure they display correctly within the grid.

10:02

πŸŽ›οΈ Creating Number Buttons for the Calculator

The tutorial continues with the creation of number buttons for the calculator, ranging from 0 to 9. Each button is created using the 'button' widget and assigned to the main window. The speaker emphasizes the importance of placing these buttons within the grid system, explaining how to use rows and columns to position them. Attributes such as 'width', 'height', and 'text' are adjusted to ensure the buttons are displayed correctly and are visually appealing. The process is repeated for each button, incrementing the row and column indices as necessary.

15:04

βž•βž–βœ–οΈπŸ”„ Adding Operators and Functional Buttons

In the final part of the script, the speaker adds the operator buttons for addition, subtraction, multiplication, division, and the equals and clear buttons. These are created in a similar manner to the number buttons, with the speaker demonstrating how to assign them to the grid system. The speaker ensures that the buttons are properly aligned and spaced, adjusting the grid configuration as needed. The tutorial concludes with a non-functional but visually complete calculator UI, with a promise to make it functional in the next session. The speaker encourages participants to create their own calculator and reach out for help if needed.

Mindmap

Keywords

πŸ’‘Tkinter

Tkinter is a standard GUI (Graphical User Interface) library for Python. It is used to create windows, dialogs, buttons, and other graphical elements. In the video, Tkinter is introduced as the frontend library for creating a simple calculator GUI. The script demonstrates how to import Tkinter and use it to set up the main window and other UI components.

πŸ’‘GUI

GUI stands for Graphical User Interface, which refers to the visual elements that users interact with on a computer screen. The video's theme revolves around creating a GUI for a calculator, showcasing how to use Tkinter to design and arrange buttons, an entry box, and other elements to form a user-friendly interface.

πŸ’‘Entry widget

An Entry widget in Tkinter is used to display and input text. In the context of the video, the Entry widget is essential for the calculator's display where numbers and results are shown. The script details how to create and configure the Entry widget to fit within the calculator's window.

πŸ’‘Button widget

Button widgets in Tkinter are used to create clickable buttons. The video script includes instructions on creating button widgets for the calculator's number keys and operations like addition, subtraction, multiplication, division, equals, and clear. Each button is configured with a text attribute to display its corresponding number or symbol.

πŸ’‘Grid layout

The Grid layout in Tkinter is a way to organize widgets in rows and columns. The video explains how to use the grid layout to position the calculator's buttons and entry widget. It demonstrates assigning rows and columns to each widget to create a structured and visually appealing interface.

πŸ’‘Main window

The main window is the primary window of a GUI application. In the video, the script shows how to create and configure the main window using Tkinter, setting its title and geometry to establish the initial appearance of the calculator application.

πŸ’‘Prefix

In the context of the video, a prefix is used to avoid conflicts in naming when importing modules. The script suggests using 'TK' as a prefix for Tkinter functions, which is a common practice to make it clear that the variables and functions are related to the Tkinter library.

πŸ’‘Configuration

Configuration in the video refers to the setup process of the GUI elements. It includes setting attributes such as the title, size (geometry), and other properties of the main window and widgets. The script provides a step-by-step guide on configuring the calculator's UI components.

πŸ’‘Attribute

Attributes in Tkinter are properties that define the appearance and behavior of widgets. The video script discusses various attributes like 'width', 'height', and 'text' that are used to customize the calculator's buttons and entry widget to fit the desired design.

πŸ’‘Functional

Although not explicitly detailed in the script, the term 'functional' implies that the next step after creating the UI will be to make the calculator operational. The video mentions that the current setup is nonfunctional, indicating that the buttons and entry widget are not yet linked to the calculator's logic for performing calculations.

πŸ’‘Activity

The video concludes with an activity for the viewers, which is to create their own calculator using the instructions provided. This keyword highlights the educational aspect of the video, encouraging hands-on learning by applying the concepts discussed.

Highlights

Introduction to Tkinter as a frontend library for Python.

Creating a simple GUI for a calculator app with Tkinter.

Importing Tkinter and using 'TK' as a prefix for its functions.

Setting up the main window for the calculator GUI.

Configuring the window title and geometry for the initial size.

Creating an entry widget for displaying calculator digits.

Assigning the entry widget to the main window and placing it in the grid.

Adjusting the width attribute of the entry widget to fill the window width.

Creating number buttons using the Button widget in Tkinter.

Assigning buttons to the main window and placing them in the grid layout.

Understanding the rows and columns system of Tkinter's grid.

Setting up the attributes like width, height, and text for the buttons.

Adding the operator buttons for addition, subtraction, multiplication, and division.

Creating the equals and clear buttons for the calculator.

Adjusting the grid layout to properly display all calculator buttons.

Ensuring that the calculator's UI elements are aesthetically aligned.

Announcement of the next session focusing on making the calculator functional.

Invitation for participants to create their calculator and share it.

Offering assistance for any issues encountered during the coding process.

Transcripts

play00:00

so hello CL uh I am here today to show

play00:03

you the uh basics of tier so tier is

play00:08

essentially a frontend library to python

play00:13

so for today we will create a simple GUI

play00:17

for a simple calculator app with the

play00:19

numbers 0 to 9 the basic operations and

play00:24

of course the equals and clear

play00:26

buttons so for our first step we need to

play00:30

to import tick so

play00:35

first let's do that so it would look a

play00:39

little bit like

play00:42

this so This TK you can see here is the

play00:45

prefix we will use to properly um access

play00:49

the functions of our ticker Library you

play00:52

can change it to anything you like but

play00:53

for Simplicity sake we will use TK for

play00:57

this video

play01:00

so next is our um window

play01:06

configuration so this is our

play01:08

configuration for the window that will

play01:11

act or that we will use to um display

play01:14

our

play01:15

DUI so first we will need a variable it

play01:20

can be of any name uh you can name it

play01:23

root main or josua if you would like but

play01:27

for today's video we will name it M I

play01:30

suggest you follow along so you don't

play01:32

get um

play01:34

confused so after we declare or we put

play01:38

in the name of the variable we need to

play01:40

make it the main window so to do that we

play01:43

need to add or type in TK that is our

play01:46

prefix here as you can see and then

play01:49

do capital T small K and then

play02:00

so that is our

play02:03

um statement for our main window and of

play02:07

course we need to make sure we put it or

play02:11

we activate the main Lo so it actually

play02:14

shows up when we run the program so

play02:17

let's try if it

play02:18

does yeah as you can see so that's our

play02:23

basic um window configuration done for

play02:27

now let's do a slight more a few more

play02:31

Twix so let's add a title so what I'm

play02:35

doing here is I'm um changing the

play02:39

attributes of the main variable so we

play02:42

will be changing the T the title of the

play02:46

main window so for for now let's say

play02:49

simple

play02:52

calculator then the next important one

play02:54

is the main.

play02:58

geometry so this geometry is what we use

play03:02

to determine the size of the initial

play03:07

window so if you run this again

play03:14

wait run this again as you can

play03:18

see so since we haven't um created or

play03:23

added functions that would

play03:26

limit the window size you can still

play03:29

change it fre but for now we wouldn't

play03:32

worry about that since we have our uh

play03:36

basic window setup here we'll be adding

play03:40

the UI elements of the calculator itself

play03:45

so first we need a box where the digits

play03:48

of the numbers will show up when we

play03:50

press on the buttons so for that we will

play03:54

use an

play03:55

entry so E1 uh equals entry one then to

play04:00

create an entry we just use the prefix

play04:02

do

play04:12

entry so we need to assign it to our

play04:16

main window like so and then we need to

play04:20

put it in the um you can still run this

play04:24

without putting it into the grid but it

play04:26

will not show up so to put it into the

play04:29

grid grid you need to assign it properly

play04:33

so that would be e1. grid equals column

play04:38

span this column span is a um attribute

play04:42

of the grid itself um it's a bit

play04:46

Advanced so for now just follow what I'm

play04:51

doing so what this this does is it sets

play04:55

the number of columns the

play04:58

entry uh

play05:00

object or the widget will

play05:06

occupy that said we should probably

play05:10

modify the width of this so it

play05:14

properly um fills up the width of the

play05:17

window so to do that we just use the

play05:21

width attribute here and change it to

play05:24

whatever we like so let's say 50

play05:29

I think that's

play05:33

40 uh around

play05:36

45 never mind 50 was

play05:40

perfect there we so that's our digit

play05:45

box so it would look like that so for

play05:48

now it looks looks a little bit simple

play05:52

no so since that's the entry is done

play05:56

next we will have to do our

play05:58

buttons so for now let's do our number

play06:03

buttons so that would be our b0 or

play06:07

B1 B2 B3 B4 B5 B6 B7 B8 B9 and B 0 for

play06:19

our numbers of 1 to Z so to create a

play06:23

botton widget we just do this uh a

play06:26

similar thing here so that's equals DK

play06:30

that is our prefix and then our botton

play06:35

object we need to assign it to our

play06:37

window and then for now let's stop at

play06:48

that now let's assign it to

play06:52

everyone there

play06:54

go as I said it would not show up if you

play06:58

don't put it in the group

play07:00

so for now let's put all of these into

play07:03

the

play07:05

grid so since we're using the

play07:09

grid you will have to learn about the

play07:12

rows and columns system of our tier grid

play07:18

so what does does that look like so

play07:21

imagine the or imagine this as a

play07:25

spreadsheet or an Excel file no the rows

play07:29

are the horizontal lines that you can

play07:33

use so that would be equivalent to our

play07:35

programming lines here so you can see uh

play07:38

in this case we have 34 rows of code so

play07:42

column is the

play07:45

vertical um position of the

play07:49

element so just imagine of it like a

play07:53

spreadsheet similar to

play07:57

this and you will probably be

play08:02

fine again you will probably figure this

play08:05

out as uh as you go experimenting with

play08:10

ti so for now you have to follow my

play08:14

lead so let's do row equals z or Row

play08:21

one since we already have the entry

play08:24

there then column

play08:27

zero let's see if that pops up

play08:31

as you can see it

play08:32

does what's uh why does it look like

play08:35

that because we didn't properly set up

play08:38

the variable or the attributes of the

play08:41

bottom so for now let's add a

play08:47

WID say 15 is good enough too much I

play08:52

think see no I think 15

play08:57

should 15 let's add

play09:03

height say

play09:06

five and that should probably good

play09:10

enough

play09:25

so

play09:28

then see if it

play09:30

changes I I mean we still haven't

play09:33

properly assigned them so since we have

play09:36

our button now you can you can click it

play09:39

now we need to have the number of the

play09:42

bottom properly um the fure so to do

play09:46

that we will use the text

play09:49

attribute this is quite similar to the

play09:51

width and the height uh the only

play09:53

difference being the string um input

play09:57

that you should put in there so for now

play09:58

since this about one let's C in one and

play10:01

check if it shows up as you can see it

play10:03

does so we're going to have to do this

play10:07

with every single one of the

play10:11

buttons see here let's do that quickly

play10:31

that should be pretty good so with that

play10:35

um with these buttons that I mean we

play10:37

should probably be just setting up the

play10:42

G so so row column

play10:51

one as you can see it is now on the

play10:54

seven Colum

play11:01

um cut down on this a

play11:06

bit maybe 14 or

play11:14

something four it's a little bit um f

play11:17

right

play11:19

now

play11:21

check I think that's still a bit too fat

play11:24

maybe 12 is the right number of

play11:32

yeah check that should probably good

play11:35

enough probably be good

play11:37

enough so like I said this is the number

play11:41

two bottom or bottom two is in the

play11:44

second column

play11:47

here or column one our B our button one

play11:51

is in the column zero so it is the first

play11:54

one here so if you want to place another

play11:57

button on the right side of our button

play12:00

two we will have to name it or set it as

play12:05

column ex

play12:08

so sure to properly set the botton name

play12:13

so as you can

play12:15

see still can't find the

play12:21

right just check if properly this

play12:26

it ah this doesn't support CL vales

play12:32

unfortunately

play12:33

13 just about for this 13 is

play12:39

about let's have to change all of this

play12:42

to 13

play12:50

again so again you just have to copy or

play12:54

copy I mean you have to do this so on

play12:56

and so forth for each of the buttons so

play12:59

in our case that's our bottom 1 two

play13:03

z s 8

play13:12

n so for every three buttons we will

play13:15

have to

play13:17

increment a row I

play13:21

so

play13:23

something wrong with the column because

play13:26

it's number P other than number

play13:42

and12 I need to modify the

play13:44

rows R

play13:47

three should be R check if this works

play13:50

properly and as you can see it does as

play13:53

our numerical numbers and the only thing

play13:56

missing is our operators and

play14:00

the equals and clear uh

play14:05

buttons so for now we will be adding

play14:09

that so let's let's do ba for button of

play14:14

addition

play14:16

uh then BS for

play14:20

subtraction BM for multiplication BD for

play14:26

division then be for equals and b c for

play14:30

B button to clear so just copy paste

play14:39

this addition is plus of

play14:42

course minus multiplication is fosty and

play14:47

division is a slash for equals of course

play14:50

we use the equal sign and for Clear

play14:53

let's just use a Capital

play14:57

C so again this will not show up if you

play15:00

don't properly assign it in our row

play15:04

system or our grd system so let's add

play15:07

addition and subtraction

play15:09

first in the same column as number four

play15:12

or number

play15:15

zero and if run that should probably

play15:17

show as you can

play15:19

see let's do 500 so should show

play15:24

properly then you just have to add all

play15:28

of this add all of the stuff you added

play15:30

so since you have the

play15:34

M we change that to row five since it's

play15:37

technically three items per row

play15:41

no so BM for multiplication BD for

play15:45

division and of course

play15:49

equs for our equal

play15:52

sign this don't have our C so we're

play15:56

going to have to create a new r for it

play16:00

like

play16:05

so so these are

play16:07

our botton declarations just to make it

play16:13

clear and this is is there grid for grid

play16:18

grid

play16:19

declarations for the butons themselves

play16:23

so if you run that there you go as you

play16:25

can see all of the buttons are now

play16:27

perfectly there

play16:29

add a little bit

play16:32

more height so you can see

play16:34

properly try to make it just straight

play16:38

just

play16:39

stra of course you can also just

play16:45

do six so it's

play16:48

39 do

play16:51

39 on equals bottom

play16:55

actually do that will show up like this

play16:59

because we didn't um fix it up so we're

play17:03

going to have to switch c and e here see

play17:08

how it looks like still doesn't or still

play17:11

hasn't fixed

play17:13

properly

play17:22

so

play17:24

say there we go

play17:28

so that's basically or let's try 40

play17:40

actually that's good enough so that's

play17:45

how to create a simple or somewhat

play17:50

simple UI using

play17:53

theer for now we created a nonfunctional

play17:57

calculator now uh in our next um session

play18:02

we will make sure to make this

play18:07

functional so for your activity this um

play18:10

day you will have to create a calculator

play18:14

and post it to us if there are any

play18:16

problems you can um reach out to me on

play18:20

messenger or on USM itself no if you

play18:23

have problems with your VSC or problems

play18:26

with the code you can message me at

play18:30

so for now uh follow

play18:33

the code that I showed here I will also

play18:36

send this

play18:37

code to your usern s so you can use it

play18:41

as again so that's about it

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
TkinterPythonGUICalculatorCodingTutorialProgrammingUI DesignWeb Development