Tkinter Beginner Course - Python GUI Development

NeuralNine
29 Sept 202138:59

Summary

TLDRهذا النص هو ملخص لفيديو يشرح أساسيات مكتبة Tkinter في بيثون، التي تستخدم لبناء واجهات المستخدم الرسومية. يشجع المتحدث على استيراد المكتبة不需 تثبيت، وتشرح كيفية إنشاء نافذة بسيطة وإضافة عناصر واجهة المستخدم مثل التسميات والمربعات النصية والزرات. كما يظهر كيفية استخدام التخطيطات 'pack' و'grid' لترتيب العناصر داخل النافذة، ويتضمن توضيح على كيفية إضافة وظائف مثل الاستجابة لنقرات الزر والإدخالات من المستخدمين. يختتم المتحدث بعرض كيفية إنشاء قوائم القوائم والقوائم المتعددة والخيارات المتاحة في Tkinter.

Takeaways

  • 😀 توفر مكتبة Tkinter بيئة بسيطة لبناء واجهات مستخدم رسومية في Python.
  • 🛠️ Tkinter هي جزء من المجموعة الأساسية من Python ولا يتطلب تثبيت إضافي.
  • 🏗️ يمكن إنشاء نافذة بسيطة باستخدام Tkinter بطريقة بسيطة مع تعيين اسم للنافذة وتشغيل حلقة الرئيسية.
  • 🔑 يمكن تعيين خصائص النافذة مثل عنوانها وأبعادها باستخدام دالة geometry و property.
  • 📦 يمكن إضافة عناصر واجهة المستخدم مثل التسميات (labels) والمربعات النصية (text boxes) والزرات (buttons) إلى النافذة.
  • 🎨 يمكن تخصيص عناصر واجهة المستخدم بخصائص مثل النص والخط والتعبئة (padding).
  • 🔲 يمكن استخدام التخطيطات pack و grid لترتيب عناصر واجهة المستخدم في النافذة.
  • 🔄 يمكن استخدام دالة grid لإنشاء صفوف وصفوف من الزرات بطريقة تلقائية.
  • 🛑 يمكن ربط أحداث مع دالات لإضافة وظائف مثل إظهار مربعات الرسالة أو طباعة في الكونسول عند النقر على زر.
  • 🔄 يمكن استخدام دالة bind لربط أحداث مع دالات معينة مثل الضغط على مفتاح معين في النص البرمجي.
  • 📁 يمكن إنشاء شريط القوائم في الأعلى مع خيارات مختلفة مثل 'ملف' و'إجراء' وربطها بوظائف معينة.

Q & A

  • ما هو تك إنتر (tkinter) ولماذا يُستخدم؟

    -تк إنتر (tkinter) هي مكتبة تستخدم لبناء واجهات المستخدم الرسومية في بيثون. هي الطريقة الافتراضية لبناء واجهات مستخدم رسميا في بيثون وتتضمن مجموعة واسعة من العناصر الرسومية التي يمكن استخدامها لإنشاء واجهات مستخدم مبتكرة وفعالة.

  • لماذا يُنصح بالعدم استيراد كل العناصر من تك إنتر في وقت واحد؟

    -يستحسن الممارسات البرمجية عدم استيراد كل العناصر من تك إنتر في وقت واحد لأن ذلك يمكن أن يؤدي إلى استيراد أكثر مما تحتاجه وينشئ تناقضًا أو مشاكلًا في الكود.

  • كيف يمكنني إنشاء نافذة بسيطة باستخدام تك إنتر؟

    -يمكنك إنشاء نافذة بسيطة باستخدام تك إنتر من خلال استيراده، ثم إنشاء متغير مثل root أو window وتعيين نوع النافذة إلى tk.Tk()، وتشغيل حلقة التشغيل الرئيسية root.mainloop().

  • ما هي الخصائص الأساسية التي يمكن تعيينها لنافذة تك إنتر؟

    -يمكنك تعيين خصائص مثل عنوان النافذة (title) وأبعادها (geometry)، التي تحدد الطول والعرض، لنافذة تك إنتر.

  • ما الفرق بين التخطيطات pack و grid في تك إنتر؟

    --Pack هي التخطيط الافتراضي في تك إنتر التي ت允許 العناصر الرسومية (widgets) الترتيب في الأعلى والأسفل، بينما grid ت允許 الترتيب العناصر في صفوف وأعمدة، مما يسمح بتصميمات أكثر ترتيبًا وتنظيمًا.

  • كيف يمكنني إضافة عنصر نصي (label) إلى نافذة تك إنتر؟

    -يمكنك إضافة عنصر نصي باستخدام تك إنتر من خلال إنشاء كائن label باستخدام tk.Label مع متغير parent يشير إلى النافذة الرئيسية، وتعيين الخصائص المطلوبة مثل النص والخط، ثم استخدام pack() أو grid() لوضع العلامة في النافذة.

  • ما هي الوظائف الأساسية للعناصر الرسومية (widgets) في تك إنتر؟

    -العناصر الرسومية في تك إنتر تشمل عناصر مثل التسميات (labels)، مربعات النص (text boxes)، خانات الإدخال (entries)، الأزرار (buttons)، والمربعات الاختيارية (check buttons) التي توفر واجهة مستخدم تفاعلية للمستخدم.

  • كيف يمكنني إضافة وظيفة لزر في تك إنتر؟

    -يمكنك إضافة وظيفة لزر في تك إنتر من خلال تعريف الدالة التي تريد تنفيذها عند النقر على الزر، ثم تمرير الدالة كمعلمة لدالة pack() أو grid() في كائن الزر.

  • ما هي الطريقة المثلى لتنظيم واجهة المستخدم في تك إنتر؟

    -الطريقة المثلى لتنظيم واجهة المستخدم في تك إنتر هي استخدام فئات (classes) لجمع واجهة المستخدم والوظائف البرمجية معًا، مما يوفر ترتيبًا أفضل وسهولة الصيانة والتوسع.

  • كيف يمكنني إنشاء قائمة بزر الإغلاق في نافذة تك إنتر؟

    -يمكنك إنشاء قائمة بزر الإغلاق في نافذة تك إنتر من خلال إنشاء قائمة (menu) وإضافة خيارات (commands) مثل 'Close'، ثم ربط هذا الزر بدالتين تحدد ما يحدث عند النقر عليه، مثل إغلاق النافذة أو إغلاق التطبيق.

  • كيف يمكنني إضافة دالة لضغط مفتاح في النافذة؟

    -يمكنك إضافة دالة لضغط مفتاح في النافذة من خلال استخدام الدالة bind() على عنصر واجهة المستخدم، مثل مربع النص، وربط نوع الحدث مع الدالة التي تريد تنفيذها عند حدوث الحدث.

  • كيف يمكنني إضافة شريط القوائم في الأعلى لنافذة تك إنتر؟

    -يمكنك إضافة شريط القوائم في الأعلى لنافذة تك إنتر من خلال إنشاء متغير مثل menubar و将它设为主窗口 (root)的一部分، ثم إضافة القوائم الفرعية (submenus) مثل 'File' أو 'Action' وربطها بوظائف معينة.

