cool hit counter PyQt52018 Latest Tutorial_Intefrankly

PyQt52018 Latest Tutorial


This tutorial will teach you how to use PyQt5 to make a Python desktop appdesktop app. This tutorial covers everything - from the best way to configure PyQt to compiling your app and distributing it for others to use. You can use Windows, Mac or Linux systems. The prerequisite is that you need to have Python version 3.5+ installed first.

What is PyQt5?

PyQt is a third-party library for Python that enables you to use the Qt GUI framework, the QT graphical user interface framework. Qt itself is written using the C++ programming language. Using it in Python will allow you to build an application faster and without sacrificing the speed of C++.

PyQt5 refers to the latest version 5 of Qt. You may still see information about (Py)Qt4 from time to time, but this version is quite old and is no longer officially supported.

One of PyQt's interesting new competitors is Qt for Python. Their APIs are basically the same. Unlike PyQt, it uses the LGPLGNU Wide General Public License, which means it can be used for free in commercial projects. It's backed by Qt, so most likely it's the future. We use PyQt in this tutorial because it is more mature. Because these APIs are basically the same, you can easily move to Qt for Python afterwards.

Installing PyQt

The best way to manage dependencies in Python is through the virtual environmentvirtual environment. A virtual environment is really a local folder containing the library libraries needed for a particular project. The virtual environment's libraries are compared to the system's globally installed libraries, and the global libraries affect all your other projects.

To generate a virtual environment in the current folder, execute the following command.

python3-mvenv venv

This command will create a venv folder. To activate this virtual environment on a Windows system, run the following command.

call venv/scripts/activate.bat

On a Mac or Linux system, use the following command.

sourcevenv/bin/activate

The virtual environment is successfully activated when you see a (venv) prefix on the command line.

Now we will install PyQt by executing the following command.

pip installPyQt5==5.9.2

The reason we use version 5.9.2 is because not all (Py)Qt distributions are stable. And this version will guarantee no problems. Except for this nuance - congratulations! You have successfully installed PyQt5.

Create a graphical user interface

Let's start creating one of our GUI applications now! While the virtual environment is still active, start Python. We will execute the following command.

First, we tell Python to load the PyQt library with the statement.

fromPyQt5.QtWidgetsimportQApplication,QLabel

# In the next step, we instantiate a QApplication

app=QApplication([])

This is a Qt requirement: every GUI application must have a concrete instance instance. Most of the Qt components will only work if you execute that line of code above. Therefore, every (Py)Qt application you write needs to include it.

The brackets [ ] in that line of code above means that command line arguments are passed to the application. Since our application doesn't use any parameters, the brackets are left blank.

Talk is cheap, show me the code, and let's create a simple label label.

label=QLabel('Hello World!')

We then tell Qt to display this label on the screen.

label.show()

Depending on your operating system, a small window has appeared in front of you: the

The final step is to pass this component to Qt and have it "keep running the application unless the user closes it manually". This is achieved by the following command.

app.exec_()

If all of the above is as expected, then congratulations! You've just successfully built your first GUI application using Python and Qt!

components

Everything you see in a (Py)Qt application is a widget widget: buttons, labels, windows, dialogs, progress barsButtons, labels, windows, dialogs, progress bars, etc. Just like HTML elements, widgets are often inline. For example, a window can include a button, and accordingly, that button can include a label.

The following screenshots show some of Qt's most commonly used components.

From top to bottom, left to right, they are.

QLabel

QComboBox

QCheckBox

QRadioButton

QPushButton

QTableWidget

QLineEdit

QSlider

QProgressBar

disposition

Like that example above, your GUI application may consist of several different widgets. In this case, you need to tell Qt how to sort the parts. For example, you can use QTableWidget to place your widgets vertically:.

The code that implements this screenshot as.

fromPyQt5.QtWidgetsimportQApplication,QWidget,QPushButton,QVBoxLayout

app=QApplication([])

window=QWidget()

layout=QVBoxLayout()

layout.addWidget(QPushButton('Top'))

layout.addWidget(QPushButton('Bottom'))

window.setLayout(layout)

window.show()

app.exec_()

Like before, we instantiated one first. Then, we created a window window. We used the most basic one because it simply acts as a container container, and we didn't want it to contain any special behavior. Next, we create a layout layout and add two to it. Finally, we tell the window window to use this layoutlayout (and therefore its contents). Like our first application, we need to call and method at the end.

