How to set the title and size of a PyQt5 window?

To set the title and size of a PyQt5 window, you can use the methods of the QMainWindow class. Here is a simple example code:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow

app = QApplication(sys.argv)

# 创建一个窗口
window = QMainWindow()
window.setWindowTitle('My PyQt5 Window')  # 设置窗口标题
window.setGeometry(100, 100, 800, 600)  # 设置窗口大小和位置

window.show()
sys.exit(app.exec_())

In the code above, a QApplication instance is created first, followed by the instantiation of a QMainWindow object. The window title is set using the setWindowTitle() method, and the window position and size are set using the setGeometry() method. Finally, the window is displayed by calling show() method, and the application’s event loop is started by calling app.exec_() method.

 

More tutorials

Creating a GraphQL API using Prisma on Silicon Cloud’s App Platform(Opens in a new browser tab)

How to add a menu bar in PyQt5?(Opens in a new browser tab)

How to use decorators to decorate functions in Python?(Opens in a new browser tab)

What is the purpose of resize in C++?(Opens in a new browser tab)

What operating systems does PyQt5 support?(Opens in a new browser tab)

Leave a Reply 0

Your email address will not be published. Required fields are marked *