Outlines

00:00

😀 توفر Tkinter للمستخدم واجهة رسومية بسيطة

تدور النص حول كيفية استخدام Tkinter لبناء واجهات مستخدم رسومية بسيطة في Python. يبدأ الشرح بإنشاء نافذة أساسية وإعداد عنوان وأبعاد للنافذة. ثم يدخل إلى موضوع العناصر الرسومية الفردية مثل اللافتة والمربع النصي والزر، ويشرح كيفية إضافة هذه العناصر إلى النافذة واستخدام التخطيطات 'pack' و'grid' لترتيبها.

05:01

📐 تركيب العناصر الرسومية باستخدام التخطيطات 'pack' و'grid'

يشرح النص كيفية استخدام التخطيطات 'pack' و'grid' في Tkinter لترتيب العناصر الرسومية. يُظهر مثالًا على كيفية إضافة اللافتة والمربع النصي والإدخالات والزر وكيفية ترتيب الزر في صفوف باستخدام التخطيط 'grid'. ويتضمن أيضًا كيف يمكن إضافة المسافة البادئة حول العناصر لتحسين التصميم.

10:02

🖼️ إنشاء واجهة مستخدم متقدمة باستخدام التخطيط 'grid'

يتضمن النص توضيحًا على استخدام التخطيط 'grid' لإنشاء واجهة مستخدم أكثر تعقيدًا مثل محاسبة. يُظهر كيفية إنشاء إطار لمجموعة من الأزرار وترتيبها في صفوف باستخدام التخطيط 'grid'، وكيف يمكن للأزرار الامتداد لملء الصفوف. ويشرح النص أيضًا كيفية استخدام الخاصية 'sticky' لضبط ترتيب الأزرار.

15:05

🛠️ إضافة وظائف وأحداث لعناصر Tkinter

يناقش النص كيف يمكن إضافة وظائف وأحداث لعناصر Tkinter مثل النقر على زر أو التحقق من المربع. ويشرح كيفية استخدام الفئة لتنظيم واجهة المستخدم والوظائف، ويوفر مثالًا على كيفية إنشاء تطبيق بسيط يتضمن اللافتة والمربع النصي والزر والمربع الاختياري، وكيفية إرسال رسالة من المربع النصي بناءً على الاختيار المحدد.

20:05

🔑 ربط أحداث الضغط على المفتاح مع وظائف Tkinter

يشرح النص كيف يمكن ربط أحداث الضغط على المفتاح مثل 'Ctrl+Enter' بوظيفة معينة في Tkinter. ويوضح كيفية استخدام الربط 'bind' مع الأحداث الفعلية لتنفيذ وظائف عند حدوثها. ويتضمن مثالًا على كيفية تنفيذ دالة عند الضغط على 'Ctrl+Enter'.

25:07

❌ التعامل مع أحداث الإغلاق في Tkinter

يناقش النص كيفية التعامل مع حدث الإغلاق للنافذة في Tkinter، مثل طلب التأكيد من المستخدم قبل الخروج. ويشرح كيفية استخدام خاصية 'protocol' لتعيين دالة ت执行 عندما يضغط المستخدم على زر الإغلاق في النافذة.

30:09

🍽️ إنشاء شريط القوائم في Tkinter

يشرح النص كيفية إنشاء شريط القوائم في Tkinter وإضافة القوائم الفرعية والوظائف المتاحة في كل قائمة. ويتضمن مثالًا على إنشاء قائمة 'File' مع خيارات الإغلاق والقائمة 'Action' مع خيار إظهار الرسالة. ويوضح كيفية إضافة عناصر القائمة وربطها بوظائف معينة.

35:13

🧹 إزالة المحتوى من المربع النصي في Tkinter

في النص الأخير، يُظهر الشرح كيفية إنشاء زر لإزالة المحتوى من المربع النصي. ويشرح كيفية استخدام الدالة 'delete' لحذف النص من المربع النصي بناءً على المؤشرات البدائية والنهائية.

Mindmap

Keywords

💡tkinter

تشير 'tkinter' إلى مكتبة Python الرسمية المستخدمة في إنشاء واجهات المستخدم الرسومية. هي جزء من الحزمة الأساسية للغة Python ولا تحتاج إلى تثبيتها. في النص، يُستخدم 'tkinter' لإنشاء أUNS门窗، واجهات تفاعلية بسيطة وعناصر واجهة المستخدم المختلفة.

💡نافذة

في سياق البرمجة، 'نافذة' هي الوحدة الأساسية التي تتكون منها واجهة المستخدم. في النص، يُوضح كيفية إنشاء نافذة بسيطة باستخدام 'tkinter' وتعيين خصائصها مثل عنوان النافذة وأبعادها.

💡تخطيط

يتضمن 'تخطيط' في 'tkinter' ال安排 المكان لعناصر واجهة المستخدم داخل النافذة. يُستخدم النص 'pack' و 'grid' كأمثلة على التخطيطات التي تسمح بترتيب عناصر واجهة المستخدم بطريقة منظمة.

💡عناصر واجهة المستخدم

تشير 'عناصر واجهة المستخدم' إلى الوحدات البسيطة مثل الزر والمربع النصي والعلامة التي تشكل جزءًا من واجهة المستخدم. في النص، يُستخدم كمثال الزر والمربع النصي والعلامة.

💡الرابط

الرابط هو الوظيفة التي تربط بين العناصر في واجهة المستخدم ووظائف Python التي تحدد التفاعلات التي تحدث عندما يحدث الحدث المحدد. في النص، يُستخدم الرابط لربط النقر على زر بعرض مربع الرسالة.

💡الفئة

في Python، يمكن استخدام الفئات لتنظيم الكود وإنشاء واجهات المستخدم متعددة الوظائف. في النص، يُستخدم الفئة 'my_gui' لجمع واجهة المستخدم ووظائفها في كائن واحد.

💡الرابطة

تشير 'الرابطة' إلى ربط حدث مع دالة معينة في 'tkinter'. يُستخدم في النص لربط الضغط على الزر أو الضغط على المفتاح معين مع الدالة 'show_message'.

