Python 3 Programming Tutorial - Tkinter adding buttons

sentdex · Beginner ·🛠️ AI Tools & Apps ·11y ago

Key Takeaways

This video tutorial covers the basics of adding buttons to a tkinter window in Python 3, including creating a button instance, packing it into a frame, and setting its text and position. The tutorial also touches on event handling and how to make the button functional.

Full Transcript

Hello everybody and welcome to the second part of our Python 3 tkinter uh tutorial minieries. Uh in the last video we made just a really simple basic window. We did it in eight lines um which would really take you hundreds if not thousands of lines to create a window normally. Uh so we can already kind of see the value of using tkinter. Um but anyway uh with that we're going to go ahead and continue on. So the first thing that we might want to have in our window is people want to maybe add buttons. So let's go ahead and add a button to this. So the first thing that we're going to do is we're gonna come back over here and in this initialization we want this window to pop up, you know, immediately. Okay, so we have a class window and it's really just a frame and we've initialized this. But now what we're going to want to go ahead and do is in our initialization we're going to say self dot and then we're going to go init window empty parameters for now. Now init window does not exist. Okay. So this is not something that's built into the tinter. We're going to use this and we're going to create this function right now. So we'll come down and we're going to say define init window. And again we're just going to pass through a single parameter. Can you guess what the parameter is going to be? self. That's it. colon. Um, and now what we're going to go ahead and say is self.master.title is going to be uh we're going to call it guey. Okay. And then we're going to say uh self.pack. And uh basically first of all let's just go ahead and I suppose what we should do is def explain what what we're doing here. So again, so what you have with a a window is you actually have a frame and then within that frame you put really windows. Um and so basically here we're initializing the window and then um here we're saying the title of our window is guey. So your understanding of what a window is uh where the title of that window might go is going to be guey. Okay. And now what we're doing is we're saying self.pack. And what we're going to do is we're going to say fill equals both and then expand oops expand equals 1. And basically what this is saying is yes uh we'll adjust dimensions as as needed. And then also we're filling up the window. And then we're going to come down here and what we want to do is we want to create our button instant. So and then pack. We're basically packing this into our frame. Okay, so we've got um let's say we want to have a quit button. So we can all not only can we make a button, but we can uh go from there into event handling. So we're going to say quit button equals capital B u t o n for button. And this button is a part of tkinter. So we're kind of taking button from tkinter. Uh maybe it would be nice uh for us to view that really quick. So 34 just a quick aside TCL. Keep that in mind. Uh you might want to check that out at some point. Anyway, Tkinter and uh buttons probably in an maybe even in main. I should have found these before. I guess I won't find it because I don't want to spend like 20 minutes searching for that on on film. But anyway, button is a function within uh tkinter. Now again, button self. Again, it could be bajjumba or whatever you wanted. And then we're going to say uh text. This is the text that will be written on the button. So you could put anything here. For us, we're going to say the text is uh quit. Okay. And then what we're going to go ahead and do is enter enter. And then we'll say quit button. And we want to place this button uh at x equals 0 and y equals z. So this will put it at the upper left. So if you're familiar with like CSS or anything like that, generally what you do is you start at the upper left and any additions right are that many pixels over that many pixels down. Okay, a little bit different than your the convention where it's really the bottom left and then you know X is over, Y is up. It's not really like that. It's actually X over true, but Y is actually down so to speak. So anyway, um that should be good. Uh we've got now a quit button placed. It's not going to do anything, but it's there. Okay, so we're placing that quit button. Now, it might be a good idea to once we start saying, "Hey, we're going to use these pixels, and here's what we're going to do, blah, blah, blah." It might be a good idea to go ahead and specify the default shape of this window. That way, as we're kind of placing things on it, we can do the math in our head, say, "Hey, this button will fit," or, "No, this button won't fit." Because like if you say we wanted to place this button at x= 500 and y equals 1,000. Well, we saw the previous window the deep by default window is not that big. So uh it wouldn't even be there. Okay. So what we're going to go ahead and do is after we've defined root equals tk next thing we can do is we can say root. geometry geometry geomet geometry goodness I can't spell. And then here you can specify the dimensions of this window. So, we're going to say it's a 400 by300 window. Okay. And then that's really all the things that we need to change. So, now what we can do is let's go ahead and save and run this. And here's our window. Now, you can see our default size of our window is different. We can still, however, you know, modify the size of the window and all of that. Uh the quit button we can click, but it doesn't do anything. But at least it acts like a button. Like look at all that. Like we didn't have to code any of that. uh you know the button being pressed in and out, shape of the button, the shadow of the button. I mean, all this stuff that's going into this window uh is pretty impressive. So anyway, uh click the X. So that's how we can make a button. But we clicked the hell out of that button and nothing happened, right? So now what we need to do is we have to give the button an event handling ability. So when someone clicks it, it does something. Okay, that's what the whole point of a button is. So, in the next video, that's what we're going to be covering is uh event handling and making this button actually, you know, quit. Okay. So, if anybody has any questions or comments regarding uh this video, please feel free to leave them below. As always, thanks for watching. Thanks for all the support and subscriptions. And until next time.

Original Description

In this tutorial, we cover adding buttons to our tkinter window. Adding buttons is very simple, and we can also begin to add events to these buttons. Sample code for this basics series: http://pythonprogramming.net/beginner-python-programming-tutorials/ Python 3 Programming tutorial Playlist: http://www.youtube.com/watch?v=oVp1vrfL_w4&feature=share&list=PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M http://seaofbtc.com http://sentdex.com http://hkinsley.com https://twitter.com/sentdex Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This tutorial teaches how to add buttons to a tkinter window in Python 3 and set up event handling to make the button functional. It covers the basics of GUI programming and event-driven programming.

Key Takeaways
  1. Import the tkinter library
  2. Create a tkinter window
  3. Create a button instance
  4. Pack the button into a frame
  5. Set the button's text and position
  6. Set up event handling for the button
💡 The key to making a button functional in tkinter is to set up event handling, which allows the button to perform an action when clicked.

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →