*COMPUTER NOISES*
What is going on everybody? Welcome to another Python tutorial video series.
This series is going to be talking about PyQt with Python.
So, PyQt is a wrapper around the Qt framework, which is a framework for creating GUIs.
We're going to be using the Qt 4 framework.
There are two frameworks. There is Qt 4 and Qt 5. Qt 5 is a slightly newer, but it
comes with a lot more dependencies, and it also has a slightly different
licensing kind of process than Qt 4 does. Qt 4 is much simpler, much more black
and white and vanilla to use, so therefore I think it would be best to go ahead and use Qt 4.
So, Qt. Well how does it differ from say Tkinter or wxPython because all
these are GUI development framework, so why should we use PyQt or learn
another framework? A lot of it's going to come down to user preference (or at least
developer preference) and Qt is a little more updated, I would say.
Back when, maybe a few years ago, I wouldn't have really said such a thing, but maybe even
wxPython would have come first because they were constantly putting updates out,
but now, I would say Qt is by far the most up-to-date framework for creating
windows, and it's going to look the best on a lot of systems, so this is going to
be the most cross platform compatible GUI that you're going to find pretty much.
And you can you can easily change the style so like with Tkinter, you can
really only change to like one style, until you need to like, you have to code
the style completely, and that takes a lot of code.
So with Qt, it comes with a bunch of styles, but also comes with a lot of
really intuitive widgets that just make sense to have pre-built in.
So obviously, there's a lot of stuff with Qt or any framework that they can't envision that
you want to do, but cutie comes pretty well packed with a lot of widgets and a
lot of widgets that are going to be cross platform and make sense
per operating system that use it on.
So anyways, now that I've sort of sold you on Qt 4, the best thing
to do is really just try them out, try out all the framework, see what suits you best
because they're all a little different, and most importantly to develop with
them is a little different.
And honestly, Qt 4 so far for like... me, if you're going to make a big application
my experience with Qt is so far has been really good actually... I like them more
than I really like Tkinter initially, but Tkinter makes it pretty hard to make
a large application, whereas Qt,
honestly Qt makes it pretty darn simple.
So let's go ahead and just jump
in, so the first thing you're going to have to do is actually acquire Qt.
So to do that, you're going to come to Riverbank Computing, the parent company,
and you're going to want to go to this link basically Software -> PyQt and download.
Now if you're on Windows,
that's what you want to do pretty much. You come down here and download the
Installer that suits you.
Ok, so this would be Qt 4.8.6 for Windows 64, and this would be for Windows 32.
Now, if you're not on Windows. Of you're on a Mac or Linux, you should be able to get
by with a simple `sudo apt-get install python-qt4`
That is all you should have to run and that should install Qt 4 for you.
Once you have those things,
drinking coffee *sip* and you're ready to move on.
So, you're gonna pretty much always have the a couple things. You're gonna pretty
much always import sys, and you're almost always
- actually, you yeah there's some scenarios where you might not import
the GUI aspect, but anyway, `from PyQt4 import QtGui`
Now, this note the caps here. They're kind of confusing, especially GUI. I want
to capitalize everything in GUI but no, it's just a capital G.
They followed PEP8, past the whole caps thing.
Anyways, `PyQt4 import QtGui` So, so this is like the
- basically `QtGui` is all the major GUI aspects
There's also `QtCore`, which we'll use kind of not too long in the future,
and then those are the two major ones that you're pretty much all his can use.
But, there are other ones so Qt comes with things like
`QtOpenGL` for including OpenGL (which is really cool).
You of course you can always
include OpenGL in your wxPython or Tkinter,
so are you fanboys don't need to jump out at me, but it's a little easier in Qt.
So you can do that.
There's `QtXml`, `QtSvg`
So XML for XML - obviously. SVG support vector graphics.
`QtSql`, `QtNetworking` and the list goes on.
So there's a lot of a kind of like built-in handling for these sorts of things.
Now, of course again, like with `QtSql`, do you have to use `QtSql` for your SQL handling?
No! But it's going to be a little more useful.
So anyways, moving on you're going to basically always have these two imports for sure.
If you're making GUIs, surley you're going to use the GUI.
lol
Anyway, but I can envision some people might just happen to really like I don't
know `QtNetwork`. You like the handling of it, so maybe you'll use `QtNetwork` with
I don't know. So, anyway you can have those two imports. Now in your application, your
GUI - whatever, you're always going to have the same kind of three major aspects
First, you're gonna have some sort of app definition, so the whole application,
so `app = QtGui.QApplication(sys.argv)` and in here, we're going to pass `sys.argv`
Now, if you're not familiar with `sys.argv`,
you can come to pythonprogramming.net,
Start learning, go to search type in `sys`
and here's what you want, and basically what is going to let you do is `argv`
is going to allow you when you could call this application from the command
line and you could pass arguments viia the command line. Now, you don't have to put
`sys.argv`, here but you have to put something. You can't go home but you
can't stay here.
Anyway, you can put in here empty list `[]` or something like, that and then you don't have to
put anything there you'll see some people do that, but `sys.argv` makes
the most sense to put there anyway but some people are just like really adamant
about not putting `sys.argv` hungry so I'll do this instead.
So anyway, you do that.
Next you're gonna have something that defines the GUI - the window,
whatever it is, so we're going to go ahead and say, for now let's say,
`window=QtGui.QWidget()
Now, one thing that I do like that
PyQt has done for us is generally when you're referencing any sort of Qt class,
they're going to go ahead and and throw a capital Q in front of it, so you can
you can look at your code really quick and no wait, Is that my method is that
Qt's method? You look at it and you just know because of the Q pretty
fast and they do that for a lot of things so I really do appreciate that
from PyQt.
So anyways, that's just a quick widget and then finally you're going to always have
`window.show()` you're always gonna have a `.show()` to your application, otherwise
what happens is everything is created in memory and then as soon as you call it
to show, then it shows because that's how Graphics work.
graphics are all created kind of the background and then when you finally
want to show, because you show because graphics take the most memory and it just makes
sense to build it first in the background and then bring it up.
So that's your application and that's basically - you're always going to use
this sort of structure.
Now, of course there's gonna be a lot more involved as we go down the road ,but
for now, this is a basic windows. So let's go ahead and save and run that with
Uh oh...lol.
Ah, blind.
Anyway, `from PyQt4 import QtGui`, drink more coffee,
and here you have your basic window. Now, we've just done this in three lines ,but
really more than three lines went into this. So this window can be resized.
It looks like a window. It looks the same as - and you can bring this window over.
It kind of follows that same kind of see-through look.
It's got the same buttons here - for anyone is curious this is for TeamViewer,
so you probably don't have that button, so don't feel like you're you're falling
behind because you don't have that button. It even resizes, it's even
got this little shadow. It maximizes. It Demaxifi...demaximizes???
lol, what do you call that? [Dubber's Note: Minimize?]
And most importantly, it can exit out. So we already have all these little
handling features that we didn't have to code it all for ourselves.
Now, the other thing I'm going to show really quick before we move on to the
next video is the title to your window and then also some the size and starting point.
So, the... Once you've defined your window, you get this nice window object.
What we can do is we can go `window.setGeometry()` and you can say the
`setGeometry()` takes four parameters.
It's the starting point so starting x, starting y
and then the dimensions, so the x and y for the width and the height.
Now, like most GUI applications, the starting points are really up here, so (0,0) is
always liked top left. Most GUI applications are like that, like Kivy is an
example of one that's from the bottom left, so it's really annoying for me
because I'm used to like everything top left,
but if you work with like HTML, or anything else, or all the other
GUI frameworks, it's top left. Anyway, so `setGeometry()`.
We're going to have a starting point (50,50) and then we'll say the window's
500 pixels wide and 300 pixels tall.
The other thing we can do really simply is `window.setWindowTitle()`, and then
you can pass in whatever window title you want. We'll call this "PyQT Tuts!"
So let's go ahead and save and run that one. That comes up, and here you have a
nice window.
It followed our rules about being being through 500 wide 300 tall, and it did
start at (50,50)
Now one thing I will just mention is you can start a window at (0,0), but (0,0) is gonna
to show us a little bit more of a fundamental thing about this window.
So, as you can see, my windows off the screen I filmed in 1080p,
it's my full monitor (at least for this monitor), but I have a monitor that sits
over here and monitor that sits up here, so above this window. So there's one of
the left - actually is one of the right to, and one above.
So, this window didn't really appear on the screen. It's on three total screens.
So, if you just had one win... one monitor,
chances are it probably appeared like this, but it tried to go up here, but your
operating system was like "(◞థ‿థ)ᴖ That's silly",
so it's stuffed it down here for you, because it has a nice little handling
for stupid choices.
So, what happened there? Well (0,0).
What is that in reference to? Well, like I was saying, you've got an application and
then you've got a window, and you might also hear this generally referred to as
"you've got a frame and then you've got a window". So the frame is what's around the
window, and then you've got the actual window which starts here. And that's why
when we said started at (0,0) this point was at (0,0),
not like this fake corner that should be here without the rounded edges.
So keep that in mind.
- It wouldn't matter. You could do (0,0) all day if you want, but it will show up
off-screen for some users, so that's kind of annoying sometimes.
Anyway, that's that with this simple application. Normally, that it's going to
be, you're going to be using object-oriented programming. You're gonna have classes on all this,
and we really did use object-oriented programming (you just didn't see it
yourself), but there's a lot of... basically everything here is an object,
and we're modifying the object here, so anyway. That's we're going to get into in
the next video is kind of converting this to the sort of structure that
you're going to tend to use when creating
an application. So that's what you guys have to look forward to. 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.
Until next time.