💡النافذة النهائية

تشير 'النافذة النهائية' إلى النافذة التي تظهر للمستخدم في النهاية. في النص، يُستخدم 'protocol' لتحديد الإجراء الذي يجب اتخاذه عندما يضغط المستخدم على زر الإغلاق في النافذة.

💡القائمة

تشير 'القائمة' إلى عنصر واجهة المستخدم الذي يتضمن مجموعة من الخيارات المنسقة. في النص، يُستخدم 'القائمة' لإضافة خيارات مثل 'file' و 'action' لوحة القوائم العلوية.

💡الزر

الزر هو عنصر واجهة المستخدم يستخدم عادةً لتنفيذ إجراءات أو دالات. في النص، يُستخدم الزر لتنفيذ دالة 'show_message' أو 'clear' النص في المربع.

Highlights

Introduction to Tkinter as a beginner course for building graphical user interfaces in Python.

Tkinter is part of the core Python stack and does not require installation.

Importing Tkinter and using it as an alias to avoid ambiguity.

Creating a basic window in Tkinter with the 'mainloop' method.

Setting window properties such as title and geometry.

Explanation of widgets in Tkinter and their functionalities.

Using layouts like 'pack' and 'grid' to organize widgets in a GUI.

Creating a label widget with custom text and font settings.

Adding a text box widget to the GUI for multi-line text input.

Difference between a text box and an entry widget in Tkinter.

Adding padding to widgets for better spacing and aesthetics.

Creating a button widget and assigning functionality to it.

Using the 'grid' layout for arranging buttons in a calculator-like grid.

Explanation of the 'place' function for manual widget placement.

Organizing the GUI within a class for better structure and functionality.

Adding interactivity with button clicks and event handling.

Creating a checkbox widget and linking it to conditional functionality.

Using keyboard shortcuts to trigger actions within the GUI.

Handling the window close event with a custom function.

Adding a menu bar to the GUI with file and action menus.

Creating a clear button to delete the content of the text box.

Final demonstration of the complete Tkinter GUI with all functionalities.

Transcripts

play00:04

[Music]

play00:09

what is going on guys welcome back this

play00:10

video is going to be a beginner course

play00:12

on tk inter we have used tk inter a

play00:15

couple of times on this channel already

play00:17

to build graphical user interface

play00:18

applications but i never published a

play00:21

video tutorial or a video course on the

play00:23

very basics of tk inter so what is a

play00:25

window how do we set some basic stuff up

play00:28

uh what are layouts what are the

play00:30

individual widgets that we can use and

play00:31

so on what are their functionality how

play00:34

do i do certain things certain basic

play00:36

things in tk inter uh i have used tk

play00:39

enter and i have explained the process

play00:41

so whenever i created something and

play00:43

typed some parameters i explained what

play00:45

they did but i never published a full

play00:47

course on the basics of tk enter which

play00:49

is what i want to do today so let's

play00:51

start with the fact that if you want to

play00:53

use tk intro you need to import it but

play00:54

you don't have to install it since it's

play00:56

part of the core python stack it's a

play00:58

library that is used to build graphical

play00:59

user interfaces in python it's the

play01:02

default way to build graphical user

play01:04

interfaces with python of course you can

play01:06

also go with pi qt5 for example but that

play01:08

is something that you need to install

play01:10

specifically tk enter is part of the

play01:12

core python stack in order to use it we

play01:14

just need to say import

play01:17

tk enter and usually

play01:19

i like to use tk as an alias so import

play01:22

tk inter stk what some people like to do

play01:26

in tutorials and i have done this myself

play01:28

as well is to say from tk inter

play01:31

import everything now this is not

play01:33

considered to be best practice because

play01:35

you import more than you need and it can

play01:38

lead to some ambiguity and problems so

play01:41

if you can avoid it

play01:42

just don't do it like that just import

play01:45

tk inter sdk or just import tk enter and

play01:47

then always when you want to use

play01:49

something for example a button you don't

play01:50

say button but you say tk dot button for

play01:53

example

play01:54

now in order to create a window that's

play01:56

quite simple we just need to say some

play01:58

name for the for the window for example

play02:00

root or window i'm going to call it root

play02:04

and then you say

play02:05

tk.tk but with a capital t and then

play02:09

uh you call the constructor and then all

play02:12

we do is we say root dot

play02:14

main loop

play02:15

not like that

play02:16

root dot main loop like this

play02:20

and by running the script here

play02:25

we will see that we already have an

play02:26

application so this is a very basic way

play02:28

to just create a

play02:30

simple graphical user interface window

play02:32

we don't have anything in here we don't

play02:33

have any uh dimensions set up we don't

play02:36

have any title or anything like that but

play02:38

this is the basic way to create the

play02:40

window now

play02:41

this window has certain properties we

play02:43

can set a title we can set the geometry

play02:46

and those two things are actually the

play02:47

most important for the beginning so

play02:48

we're going to say root dot and in order

play02:51

to set the geometry we don't use a set

play02:53

geometry function we just use the

play02:55

geometry function and

play02:58

we pass to that function a string

play03:00

with the dimensions so first the x value

play03:03

then the y value in this case we could

play03:05

say something like 800 x

play03:08

500 so it would create

play03:10

a window with a width of 800 pixels and

play03:14

a height of 500 pixels and of course you

play03:17

can play around with that however you

play03:18

want we can also swap that uh let's do

play03:20

something like 500 times 500 so we have

play03:23

a square more or less uh and then we can

play03:26

also set the title of the window by just

play03:28

saying root dot

play03:31

dot title

play03:32

uh my first gui for example

play03:36

now if we do that

play03:38

there you go you can see that we have a

play03:40

title now

play03:41

so those are the basics we can set the

play03:43

geometry and the title uh now i want to

play03:45

get right into the widgets because

play03:47

that's what what uh tk intro is all

play03:49

about we want to have something inside

play03:51

of that window and if you want to add

play03:52

something to that window you create the

play03:54

object you pass this window as the

play03:56

parent object and then you use a layout

play03:59

to get this object or this widget into

play04:01

the graphical user interface usually you

play04:04

use something like pack or grid there is

play04:06

also place where you can just choose the

play04:08

coordinates

play04:10

but usually you go with pack or with

play04:12

grid which are the two layouts and we're

play04:13

going to look at all of them today so uh

play04:16

what we're going to do first now let's

play04:17

go with a simple label a label is just a

play04:19

text so let's say label equals

play04:22

and then

play04:24

tk.label with a capital l

play04:26

and what we pass first here is the

play04:28

parent object so the parent object of

play04:30

the label uh the parent the parent

play04:32

