Create Graphical User Interfaces With Python and PyQT5 - PyQt5 GUI Thursdays #1

ALEFBIT Team

ALEFBIT Team

Aug 2, 2025 2:34 PM

Summary

  • Introduction to PyQt 5 (00:02-00:38): John Elder introduces the video series dedicated to building GUI apps with PyQt 5, announcing that every Thursday will focus on PyQt development.

  • Setting Up Environment (01:02-02:19): The tutorial begins with setting up a project directory and creating a virtual environment using python -m venv. Instructions for activating the environment on both Windows and Mac/Linux systems are provided.

  • Installing PyQt 5 (02:49-03:19): John demonstrates how to install PyQt 5 using pip install PyQt5 and checks the installation with pip freeze.

  • Creating a Basic PyQt App (04:16-07:11): John starts coding by creating a simple "Hello World" application. He explains class creation, application initialization, and the necessity of using classes in PyQt over simpler methods, like with Tkinter.

  • Adding Widgets (08:06-10:50): John covers adding a vertical box layout and a label to the main window, explaining the process of importing necessary modules and how to set the layout.

  • User Input and Button Functionality (12:48-17:27): John adds an entry box for user input and a button. He explains how to create function callbacks to display user input from the entry box onto the label when the button is clicked.

  • Conclusion and Future Learning (17:51-18:16): John wraps up the video by encouraging viewers to subscribe and engage with the content, announcing future lessons in PyQt, and promoting his courses at Codemy.com.

Transcript