Of course.,Qt There are many others in thedisposition types( For example, usingQHBoxLayout Place the content on a linerow in)。 View OfficialQt's documentation Learn more。

Custom styles

Qt One of the features is support for Custom stylescustom styles。 (located) atQt There are many ways you can customize the look and feel of your applook and feel, This section summarizes some of the。

Built-in styles

The easiest and most brutal way to modify the appearance of your app is to just set the global style. Take a look at that previous screenshot:.

This one uses the named style. If you use styles, you will see this look like the following.

To apply a style, you need to use.

fromPyQt5.QtWidgetsimport*

app=QApplication([])

app.setStyle('Fusion')

...

How many styles are available depends on your operating system. But it's generally only available on Windows OS,, and Mac OS.

Custom Colors

If you like a style but want to change its color (for example to a black theme), then you can use QPalette and, for example:

fromPyQt5.QtCoreimportQt

fromPyQt5.QtGuiimportQPalette

fromPyQt5.QtWidgetsimportQApplication,QPushButton

app=QApplication([])

app.setStyle('Fusion')

palette=QPalette()

palette.setColor(QPalette.ButtonText,Qt.red)

app.setPalette(palette)

button=QPushButton('Hello World')

button.show()

app.exec_()

It will change the color of the text in the button to red:.

To modify the style to black, the code is as follows.

fromPyQt5.QtCoreimportQt

fromPyQt5.QtGuiimportQPalette,QColor

fromPyQt5.QtWidgetsimportQApplication,QPushButton

app=QApplication([])

app.setStyle('Fusion')

palette=QPalette()

palette.setColor(QPalette.ButtonText,Qt.red)

app.setPalette(palette)

button=QPushButton('Hello World')

app.setStyle("Fusion")

dark_palette=QPalette()

dark_palette.setColor(QPalette.Window,QColor(53,53,53))

dark_palette.setColor(QPalette.WindowText,Qt.white)

dark_palette.setColor(QPalette.Base,QColor(25,25,25))

dark_palette.setColor(QPalette.alternateBase,QColor(53,53,53))

dark_palette.setColor(QPalette.ToolTipBase,Qt.white)

dark_palette.setColor(QPalette.ToolTipText,Qt.white)

dark_palette.setColor(QPalette.Text,Qt.white)

dark_palette.setColor(QPalette.Button,QColor(53,53,53))

dark_palette.setColor(QPalette.ButtonText,Qt.white)

dark_palette.setColor(QPalette.BrightText,Qt.red)

dark_palette.setColor(QPalette.Link,QColor(42,130,218))

dark_palette.setColor(QPalette.Highlight,QColor(42,130,218))

dark_palette.setColor(QPalette.HighlightedText,Qt.black)

app.setPalette(dark_palette)

app.setStyleSheet("QToolTip { color: #ffffff; background-color: #2a82da; border: 1px solid white; }")

button.show()

app.exec_()

Once run, you will see the following.

Note that the code for this black theme was originally placed on the author's github above https://gist.github.com/mstuttgart/37c0e6d8f67a0611674e08294f3daef7. The translator copied it out and ran it to generate screenshots. So the reason you see this window style different from the previous one is because the translator is using windows 10, which is different from the Mac the author is using. (Compared to Mac and Linux, windows 10 really doesn't look that good)

style sheet

In addition to that method above, you can also modify the appearance of the app through style sheets style sheets. This is pretty much the same as CSS. We can use it in the following example to add some spacingspacing.

fromPyQt5.QtWidgetsimportQApplication,QPushButton

app=QApplication([])

app.setStyleSheet("QPushButton { margin: 10ex; }")

button=QPushButton('Hello World')

button.show()

app.exec_()


Recommended>>
1、Anatomy Redis transaction implementation principles
2、AI loss determination black technology has come industry have layout to push insurance technology innovation
3、Lao Mouzi plays cool eightminute recreation of Beijing Welcome in Korea tomorrow
4、KPMG releases 2017 list of Chinas leading fintech 50 companies Chongqingbased consumer finance company named
5、Implementation of centralized privilege management based on domain accounts and Siemens simaticlogon

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号