window the master is going to be root

play04:35

itself so label is going to be part of

play04:37

root and then we can set some named

play04:40

parameters the text of the label is

play04:42

going to be hello world for example and

play04:44

then we can also pass some

play04:46

design stuff like the font if we want to

play04:48

define a font what we do is we say font

play04:50

equals and then we pass a tuple and the

play04:53

first

play04:54

element of that tuple is going to be the

play04:56

font family so arial for example

play05:00

and the second one is going to be the

play05:01

font size for example 18 points i think

play05:04

it's points not pixels

play05:06

i hope that's true

play05:08

now if we want to get that label into

play05:10

the root all we need to do is we need to

play05:12

specify

play05:13

um a layout that we use as you can see

play05:16

we can use pack we can use grid and i

play05:18

think somewhere down here should also be

play05:21

uh place there you go we're going to go

play05:23

with pack for now we're going to build

play05:24

everything with pack for now and what we

play05:27

can add here as well is a padding but

play05:29

first of all let's see what it looks

play05:30

like without a padding

play05:32

uh not debug sorry

play05:34

let me run this

play05:36

again there you go we have a simple

play05:40

label as you can see it's quite uh

play05:42

basically

play05:43

at the topmost position with a small

play05:46

uh with a small padding you could say

play05:48

with a small distance with a small

play05:50

spacing uh what we can pass here is pad

play05:52

x equals 20 for example and pad y equals

play05:56

20. obviously x is for the x dimension

play05:58

for the horizontal padding and y's for

play06:00

the vertical

play06:02

padding there you go you can see there's

play06:04

a little bit more distance now so we can

play06:06

do that with other uh elements as well

play06:09

let's go with a simple text box

play06:11

text box is going to be tk.text with a

play06:14

capital t root is going to be the master

play06:16

object again

play06:17

then uh text you can also set a text if

play06:20

you want to but usually you want the

play06:22

user to input the text

play06:24

what you can set for a text box is you

play06:26

can set the height so you can say or

play06:29

actually let's don't set the height and

play06:30

see what it looks like without setting

play06:32

the height but we're going to add a font

play06:34

here again ariel but this time a little

play06:37

bit smaller so let's say it's 16

play06:40

points and then text box dot pack

play06:44

and let's just do no padding for

play06:46

now and you can see that it uh is the

play06:50

whole it it scratches across the whole

play06:52

graphical user interface and it has no

play06:54

distance here uh on the horizontal

play06:57

because of course

play06:58

uh

play07:00

we don't have any padding here so now if

play07:02

i scale the window

play07:04

you can see it's quite large

play07:06

uh so if i want to set the height to

play07:08

something specific i can also go ahead

play07:11

and say okay i want the height to be

play07:12

three

play07:13

and then it's going to be way smaller as

play07:16

you can see here it's going to be three

play07:18

lines

play07:19

so i can write something here i can

play07:21

write something here and here and now of

play07:23

course i can also go down but

play07:26

then

play07:27

i have to scroll

play07:29

all right so

play07:31

this is a basic text box we can also use

play07:33

an entry if we want to an entry is

play07:34

basically just a text box with height 1

play07:39

without multi-line features and all that

play07:41

so if you want to have a text box for a

play07:42

password maybe an entry is more useful

play07:45

than a text so let me maybe show you

play07:47

that

play07:48

my entry is going to be tk dot entry

play07:51

like that part of root

play07:53

and then just my entry

play07:56

dot

play07:57

pack

play08:00

and you can see that this here is an

play08:02

entry because it's one line i cannot

play08:04

press enter i mean i can press enter but

play08:06

nothing happens it doesn't go into

play08:07

multi-line whereas here even if the

play08:09

height was one i can still scroll down

play08:12

uh this is the difference we're going to

play08:13

use a text box here though

play08:16

um and of course we can also add a

play08:18

padding now maybe we don't want to have

play08:20

a vertical padding because we already

play08:21

have one from the label and it's fine

play08:23

but maybe you want to have a horizontal

play08:25

one so pedex is going to be 20

play08:28

and now you can see that we have some

play08:30

distance here now 20 seems to be a

play08:31

little bit too much so let's go with 10

play08:34

this should look fine there you go

play08:37

now we can also add a button now and

play08:39

we're going to talk about buttons later

play08:41

on in more detail because we got to add

play08:43

some functionality to the button we're

play08:45

not going to do that now i'm just

play08:47

showing you that you can add a button

play08:48

here so a button is tk.button

play08:51

and pass it to root again and the text

play08:54

of the button is for example click me

play08:57

and it can also have a font i'm going to

play08:59

go with arial again and we can go with

play09:02

18 again

play09:04

and then button.pack

play09:06

there you go

play09:08

and you can see i can click that button

play09:10

uh since there is no padding it is

play09:12

exactly below directly below the text

play09:15

box so we can go with some padding

play09:18

pedex equals 10

play09:21

and pad y equals 10 maybe you want to

play09:23

have some padding here as well pad y

play09:26

equals 10

play09:29

and you can see now it looks a little

play09:31

bit better now this function doesn't

play09:32

have a functionality obviously we are

play09:34

going to talk about that in a second as

play09:36

i or not in a second a couple of minutes

play09:37

as i already told you uh but those are

play09:40

some of the widgets some of the most

play09:42

important widgets we can also just type

play09:44

tk dot and if you have an ide you're

play09:47

going to see what you can use here so

play09:49

for example

play09:50

we also have a check button here

play09:53

uh we have frames a frame is basically

play09:56

just

play09:57

a frame a an area that you can put

play10:00

buttons into for example or text boxes

play10:02

just widgets we have list boxes we have

play10:04

menus and all that so you can play

play10:06

around with that

play10:07

now let's try out let's try the grid

play10:10

layout because the grid layout is a

play10:11

little bit more interesting when it

play10:13

comes to multiple buttons for example in

play10:15

a row so if you want to have a

play10:16

calculator for example let's call up the

play10:18

windows calculator you don't want to

play10:20

have one button below the other buttons

play10:22

you want to have a grid of buttons like

play10:24

here so you want to have for example a

play10:26

text box a label and then you want to

play10:28

have some buttons here

play10:30

in a grid layout if you want to do that

play10:31

in python first of all i have a video on

play10:33

that so you can just check out the gui

play10:35

calculator with tk intro on my channel

play10:38

uh but now we're just going to have a

play10:41

grid of buttons and for this we're

play10:42

actually going to use a button frame

play10:45

so let me just think about this we're

play10:48

going to use a label here in the pack

play10:50

layout we're going to use a text box

play10:52

with a pack layout and we're going to

play10:54

add a frame so the frame is going to be

play10:56

tk or actually let's call it button

play10:58

frame is going to be tk.frame

play11:01

the frame itself is going to be part of

play11:03

root so it's going to have root as a

play11:05

master

play11:06

um

play11:08

and that's actually the only parameter

play11:10

that we pass here but then we're going

play11:12

to say button frame

play11:14

dot

play11:15

configure or actually column configure

play11:18

and here we can set

play11:20

the zeroth column to have a weight of

play11:22

one this is going to be important

play11:24

because we want the buttons to stretch

play11:25

we don't want to have three buttons in a

play11:28

row and then a lot of space left and

play11:30

right we want it to stretch

play11:32

to fill the x-axis and we're going to

play11:33

talk about that

play11:35

uh in a second how exactly we're going

play11:37

to do that but this is a setting that we

play11:38

need we need to say column configure

play11:41

for the individual rows depending on how

play11:43

many rows you have obviously if you have

play11:44

multiple uh actually columns sorry if

play11:46

you have multiple columns uh if you have

play11:48

five columns you need to do it for all

play11:50

five of the columns in this case we're

play11:51

going to have three columns so we're

play11:53

going to do it for the three columns

play11:54

here and what we're doing now is we're

play11:58

going to say btn

play12:00

1

play12:01

is going to be tk button

play12:04

and

play12:05

here now we're not going to pass root as

play12:07

the master we're going to pass the

play12:09

button for a master master because the

play12:10

button is going to be inside of the

play12:12

button frame and the button frame is

play12:13

going to be inside of the root so we're

play12:15

going to pass button frame here and the

play12:18

text is just going to be one we're not

play12:20

going to have any

play12:22

functionality here we're going to say

play12:24

arial

play12:28

and font size 18

play12:30

and what we do now is we say btn

play12:33

1 dot and now not pack but grit

play12:36

and grid takes the parameters row and

play12:40

column

play12:42

so row zero and column zero basically

play12:45

means first row and first column we're

play12:47

in programming we start counting with

play12:48

zero unless you use matlab obviously

play12:52

but that's where you start so for

play12:54

example we want to have the first button

play12:55

in the first row and the first column so

play12:57

zero zero now we also want it to be

play13:00

sticky

play13:01

to the west and to the east so to the

play13:03

left and to the right so what we can do

play13:04

is we can say sticky equals tk.w

play13:08

plus tk.e i mean i think you can also

play13:11

pass a string so you should also be able

play13:13

to say w and e or you can also say

play13:17

uh news for north e north east

play13:21

west and south yes uh but i like to do

play13:24

it like that tk.w plus tk dot

play13:28

e for west and east so basically what we

play13:31

want to have is uh maybe i'm going to

play13:34

explain this with paint real quick we

play13:36

don't want to have

play13:37

uh a

play13:39

window where we have like a text box

play13:41

and then a bunch of buttons like that

play13:45

right so if we have three columns here

play13:46

we don't want to have a numpad like that

play13:49

we want to do it more like that we want

play13:51

to stretch the buttons here

play13:54

of course equally sized not the way i

play13:56

draw them but want them to be stretched

play13:59

across the whole row

play14:01

so we want it to be like that one two

play14:03

three and this is why we do the sticky

play14:05

uh the sticky stuff

play14:07

so we can copy that now and

play14:10

just

play14:12

paste this uh how many times four more

play14:15

times

play14:16

so we have button one button one here we

play14:19

have button two button two

play14:21

button three

play14:23

button three button four button four

play14:26

five five

play14:28

six

play14:29

and of course we need to change the text

play14:32

to 2

play14:33

3

play14:34

4

play14:35

5 and 6.

play14:37

and now we can place them in different

play14:39

columns so we can say okay we want to

play14:41

have button 2 also in the first row but

play14:43

in the second column so we change this

play14:45

to 1 this to 2 then we want to have 3

play14:47

buttons and start the next row

play14:50

so button 4 would be in row 1

play14:53

but column 0 then

play14:55

row 1

play14:57

column one

play14:58

row one

play15:00

column two

play15:02

and those are actually just two rows but

play15:05

we can actually take a look at that now

play15:07

so once we have the grid um i think we

play15:10

only need to pack the frame so we're

play15:12

going to say

play15:13

button frame dot pack because remember

play15:16

we have the pack layout in general but

play15:19

inside of the button frame we have the

play15:21

grid layout so the grid layout of the

play15:22

application or actually the the layout

play15:25

of the application is not the grid

play15:26

layout it's the pack layout but inside

play15:29

of the button frame we have the grid

play15:30

layout uh but because of that we need to

play15:33

say button frame dot pack and we're

play15:35

going to say fill equals

play15:38

x

play15:38

actually as a string is enough

play15:41

we can also pass tk.x i think but this

play15:44

means that it's going to stretch into

play15:45

the x dimension

play15:47

and now you can see that we have the

play15:49

result

play15:50

that we want it a basic grid layout uh

play15:54

where we can do that

play15:55

now before we move on i also want to

play15:57

show you the place function so instead

play15:58

of using grid or

play16:00

pack we can also use the place function

play16:02

to place something manually for example

play16:05

down here i can say another

play16:08

button it's going to be tk.button and

play16:11

it's going to be part of root and it's

play16:13

going to have a text

play16:15

test and now we're going to see another

play16:17

button dot place and i can just say x

play16:20

equals uh what's the geometry 500 times

play16:23

500 let's just say 200

play16:26

and 200

play16:28

for y

play16:29

and then i can say height equals

play16:32

i don't know 100 and

play16:34

width equals 100 as well

play16:37

and by doing that i can just decide on

play16:39

the position because pack is a layout we

play16:41

have a certain structure grid is a

play16:43

layout we have a certain structure we

play16:45

can manipulate parameters place

play16:47

basically means put it there and make it

play16:49

that size so it's a very manual thing

play16:51

and you can see that in this case it

play16:53

looks quite

play16:54

bad but this is how you can place

play16:56

widgets

play16:58

exactly where you want

play17:00

all right so i think you understand now

play17:01

basically how tk intel works we import

play17:03

the module we create a root window we

play17:05

add some widgets and then we choose a

play17:08

layout for example grid or pack or we

play17:10

use the place function to create our

play17:12

graphical user interface now the next

play17:14

step is to add some functionality so to

play17:16

button clicks for example or to certain

play17:18

events that happen we want to add some

play17:20

functionality to the graphical user

play17:22

interface so that it's more than just a

play17:24

design and for this we're going to

play17:25

define our graphical user interface

play17:27

inside of a class because oftentimes

play17:30

it's a little bit tricky with the order

play17:32

in which you define a certain element so

play17:34

if you have the graphical user interface

play17:36

defined and then the functionality in

play17:38

terms of functions so you have a

play17:39

function that does something

play17:41

this function maybe needs to know the

play17:43

graphical user interface in order to

play17:45

access certain elements then again the

play17:47

graphical user interface needs to know

play17:49

the function because the function is

play17:50

what it's called what is called when we

play17:52

do something

play17:54

and this can get a little bit tricky in

play17:56

a class in an object we have everything

play17:58

structured so i can just say refer to

play17:59

that ui element

play18:01

via the self object or refer to that

play18:04

function via the self object it's just a

play18:06

best practice or better practice to do

play18:09

it inside of a function so let's say

play18:11

again import tk

play18:14

import tk enter sdk

play18:18

and then we're going to create a class

play18:20

my gui like that

play18:22

and in the constructor we're going to

play18:23

have the whole graphical user interface

play18:25

process so we're going to start with

play18:28

self.root is going to be tk.tk

play18:32

as before and then self.root.mainloop

play18:36

so just calling the constructor is going

play18:38

to create the graphical user interface

play18:40

already

play18:41

now so that we don't do just anything

play18:43

here let's uh think about a very simple

play18:46

application

play18:47

we're going to have a simple label at

play18:49

the top we're going to have a text box

play18:51

where we can enter some text as a user

play18:53

and then we're going to have a button

play18:55

and when we click that button we're

play18:56

going to get a message box

play18:58

with the text that we typed in the text

play19:00

box displayed onto the screen and in

play19:03

addition so that we use a little bit

play19:05

more ui elements we're also going to add

play19:07

a checkbox if that checkbox is checked

play19:09

we're going to get the pop-up if it's

play19:11

not checked so if it's unchecked we're

play19:13

going to get some console output so

play19:15

we're going to get the message printed

play19:16

onto the console if it's checked we're

play19:18

going to get a pop-up

play19:20

so let's start with the label we're

play19:22

going to say self.label is going to be

play19:26

tk.label again and it's going to be part

play19:28

of self.root

play19:30

and we're going to say the text is

play19:32

your message for example

play19:35

and the font is going to be

play19:38

arial

play19:38

[Music]

play19:40

18 for example

play19:42

self dot label dot pack

play19:45

padding x equals 10 padding y equals 10

play19:48

[Music]

play19:49

then self dot text

play19:52

box

play19:54

going to be tk text box or tk text

play19:58

self dot root

play20:02

and font equals

play20:04

ariel

play20:05

16

play20:08

self.textbox

play20:10

dot pack

play20:12

pad x 10

play20:14

pad y 10 and of course don't forget the

play20:17

height let's say the height is five for

play20:20

something should be enough

play20:22

and then we're going to add the checkbox

play20:24

so the checkbox is going to be

play20:26

self.check

play20:28

it's going to be tk.check button

play20:31

it's going to be part of self.root and

play20:33

we're going to say the text is

play20:35

show

play20:37

a message

play20:39

box

play20:40

like that

play20:41

and i don't think that we need much more

play20:45

for that i mean of course yeah we need

play20:47

uh to have

play20:48

the font size again

play20:52

ariel

play20:53

and 18 or actually let's make the 16.

play20:56

now

play20:57

the check button in tk enter is a little

play20:59

bit tricky because we cannot just into

play21:02

intuitively go and say self.check dot uh

play21:05

is checked or something and we get true

play21:07

or false we need to add a variable to

play21:10

that check button and this variable is

play21:12

going to have the value of the check

play21:13

button it's a little bit complicated uh

play21:16

so let's first say self.jack.pack

play21:19

uh

play21:20

pack

play21:21

pad x equals 10

play21:24

pad y equals 10 as well

play21:26

and uh before that we're going to create

play21:28

now here's self dot

play21:32

let's just call this

play21:34

check

play21:36

check state or something

play21:38

and this is going to be tk dot

play21:41

int

play21:42

var in variable basically

play21:44

and this in variable is going to have

play21:46

this integer variable is going to have

play21:49

the state of the checkbox and in order

play21:51

to get that we need to say

play21:53

variable

play21:54

equals

play21:57

self dot check state

play21:59

like that

play22:01

and last but not least let's add the

play22:02

button

play22:05

self dot button is going to be tk dot

play22:07

button

play22:08

self dot root

play22:11

the text is going to be

play22:12

show message

play22:15

font is going to be ariel

play22:18

18

play22:20

self.button.pack

play22:23

there you go now in order to add the

play22:25

functionality first of all we're going

play22:26

to define the function itself so we're

play22:28

going to say def

play22:30

and we're going to call the function

play22:31

here

play22:32

show message

play22:34

like that

play22:36

i'm going to pass for now and in order

play22:38

to make this function being executed

play22:40

when the button is clicked we're just

play22:42

going to go to the end here and say

play22:44

command equals and then self dot

play22:49

show message now it's important that you

play22:51

don't call the function so you don't

play22:52

want to say show message and then

play22:54

parentheses like that because we're not

play22:56

calling show message we're passing show

play22:59

message as a parameter and it's going to

play23:01

be called whenever we click the button

play23:03

so if we for example write something

play23:06

like hello world here

play23:09

and we create a my gui object here at

play23:13

the bottom

play23:15

and we run the script

play23:18

we should be able that when i press

play23:20

we should be able to see that when i

play23:22

press show message we see hello world in

play23:24

the console all right so it works

play23:27

now all we need to do is we need to um

play23:31

to show the message from the text box

play23:33

here onto the screen via message box

play23:36

depending on the check so the first

play23:38

thing is want to know okay is the

play23:40

checkbox checked or not now in order to

play23:42

get the the state of this variable here

play23:46

we need to now say

play23:48

print

play23:49

self.checkstate.get

play23:53

like that this gets uh the state of the

play23:56

checkbox so now if i run this you're

play23:58

going to see that we're going to get a

play23:59

number

play24:00

if i click it you see 0 if i check and

play24:03

then click it you see one so if the

play24:06

check box is unchecked we're going to

play24:07

have zero otherwise one

play24:10

and this is what we can look at so we're

play24:13

going to say if

play24:15

self dot check state dot get

play24:19

equals zero then we're just going to

play24:22

print

play24:23

what exactly are we going to print we're

play24:25

going to print the content of the text

play24:27

box so self dot text box and this is

play24:30

again something that's a little bit

play24:32

tricky because in

play24:33

uh in tk intro we need to use the get

play24:36

function and provide a start and an end

play24:38

so index 1 index 2.

play24:41