What's going on guys, John Elder here from Codemy.com, and in this video, we're going to start to learn to build GUI apps with PyQt 5 and Python. Guys, like I said, in this video, we're going to start to learn to build GUI apps with PyQt 5. But before we get started, if you like this video, want to see more like 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 Codemy.com where I have dozens of courses with hundreds of videos that teach you to code. Use coupon code U2ONE to get $30 off membership. That's all my courses, videos, and books for one time fee at just $49, which is insanely cheap. Okay. Like I said, in this video, we're going to start to learn PyQt 5. This is something people have been asking me to talk about for many, many months. So we're going to start doing that now. So
from now on, every Thursday is going to be PyQt Thursday. It doesn't really have a very good ring to it, but that's how we're going to do PyQt Thursdays. And today we're just going to install PyQt 5. And we're going to build this very basic app says, hello world. What's your name? Type in John, click the button. Hello, John. Right? So just a very basic app just to get us rolling here and get this all installed and stuff. So let's head over to our terminal. I'm using the get bash
terminal and the spline text editor as always. And I've just got the get bash terminal open. And the first thing we want to do is create a directory to hold all of our cool PyQt 5 files we're going to be building in the future. So let's go MKDIR. And I want to put this in my C drive. And I'm just going to call this PyQt 5. So now we can CD change directories to move into that directory. And so let's go C slash PyQt 5. Okay, and you can see now we're in this directory,
if we type in LS, there's nothing in there yet. So the first thing you want to do when working with any sort of Python project is create a virtual environment and virtual environment is a little walled garden where you can install all the things that you're going to be using in your project, in our case, PyQt 5. So to do that, we type in Python dash m, ven v stands for virtual environment, and then just name this thing. And I usually call this vert really doesn't matter.
And it takes a second to spin up. And boom, there we go. And we type LS, we see we have this vert directory. So we've installed our virtual environment. Now we need to turn it on. And to do that, we just type in source, and then vert scripts and then activate. And you can see boom, this little vert thing is on top of our command prompt. That means our virtual environment has been turned on, we can turn it off if we want by typing deactivate. And now it's gone again. And
we can bring it back by that same command. Now, if you are on a Mac, I think the command is source, maybe bin activate, something like that, or source, maybe slash bin activate or source, vert bin activate something like that. If you're on Mac or Linux, that should do the trick. So Okay, so now there's still nothing in here but our virtual environment directory. And the first thing we want to do is pip install PyQt five. So we just go up py qt
five. And notice capital P and capital Q. I'm not really sure if you have to do that, but I think you do. So all right, okay, so now we can go pip freeze. And we can see that pyqt five version 5.15 point two, and piety and pyqt five dash sip 12.8.1 have been installed. If these numbers are different by the time you watch this video, no big deal, just whatever the current version is, go ahead and use that should be okay. Unless there's like a major version upgrade to pyqt
six or something, but I don't see that happening in the immediate future. So we should be good to go. So okay, we've got pyqt five installed in our computer. And it was just that easy to install it super, super easy. So let's create a file, I'm going to call it hello dot pi. And we can use the touch command or we can open up our sublime text, text editor and do it that way. I'm here. So I'll just do it like this. Now we type in LS, we see we have this file, it's a blank file,
but now at least exists. So let's head over to our sublime text editor. And now in the future, we're going to use the pyqt five designer thing to make apps. But for this video, we're just going to start building out a regular pyqt five Python file, we're going to do all the code ourselves, not much to it. So it shouldn't take us very long. So let's open that file we just created. So I'm going to go to my C drive, I'm going to find that pyqt five directory we created.
There we go. And there it is hello.pi. And pops right up. So the first thing we need to do is import pyqt five. So to do that, there's lots of different ways. I'm going to do it this way, we're going to go import pyqt five dot qt widgets as qt w, right. So I do a lot of videos on Kinter and you know, with Kinter, we do things with widgets, everything's a widget. And it's sort of the same way with pyqt five, there's widgets, and we need to import the widget stuff from pyqt
five, we don't want to just import everything from pyqt, we just need the widgets right now. So we'll do that. I'm importing it as qt w, it's the sort of normal convention to do that. And you'll see why in just a minute. So okay, you can go ahead and save this and run it if we want just to make sure this imported. Okay, since this is sort of a new thing. So we can go Python, hello.py, it runs, no errors, we're good to go. So pyqt, you really want to use classes for this.
I know with a lot of my Kinter videos, we don't use classes all that often, because you can get away without using classes in Kinter. And classes tend to confuse people, especially newer coders. So I don't use them that much in Kinter. But with pyqt, you really need to. So to do that, let's create our first class here. And I'm just going to call this main window. And we want to pass in qt w dot q widget. Now I know we're pulling qt widgets here, but this is singular.
So sort of make note of that. And now we need to define underscore underscore in it, underscore underscore under if you want to say that, like a weirdo. That's what people call that Dunder double underscore, I just call it underscore underscore, and we need to call super dot underscore underscore and net underscore underscore. And I'm not going to get into a whole lot of what this stuff is just basic class initialization stuff, right? Okay,
and then we also need self doc show. Right? Now this will show our app when we start the thing. So okay, that's our main class. Now to actually run the thing, we need to create an instance of an app. So let's go app equals, and this is going to be qt w dot q application. And inside of here, we need to pass an empty Python list. Maybe we'll talk about why later, it's not super important right now. And we also need to sort of initialize our main window up here. So I'm just going to call
this MW for main window, and set this equal to main window. And then finally, to run the app, we need to execute it. So let's go app dot execute. And then this, we need to use this underscore because ex EC is a special keyword in Python. So it needs to sort of change a little bit. So we stick an underscore after it. And that should do the trick. So 15 lines of code, well, and some spaces, but not much to create a basic app. So let's run this and see if this worked.
And this should just give us a blank window, basically. So we go Python, hello.py. And when we do, we get this black blank window. Nothing much going on here. But it seems to work. So it says Python up here for the title. So let's go ahead and really quickly change the title. It's super easy to do inside of our class here, let's add a title. And to do that, we just call self dot set window title, and then just say whatever we want. So I'm gonna go Hello,
world. Alright, so notice the W and the T are both capitalized here. So let's go ahead and save this and run it. Very exciting. So we get the same blank window, but now it says Hello, world for the title bar here. And we're moving right along. So okay, now we need to set a layout. So let's set layout. So with Kinter, there's like pack and grid, there are several layouts you can use with pyqt. Five, I'm just gonna call it pyqt. I'm tired of saying five after the end. So there's
several layouts you could use with pyqt. There's a vertical box layout, a horizontal box layout, there's a grid layout, we'll look at that in a little bit. For now, we're just going to use the vertical box layout. So up and down in a boxy sort of thing, sort of like the box layout in KV, where it's just one thing after another vertical. So to do that, we go self dot set layout. And you're, you might be starting to see sort of a pattern here, the first thing is
lowercase. And the second thing is uppercase, like lowercase, uppercase, uppercase, that sort of seems to be a theme. So just sort of keep that in mind. And then we want qt w dot. Now, this is going to be a qv box layout. And that's a function. And you notice this qt w, we're doing that for everything as well here, here, right here. That's because we imported by qt five as qt w. So we have to stick this qt w in front of everything we want to do. So that's why that's
happening. So okay, that will set the layout. This is the vertical box layout. If you want the horizontal box layout, it's h box layout, right? We want the V. So let's set vertical layout, say, okay, so that's good. Now, let's create a label. Let's create a label, just a basic label. And I'm gonna call this minor score label, call it really anything you want. And this is going to equal a qt w dot q label. And here's another thing you might be noticing, we're slapping qs
in front of everything q q, q, right q, that's another sort of quirky thing of pi qt. So keep that in mind. And here's our label, we can put anything we want. Let's just put Hello, world. Woohoo. Now we've created this, we also need to sort of put it on the screen. So let's go self dot layout, and then dot add widget. And what do we want to add, we want to add my underscore label. Right? Again, notice this lowercase uppercase invention going on. So okay, let's
go ahead and save this and run it. See how this looks. And boy, I can barely grab it. Let me just do it like this. There we go. You see very tiny, it says Hello, world. And we can sort of resize this and it kind of moves around a little bit. labels don't resize especially well buttons do a little bit better. But yeah, it's, it's a label. So we've got something on the screen. So if we want to change the size of the label, this is a little bit weird. This is not really a
property of widgets. Even though the label is a widget, changing the font of the label is not it's part of the GUI system. So we need to import that if we want to use it. So let's go import pi qt five dot qt GUI. As and let's name this something that's just go I don't know qt g for qt GUI. And this is qt w for qt widgets. So now we can use this. So let's come down here and change the font size of label. So let's go and actually maybe
I'm gonna go ahead and put this first. And then we'll add it to the screen after we've changed the font. So let's go my underscore label dot set font. And again, lowercase s, uppercase F, right. And then we want to call qt g dot q font. And now what do we want to do? Well, let's use our trusty Helvetica. And let's make this like size 18 or so. And that should do the trick. So let's go ahead and save it, run it again. Boom. And now our text is bigger.
So okay, looking good. So what do we want to do now? Let's add a button. Or let's add an entry box so that we can enter a name. So first, I want to change this from Hello World to I don't know Hello World, what's your name? Right? So now let's create a box. Or let's call it an entry box. It's not what it's called. But that's what I'm going to call it. An entry box that we can type into, right. So I'm going to call this my underscore entry.
And this is going to be a qt w dot q line edit. Right, so this is a q line edit widget, I guess, not an entry box, but entry box, in my mind makes more sense. So I'm going to call it an entry box. So let's go my underscore entry dot set object name. And I'm just gonna call this name, or name field, whatever. So we need to set an object name because we need to reference this entry box later on. So okay, and we don't have to do this. But we can set the my underscore
entry, we can go set text, and we can set the text to anything we want. So if you wanted placeholder text, you could type that in there. I don't really want any placeholder text. So I'm just going to leave it blank. You don't have to add this at all. It's just kind of interesting. And it'll come up later. So I thought I'd put it in there so we could see it. So let's go ahead and slap this guy onto the screen. And we do it the same way we did with our label. So I could
just copy this. And instead of my label here, we want to put on the screen my underscore entry. So okay, let's go ahead and save this and run it. And boom, now it says Hello, world, what's your name, we've got a little box here. And if we resize this, it kind of resizes as well. It's kind of interesting the label doesn't, but the box does. So okay, that's cool. Finally, now let's create a button. So create a button. And this is a very basic app. But you know, we're getting some
stuff up on the screen. And we're seeing that, hey, this is really easy to do and really quickly to make something and that's kind of cool. So let's call this minor score button because I have all the original names. And let's go qt w dot q push button. So buttons in pi qt are called push buttons, right? And so now we can say I don't know, press me. And that'll be all for now. Let's just get this up on the screen. So to add it to the screen, as always, we just call
this guy right here. So I'm just gonna copy this again. And boom, and this is going to be my button. Okay, so let's save this and run it. Just make sure that looks good. And okay, we've got a button here, it kind of glows when we hover over it. When we press it, it kind of does a thing, we can resize and it resizes the label still doesn't, but everything else does. And okay, pretty cool. Now this doesn't actually do anything yet. So to actually do
something, it's not that tricky. We just come up to our my button here, and we slap a comma in there. And let me put this on another line. And whenever the button is clicked, we need to call clicked and set that equal to and now here, we're going to use a lambda. And we've done this sort of thing with Kinter, we call la m, b, d, a. And as soon as I do that, this looks like an uppercase L, it is not it's still lowercase. I don't know why sublime does that, but it does it here too.
So this is lowercase l, just keep that in mind. And let's have this call a function called press underscore it. And we haven't created that just yet. So we will do that now. And we need to do it below here. So let me just slap a comment in here, show the app. And this is really important, don't forget to put this self dot show on here. Because if you do, your app won't show, it'll sort of be there in the background running, but it won't be showed and the terminal will sort
of freeze. So make sure you always have that. So let's define press it. And now what we want to do is take whatever's in the box and put it on the label. So we can change the label, we did it up here to where to go right here, my entry, that set text, we can do that same thing with the label. So we could go my underscore label, dot set text. And instead of here, I'm going to create an F string. And I'm going to say hello, and then we can pass in whatever we want. Well, what do we
want, we want whatever's in the my entry box. So my entry dot text like that, I'll put an exclamation point on there. Now, we probably also want to delete whatever's in the box after somebody clicks the button. So if they want to type something else later, it'll be nice and empty for them. So we can do the same thing we did up here, just kind of copy this and paste that in. And that should do the trick. So let's clear the entry box. And here, let's add name to label.
Okay, so go ahead and save this and run it. So we get Hello, world, what's your name, we could type in john, click the button, press me boom, hello, john. And it's just that easy. So that's pi qt. In a very quick nutshell, pretty simple to do, you know, very basic things, and not a whole lot to it. And it's, you know, kind of cool. So like I said, in the future, we'll be looking at the pi qt designer and doing all that stuff. We'll also get into all of this in more detail. So Thursdays from now on
pi qt Thursdays should be a lot of fun, and looking forward to it. So 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 codemy.com where you can use coupon code you do want to get $30 on membership. So pages $49 to access all my courses over 47 courses, hundreds of videos in the PDFs of all my best selling coding books, doing over 100,000 students
learning to code just like you. My name is john Elder from codemy.com. And I'll see in the next video.

Share This Audio Transcript & Summary 📚

Create Graphical User Interfaces With Python and PyQT5 - PyQt5 GUI Thursdays #1

00:00
18:21