Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create a GUI graphical user interface in PyQt5

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to create GUI graphical user interface in PyQt5". In daily operation, I believe many people have doubts about how to create GUI graphical user interface in PyQt5. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to answer the doubts of "how to create GUI graphical user interface in PyQt5"! Next, please follow the small series to learn together!

The first GUI.

First, we create a basic GUI using pyqt5 widgets:

# coding:utf-8import sysfrom PyQt5.QtWidgets import *app = QApplication(sys.argv)win = QWidget()win.show()sys.exit(app.exec_())

Run the program and automatically generate an original window with nothing, as shown in the following figure:

In the above program, we first referenced the required modules: sys and pyqt5's QtWidgets module:

import sysfrom PyQt5.QtWidgets import *

The sys module provides functions to access variables used or maintained by the interpreter and to interact with the interpreter;

The QtWidgets module contains methods for providing a set of UI elements to create a classic desktop-style user interface that makes it easy to create window objects.

Next, we instantiate an application object QApplication(). In PyQt5, every application must instantiate a QApplication():

app = QApplication(sys.argv)

Then we create a QWidget() object, which is the base class for all graphical user interfaces in pyqt5:

win = QWidget()

Then use the show() method of the QWidget object to display the created window:

win.show()

Finally, we call the application object's exec_() method to run the program's main loop and use the sys.exit() method to ensure that the program exits perfectly.

sys.exit(app.exec_())

Second, set the window title, size and position

The GUI program created above is just an empty window. Let's add something to it, such as setting a title, changing the size of the window, etc.:

# coding:utf-8import sysfrom PyQt5.QtWidgets import *

app = QApplication(sys.argv)

win = QWidget()win.resize(450,150)win.move(0,300)win.setWindowTitle ('Zmister.com GUI Tutorial') win.show()

sys.exit(app.exec_())

In this program, we set the following three points:

Use the resize() method of the QWidget() object to set the window size to 450*150;

Use the move() method of the QWidget() object to move the window to the position x=0, y=300 on the display;

Using the setWindowTitle() method of the QWidget() object, we set a title for our window.

Running the above code results in a window that looks like this:

This is the end of learning about "how to create GUI graphical user interfaces in PyQt5", hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report