the index 1 is a string which is 1.0 if

play24:45

you want to start in the beginning

play24:47

and if you want to stop uh in the end so

play24:50

if you want to go all the way to the end

play24:52

you have to pass tk.end

play24:55

now this is a little bit unusual because

play24:57

in other frameworks you would just say

play24:59

box dot get value or get text or dot

play25:01

text here you need to say dot get and

play25:04

you need to pass that as a beginning and

play25:06

this is an end if you want to get

play25:08

everything

play25:09

and

play25:10

otherwise if we don't get that we're

play25:13

going to

play25:14

we're going to print a message box and

play25:16

for that we need to make an import we

play25:18

need to say

play25:20

from tk inter import

play25:23

message box like that

play25:27

and now we're just going to say

play25:31

message box dot

play25:34

show info

play25:35

and the content first of all the title

play25:37

is going to be

play25:39

message and the message itself

play25:43

is going to be self.textbox

play25:46

dot get again 1.0

play25:50

tk.end which basically means

play25:53

go all the way

play25:54

through so this should already work

play25:58

if i type something

play26:01

there we go

play26:02

and if i uncheck this we see it in the

play26:05

command line instead of in a message box

play26:08

so this works fine

play26:10

uh what else did we want to do here now

play26:13

one thing that i would like to add here

play26:14

because this is something that a lot of

play26:16

people are asking when i do gui

play26:17

tutorials is how can i do something

play26:21

like when i'm in the text box i don't

play26:23

want to click the button i want to say

play26:25

enter for example and based on that

play26:27

click based on that keyboard press i

play26:29

want the same thing to happen now in

play26:31

this case enter would be a stupid choice

play26:33

because enter is something that we can

play26:34

use inside of the text box to change the

play26:37

text but maybe we want to have something

play26:39

like control enter

play26:41

and control enter basically

play26:43

triggers

play26:44

that this function is executed as well

play26:47

how can we do that

play26:49

it's again a little bit tricky in tk

play26:51

enter it's not the most straightforward

play26:53

thing but what we do here is we go to

play26:55

the text box and we say self dot text

play26:57

box and then we can bind something here

play27:00

we can bind

play27:01

a certain type of event to a function

play27:05

for example we combined

play27:07

key press

play27:08

in angle brackets this has to be that

play27:10

exact string

play27:12

this thing has to be bound to self dot

play27:15

and now we can define another function

play27:17

for example

play27:19

let's call it shortcut

play27:22

and

play27:23

we can pass an event here

play27:26

we have to pass an event here because

play27:27

the event is going to be passed by the

play27:30

bind function so we bind the keypress

play27:32

event to self.shortcut

play27:36

and this automatically passes an event

play27:38

and from that event object we can now uh

play27:42

look at what's happening now one thing

play27:44

that i want to show you is that this

play27:45

event object has certain parameters now

play27:50

if i run this i'm not sure if we're

play27:51

going to see that

play27:54

so if i'm now in the message box here

play27:56

you can see that whenever i press

play27:57

something we get a key press event down

play28:00

here

play28:01

and this keypress event has an event a

play28:04

state here and a key symbol

play28:07

so what we're interested in is

play28:09

we want to have enter and we want to

play28:11

have control now if i run this here and

play28:13

i just press enter you can see the key

play28:15

symbol is return

play28:17

now if i press control you can see the

play28:19

key symbol is control but if i hold ctrl

play28:22

and i press enter

play28:24

you can see that the key symbol was

play28:27

returned

play28:29

but the state was control mod 1. so if i

play28:33

run this again and i don't press control

play28:35

i just press enter you can see that the

play28:37

state was mod 1 if i now press control

play28:40

and enter you can see control is part of

play28:42

the state so all we need to do here in

play28:45

order to get the key combination is we

play28:47

need to uh print event dot key symbol

play28:50

and we need to

play28:52

print event dot state

play28:55

and this is going to tell us what we

play28:57

need to look for because if i now press

play28:59

enter we get return and the state is

play29:02

eight if i press ctrl enter

play29:04

the state is

play29:06

12 and return is

play29:09

uh

play29:10

return is the button

play29:12

so all i need to do here is i need to

play29:14

say if

play29:16

event

play29:17

dot

play29:18

state equals 12

play29:21

and

play29:22

event

play29:23

dot key symbol equals return then do

play29:28

something for example let's print hello

play29:29

for now

play29:30

to see if that works in the first place

play29:33

so now if i press something else nothing

play29:36

happens but if i press ctrl enter we get

play29:38

hello so this works now instead of

play29:40

printing hello

play29:42

we're just going to call show message

play29:45

so self.showmessage

play29:50

and now you're going to see that if i

play29:52

have something in here and i now press

play29:55

ctrl enter

play29:57

oh of course we need to trigger that

play29:59

checkbox otherwise we're not going to

play30:00

get the message box but you can see that

play30:02

this trigger is the same thing as the

play30:04

button

play30:06

so this works like that um

play30:09

now one thing that i would like to also

play30:11

show you is that sometimes i mean in

play30:13

this case probably not but sometimes you

play30:15

want to have some uh closing dialogue so

play30:18

when you press the close button you

play30:19

don't want the application to just be

play30:21

closed but maybe you want to ask the

play30:23

user do you really want to leave or do

play30:25

you want to save something we're not

play30:27

going to talk about safe dialogues and

play30:29

open dialogues and all that

play30:31

i think in the editor video in the

play30:33

notepad clone

play30:35

you're going to find that dialog but

play30:37

besides that or actually that was pi qt

play30:39

but i think in a paint clone you're

play30:41

going to find safe and

play30:43

open file dialogs today we're going to

play30:45

only talk about the closing event so

play30:48

what happens when i close the window how

play30:50

can i react to that so let's say maybe i

play30:53

just want to print hello world or

play30:54

goodbye

play30:56

whenever the user presses on the x

play30:58

button

play30:59

now in order to get the event what we

play31:01

need to do is we need to say self

play31:04

dot root dot protocol

play31:07

and this is now a string which is called

play31:10

wm underscore delete

play31:13

underscore window and what we passed

play31:15

here as a second parameter is the

play31:17

function that we want to call whenever

play31:19

this event happens so whenever we click

play31:21

on the close button we're going to call

play31:23

that function let's call it on closing

play31:27

and of course we need to define it down

play31:28

here so def on closing

play31:33

and in here we're going to just say

play31:35

print

play31:37

hello world

play31:38

and important we need to also destroy

play31:41

the window because if we don't do that

play31:43

all that's going to happen is that i'm

play31:45

going to print hello world when i click

play31:47

that it's no longer going to close so we

