All About The Button Widget - Tkinter Widget Library 1

ruticker 05.03.2025 16:01:07

Recognized text from YouScriptor channel Tkinter․com

Recognized from a YouTube video by YouScriptor.com, For more details, follow the link All About The Button Widget - Tkinter Widget Library 1

```markdown # Button Widget in Tkinter What's going on, guys? John Older here from tkinter.com, and in this video, we're going to dive into the **Button widget**. Oh yeah! Alright, guys, like I said, in this video, we're going to learn absolutely everything there is to know about the Button widget. This video is part of my in-depth widget playlist, so check that out if you're new to Tkinter. Link in the pinned comment below. Also, be sure to grab a free PDF copy of my Tkinter Widget Quick Reference Guide book. Just head over to [tkinter.com/widget-book](https://tkinter.com/widget-book) or find the link in the pinned comment as well. While you're there, check out [membershipntkinter.com](https://membershipntkinter.com) where you can get all my Tkinter videos and courses and all future courses for one low price. Use coupon code **YOUTUBE** to get 30% off membership while you still can. ## Let's Learn About the Button Widget Alright, like I said, in this video, we're going to look at the Button. Here I've got just a basic Tkinter app and a button on here. We click it, and nothing actually happens, but we're going to talk about this button in this video. Now, buttons may seem very boring, but there are like 23, 24, 25 different attributes you can use to mess around with the button, and most people don't even know many of them exist. So we're going to talk about all of them in this video. That's sort of what this book does; it goes through every single widget and talks about all the attributes. If you didn't download the free PDF copy of this book, go ahead and do that now. I'll put the link in the description below. So let's head over to our code. I'm using the Sublime Text editor and the Git Bash terminal as always. And as always, you can find a link to the code in the pinned comment section below, as well as a link to the playlist with all the other videos in this widget series. So check that out if you haven't so far. This is actually the first video in the widget series. We're just going to go through all the widgets alphabetically and just talk about them all, one per video. **B for Button**—that's right at the top of the list, so that's what we're going to do in this video. I've just got our basic Tkinter starter code that we sort of always have. I've got a little app, 500x350. I'm going to come down here, and I'm just going to create a button called anything you want. I'm going to call it `my_button`, and that's going to be a button, and we want to put it in `root`. Let's just start out with the text equaling **Click Me**. Now, once we've created it, we need to pack it onto the screen, so let's go `my_button.pack()`, and I'm going to give this a `pad_y` of like 50 to really push it down the screen since there's nothing really else in our app. Let's go ahead and save this. I'm calling this `buttons.py`. Head over to our terminal. I'm in my `ctkinter.com` directory; that's where I'm going to be saving these files. Put it in any directory you want; it doesn't matter at all. Let's run `python buttons.py`, and when we do, we get this little button that says **Click Me**. Pretty boring; nothing happens here, but that's the button. Now I want to spend the rest of this video talking about all the different attributes, showing you each one, how to use them, what they do, and all that good stuff. So what I'm going to do is I'm going to come back over here, and I'm just going to add a comma here and give us some space, and I'm just going to paste in this list of attributes. Like I said, there's 20, 22, 23, something like that in this list. So you see I've got them all commented out. We'll just go through these one at a time and kind of knock these guys out. ### Attributes of the Button Widget 1. **Active Background**: This is the background color of the button when it's active. Let's just go ahead and change this. You can use hex codes, so like `#000000` or you can just say like **black**. Let's go ahead and save this and run it. You can see when I click it, when I hold down my mouse button, it turns black. When I let off the mouse button, it goes back to normal. That's the active background color. 2. **Active Foreground**: Same thing, but instead of the background, it's going to be the foreground. Let's say **red**. Save this, run it, and when we click this, you'll see the text is red now when I'm holding my button down. This is the active foreground. 3. **Anchor**: This allows you to sort of move the widget around the app. The default here is going to be **center**, but you can change this to a direction: north, south, east, and west, or southeast and southwest. If we want to put it south, it's just lowercase **s**. Go ahead and save this, head back over here, run it, and you can't really see the difference because there's nothing else to sort of reference it to. But if there are other things, this is going to move stuff around. 4. **BG**: Stands for background. We can just change this to something dramatic. This is going to change the background color. Now the whole thing is just black. We could change it back to a default with **system button face**. Now we get it back to normal. 5. **BD**: Stands for border width. If we give this something dramatic like **10**, this is going to change the border size of our button. You can see now it has a huge relief. 6. **Command**: This is the most important part of the button. When you click it, what does that button do? Right now, we've just got a button that we click, and nothing happens. You're never going to have that in real life. Whenever you have a button in real life, when somebody clicks it, you want something to happen. So that's what command is. We can just create a function name. I'm just going to call it **clicker**. Now this function will get called every time we click the button. We don't have that function yet, so let's come up here and define **clicker**. What do we want to happen? Let's just go ahead and change the text of our button. We can do that by configuring the button: `my_button.config(text="You clicked the button")`. Let's go ahead and save this, head back over here, and run this guy. Now the button says **Click Me**. When we click it, boom! It changes to **You clicked the button**. 7. **Default**: The options for default are **active**, **disabled**, **normal**, and **disabled**. The default is disabled for this, so we can change this to **active**. If we save this, we're not really going to see much of a difference, but you can see it has a little bit different border by default. 8. **Disabled Foreground**: When the button is disabled, this will change the foreground color. If we change this to, for instance, **green**, if we save this and run it, we're not going to see anything different here. 9. **State**: This does what it sounds like—what's the button state? That could be **normal**, **active**, or **disabled**. If we change this to **disabled**, we now have a disabled foreground color. 10. **Font**: Changes the font of the text in the button. We can say let's change this to **Helvetica** and say we want size **32**. 11. **FG**: We've already looked at this in other things, but that's the foreground color. 12. **Height**: This will depend on your button. 13. **Highlight Background**: This will set the color used for the highlight border when the button doesn't have focus. 14. **Highlight Color**: This finds the color used for the highlight border when the button has focus. 15. **Image**: We can actually add an image here. 16. **Justify**: Defines the alignment of text if you have multiple lines of text. 17. **Wrap Length**: Defines whether or not text should be wrapped. 18. **Relief**: This is how the border of your button looks. 19. **Over Relief**: This is how it looks when the mouse moves over the button. 20. **Border Width**: This is the width of the border around the button. ### Conclusion That's a lot of attributes to play with! You can customize your button in many ways to make it fit your application's needs. Be sure to experiment with these attributes to see how they affect the button's appearance and behavior. ``` ```markdown # Continuing with the Button Widget in Tkinter To you know, it wouldn't look quite as dramatic, but you know, for purposes of just checking it out, it's nice to have it real big so we can see exactly what it looks like. ## Take Focus Next, we have **take focus**, and take focus is all about the tab key. So let's say you've got five buttons, and yeah, you can move your mouse over and click on each one, but a lot of people like to just use their tab key to sort of move from thing to thing. Take focus allows you to allow that or not. The default is yes, you can tab things, but if we wanted to change this, for instance, to **false**—and this is a boolean, true or false—we could go ahead and save this and run it. It's kind of hard to tell, but when I hit my tab key, nothing happens; the button doesn't change at all. If we come back over here and change this back to **true**, run this guy again, now when I hit my tab key, it kind of highlights a little, right? It has focus, so that's what that is. ## Underline Next, we have **underline**, which indicates which item of text in the button is underlined. If we actually let me comment this out, save this, and just see what it does by default, you notice none of the letters are underlined, right? But if we want one underlined, which is sort of common with buttons, it starts at negative one, I think is the default. So if we go **0** here, and actually, I think we just do it like that—it's an actual number—so **0**. Think of it like a Python list; it starts at zero. The **c** is zero, the **l** is one, the **i** is two, the **c** is three. So if we want the **c** to be underlined, which I don't know why you would, but if you did, you change that. Now the **c** is underlined. ## Width Finally, we have the **width**, and that just changes the width. Same thing as before; if you're using text, it's going to use text units as the sort of unit of measure. If you're using a button, it's going to use pixels as the unit of measure. So, you know, a pixel is like one little dot, so you would need like 200 pixels to make it pretty big or whatever. Let's just change this to, I don't know, **40**. Save that, run it, and see our button is now very big. You really have to kind of play around with this. Maybe we want to go **20**—something like that, still very big. So let's go **10**, and now we're getting back to sort of normal size. You notice when we clicked it, it changed the text, but it didn't resize the button. So if you're using width and you need to resize the button on click, you would do that in the function. We could come up here and go `my_button.config(width=15)`, whatever you have to play around with. So now, boom! The button resizes. Still not big enough to get all that on there, but you play around with it and get it however you want. I don't know, let's try **18**. Yeah, it almost fits there now, so probably **20** or **19**, but our app isn't quite big enough for that, so you might have to resize the app too. Whatever, there you go! Now it all sort of fits, but just sort of keep that in mind when you use width. ## Conclusion So those are the main attributes. There are a couple more that I've got listed in the book, but they're so obscure that nobody ever uses them, and almost can't use them. So I didn't, you know, those maybe three or four other ones—two or three—but pretty much irrelevant. These are the ones you're going to use all the time, and that's kind of all there is to it. So that is everything you ever wanted to know about the Button widget. Pretty fun! And like I said, this is the first of the widget series. We're just going to go through alphabetically all 35 or however many widgets there are, including TTK widgets, and we're just going to do this. It's going to be very boring unless you're very interested in changing these things for all these specific widgets, but if you are, it'll be a nice little library you can just kind of look up, or if you've got the book, you could just flip to the book and find the one you want and get all this information right from there. That's all for this video! If you liked it, be sure to smash the like button below, subscribe to the channel, give me a thumbs up for the YouTube algorithm, and check out [tkinter.com](https://tkinter.com) where you can use coupon code **YOUTUBE** to get 30% off membership. So that's access to all my courses, hundreds of videos, and the PDF of my new Tkinter book. My name is John Elder from tkinter.com, and I'll see you in the next video! ```

Назад

Залогинтесь, что бы оставить свой комментарий

Copyright © StockChart.ru developers team, 2011 - 2023. Сервис предоставляет широкий набор инструментов для анализа отечественного и зарубежных биржевых рынков. Вы должны иметь биржевой аккаунт для работы с сайтом. По вопросам работы сайта пишите support@ru-ticker.com