Button Functions - PyQt with Python GUI Programming tutorial 4
Skills:
UI Design70%
Key Takeaways
This video tutorial covers creating custom button functions and methods in PyQt4, including defining custom event handling and modifying window properties.
Full Transcript
What is going on everybody? Welcome to part four of our Python with PIQT tutorial video series. In this video, we're going to be talking about having custom methods or functions uh when you go to click your button. So, custom event handling. So, uh the first thing that we need to do is let's go ahead and create the method that we want to use when we click a button. So you can put this method really anywhere within our window class, but I think it's best to have like your your in it obviously at the top, then maybe your views and then your methods below. You might also a lot of people get angry because I write everything in one script. You might also have another script for you know methods that you might run or something like that. But until an application gets to like over a thousand lines, I really think it's unnecessary to make a bunch of extra files. But do what you want. So define uh and this one's going to be define close application and again you have self now then we're going to go print and then we'll just say whoa so custom and then we'll just run a simple cy.exit and that should that should close every close it for everybody there. Now then we come here and button.click.connect connect we can replace this here with self.close application. Now when we go to run this we can click quit and then not only did it quit but we also got our wa so custom message. Now I understand you're probably like oh well we already had something that could quit this stupid or something like that. Uh, and really like closing an application or quitting is like the easiest thing to code besides like maybe a print function, but we also want to show that we can do stuff with the application. We could also do other things. You know, you could say uh something like this instead of exit. You could you could leave print or you know, wo custom. We could do set window title piqts with more exclamation points. Should be able to get away with this. We'll see. Quit. And then yeah, so you can see that we got more expl exclamation points. And so I was that's really just a quick example, but it's good enough to show that you can start modifying the window with these methods. You don't necessarily have to quit or something like that, but we're going to leave this quit and we'll we'll actually build up this quit more as time goes on. Uh but anyways, that's the quit. Now, the only other thing I want to show you guys before we move on is going to be we're going to come down to uh this button definition here. And there's a couple of things that are kind of built in to QT which are kind of nice. So, you have button resize and button move. You can leave button move alone, but resize, if you recall, there was a default size, right? So, if we ran this without specifying any size to our button, that's the size. And in fact, let's put it at 0 0. So we can use um we can use the title as a point of reference. So as you can see, this pretty big button and it goes past the the window title. Now we can also do uh button resize 100 100. Okay, so that's now at the top corner. And then we can also put in a different parameter here. We can say button and we're going to say the size we want it to be is btn. Hint. Okay. So what this is going to do is it's going to p QT is going to give us sort of a a hint at what size maybe we ought to use and this is in accordance to how much space is there how many other buttons we have and so on and it might behoove you to use size hint on all of your buttons uh just so you can have um some you know some sort of automated structure now so when we run size hint you can see we're actually returned a pretty small button and it doesn't quite go past or it might but it would be slight if it uh the title but it's much smaller. So you've got size hint and then you've also got another one which is uh minimum size hint and that will return the smallest suggested size. So size hint will return just like uh the best it thinks and then you can also pass in minimum size. So it'll return to you like the smallest it thinks is also acceptable. Now as we can see this returned basically the same thing. So there's not much difference here. Uh but you just can know that these two things exist. So we can leave uh I'm just going to leave it at 100. Well, I can't decide what I want. We'll we'll leave minimum size in. That's totally fine. It doesn't really matter what we do with this button. You can do what you want as well. So anyways, that's that with uh this application and buttons and doing your own little custom methods. Don't worry, we will build up uh our own custom methods to be a little cooler than what this one is doing, but I think it's good to start with the basics first, at least keeping things basic so it's easy to understand how simple this connection really is. Now, uh in the next video, we're going to be talking about adding a main menu to our window because that's something pretty much everybody wants. So, that's what we're going to be talking about in the next video. If you have any questions or comments up to this point, please feel free to leave them below. Otherwise, as always, thanks for watching. Thanks for all the support and subscriptions. And until next time.
Original Description
In this PyQT4 tutorial, we cover how to create our own button functions and methods.
sample code: http://pythonprogramming.net
http://hkinsley.com
https://twitter.com/sentdex
http://sentdex.com
http://seaofbtc.com
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: UI Design
View skill →Related Reads
📰
📰
📰
📰
How I Built a Free Online Image & PDF Processing Platform with Vue 3 + FastAPI
Dev.to · IAMUU
I Built a Free AI-Powered YouTube SEO Toolkit With Zero Budget. Here’s What Actually Happened.
Medium · Startup
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Dev.to · David García
🎓
Tutor Explanation
DeepCamp AI