play31:49

need to say

play31:50

self

play31:51

dot root dot destroy

play31:57

so again

play31:59

there you go

play32:00

now instead of printing hello world

play32:02

we're going to ask for

play32:03

uh for the user to

play32:06

confirm that the user wants to leave the

play32:08

application so we're going to say

play32:11

i need to look up the syntax again here

play32:13

if

play32:14

messagebox

play32:16

dot and now we can choose a dialog from

play32:18

here we have something like

play32:20

ask question ask retry cancel ask yes no

play32:24

cancel

play32:25

or ask yes no this is what we're going

play32:27

to use here

play32:29

uh ask yes no there you go and we're

play32:31

going to pass the title

play32:33

quit question mark message

play32:36

equals

play32:37

do you really want to quit question mark

play32:41

now if the answer to that is yes we're

play32:44

going to have a positive value here so

play32:47

this if statement is only going to be

play32:49

triggered if we uh if we pass if we

play32:52

press yes and otherwise we're going to

play32:54

do nothing

play32:56

so if i press on x now do you really

play32:59

want to quit no nothing happens do you

play33:02

really want to quit yes

play33:03

destroyed

play33:05

now last but not least for this video in

play33:06

order to just show you a little bit more

play33:08

ui elements we're going to also add a

play33:10

menu to the top so something like here

play33:12

in pycharm with file edit view and all

play33:15

that we're going to keep it minimal but

play33:16

i'm going to show you how you can just

play33:19

create a simple menu bar and inside of

play33:21

that menu bar you want to have different

play33:23

menus for example file and action or

play33:25

help or something

play33:27

and then we can also add functionalities

play33:29

to the individual presses inside of that

play33:32

menu bar so what we're going to do is

play33:34

we're going to say self dot

play33:37

menu bar which is going to be the main

play33:39

thing it's just going to be tk dot menu

play33:42

and it's going to be part of self dot

play33:44

root

play33:45

then we're going to also create self dot

play33:48

file menu

play33:49

and this is going to be tk menu as well

play33:52

so one menu is going to be part of the

play33:54

other menu

play33:56

and this is going to be part of self dot

play33:58

menu bar

play34:00

and we're going to also pass the

play34:01

parameter

play34:03

uh tear off or tear off tierra right

play34:06

is going to be zero so that we have

play34:08

otherwise we're going to have a dashed

play34:10

line at the top and we don't want that

play34:12

and inside of that file menu we're now

play34:14

going to add some uh

play34:16

things that we can do so for example we

play34:18

can say self dot file menu dot add

play34:21

command

play34:23

and we're going to have a simple command

play34:24

with a label

play34:26

close

play34:27

and this label close is going to have

play34:29

the command exit we're going to refer to

play34:32

the basic python exit function and

play34:34

that's all it's going to do

play34:36

now if i want to add this now to the

play34:39

final to the to the menu bar what i do

play34:41

is i say self dot menu bar

play34:44

dot add cascade

play34:47

and the menu i'm adding is self dot file

play34:50

menu

play34:52

and the label of that menu is going to

play34:54

be file

play34:56

and then last but not least i think i

play34:58

need to also add the menu bar to the

play35:01

root and we do that by saying

play35:03

self.root.config

play35:05

and we set the menu of the root to be

play35:08

self dot menu bar

play35:12

so now if we run this you can see we

play35:15

have file up here and i can click on

play35:17

close and closes the window

play35:21

now actually we can also link this to

play35:23

not exit but to self dot on closing

play35:27

i think that it has the same effect as

play35:30

clicking this button so

play35:32

no

play35:33

or

play35:35

yes there you go so this also works now

play35:38

we can also add multiple commands i'm

play35:40

going to add the same command

play35:43

again so we can say close with error or

play35:45

close

play35:47

without

play35:48

question just so we have a second item

play35:50

here

play35:51

we're going to pass exit here and

play35:54

between those we're going to have a

play35:55

separator so we can also say file menu

play35:57

dot at separator

play36:00

and this may be useful if you have a lot

play36:02

of different options of different

play36:03

categories you can have this separator

play36:06

line between these two buttons here

play36:08

or between these two commands now let's

play36:10

just um so that we see that this also

play36:13

works let's just add another menu so

play36:15

action menu for example is going to be

play36:17

tk menu

play36:19

part of self dot menu bar

play36:21

tear off

play36:24

zero um not sure how it's pronounced

play36:28

tear off tear off i think tear off makes

play36:30

more sense right

play36:31

let me know in the comment section down

play36:33

below

play36:34

so action menu dot add command

play36:37

on we can just say i don't know label is

play36:40

going to be show

play36:43

message

play36:45

and the command is going to be

play36:47

self dot show message like that

play36:51

then again

play36:52

self dot menu bar actually let's do it

play36:55

down here

play36:56

add cascade

play36:57

self dot action menu

play37:01

and label is going to be action

play37:06

there you go so now we have action so uh

play37:08

show message

play37:10

like that if we have the message box

play37:12

checked

play37:13

this is going to work as well

play37:17

one final thing i want to show you is if

play37:19

you want to for some reason clear the

play37:21

text box let's create a second button

play37:24

self dot clear button

play37:26

and this button shall delete the content

play37:29

of the box

play37:30

so let me just write it real quick text

play37:33

equals clear

play37:35

font equals ariel 16 no 18 actually

play37:39

the command is going to be

play37:41

self dot clear and we're going to define

play37:44

it

play37:45

self dot clear button dot

play37:48

pack with padding

play37:53

and uh if we want to do that we need to

play37:55

do something similar to the getting here

play37:57

so we need to also specify these indices

play38:00

so what we're going to do is we're going

play38:02

to say def clear

play38:04

and we're going to say self dot

play38:08

text box dot delete

play38:11

and here we pass 1.0 and tk.end as far

play38:14

as i know this is how it's done so

play38:19

there you go if i write something and

play38:20

click on clear this clears

play38:23

the message box

play38:25

all right so that's it for today's video

play38:26

i hope i was able to give you a good

play38:28

introduction to tk inter if you liked

play38:30

that video let me know in the comment

play38:31

section down below and click the like

play38:33

button and of course don't forget to

play38:34

subscribe and hit the notification bell

play38:36

to not miss a single future video for

play38:38

free other than that thank you very much

play38:39

for watching see you next video and bye

play38:43

[Music]

play38:58

you

Rate This

5.0 / 5 (0 votes)

Related Tags
TkinterPythonGUIبرمجةواجهة مستخدمتutorialsبرمجة بيثونتصميم واجهةبرمجة تطبيقاتدروس برمجية
Do you need a summary in English?