table of Contents
Window Type Description
- Three classes can be used to create a window, can be used directly, it can also be inherited after use
- QMainWindow: The main window GUI program, including the menu bar, toolbar, status bar, title bar, is the most common form of window
- QWidget: base class for all user interface objects, all windows or controls directly or indirectly inherits from QWidget class, may be used as top-level window, it may also be embedded into other windows
- QDialog: dialog window of the base class, the main task to perform short-term, or interact with the user, it can be modal or a non-modal
Display the status window
.showFullScreen()
full-screen display.showMaximized()
Zoom.showMinimized()
Minimum Show.hide()
Hidden windows are also suitable for use with most components.show()
Display window, also suitable for use with most components.windowState()
Returns the window stateisMinimized()
If the window is minimized, the return value is True; otherwise, False;isMaximized()
If the window is maximized, the return value is True; otherwise, False;isFullScreen()
If the window full-screen display, the return value is True; otherwise, False;isActiveWindow()
If it is the active window, the return value is True; otherwise, False ;;
QMainWindow
- QMainWindow is a subclass of QWidget
- It contains
状态栏
,工具栏
and菜单栏
the classic application framework - If the window is the root inherited such general
You can use absolute layout, you can not use the box layout
Status Bar
To display the state information of the serial member of the window shown on the left lowermost
Menu Bar
Drop-down menu
toolbar
Toolbar provides a quick way to perform the most frequently used commands
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, qApp
from PyQt5.QtGui import QIcon
class FirstWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUi()
def initUi(self):
self.setGeometry(300, 300, 200, 200)
self.setWindowTitle("第一个窗口")
#给生成的二级菜单添加一个内容,绑定图标,文本,绑定类(这个按钮属于哪个控件)
exit_menu = QAction(QIcon(r"1.ico"), "退出", self)
#绑定一个快捷键
exit_menu.setShortcut("Ctrl+Q")
#生成一个菜单按钮链接事件,执行退出命令
exit_menu.triggered.connect(qApp.quit)
copy_menu = QAction(QIcon(r"2.ico"), "复制", self)
copy_menu.setShortcut("Ctrl+C")
version_menu = QAction(QIcon(r"3.ico"), "版本", self)
#生成菜单栏
menubar = self.menuBar()
#生成一个二级菜单,并命名
file_bar = menubar.addMenu("文件")
#给生成的二级菜单添加上面代码生成的二级内容
file_bar.addAction(exit_menu)
edit_bar = menubar.addMenu("编辑")
edit_bar.addAction(copy_menu)
help_bar = menubar.addMenu("帮助")
help_bar.addAction(version_menu)
#生成一个工具
exit_tool = QAction(QIcon(r"4.ico"), "退出", self)
exit_tool.triggered.connect(qApp.quit)
#生成一个工具栏,并给整个工具栏命名
toolbar = self.addToolBar("工具栏")
#添加工具
toolbar.addAction(exit_tool)
#生成每个按钮的状态栏信息
exit_menu.setStatusTip("退出程序")
copy_menu.setStatusTip("复制")
version_menu.setStatusTip("版本")
exit_tool.setStatusTip("退出程序")
#生成状态栏
self.statusBar()
def mainGui():
app = QApplication(sys.argv)
#设置显示风格Fusion及windows
app.setStyle('Fusion')
firstwindow = FirstWindow()
firstwindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
mainGui()
QWidget
- All that can be displayed in all PyQt5 QWidget subclasses
1. Control .setToolTip ( "bubble content display")
- Control may be provided a bubble content display
- Been
from PyQt5.QtWidgets import QToolTip
imported - By
QToolTip.setFont(QFont('SansSerif', 30))
setting the font and size of text bubbles
2. Get Screen Size
from PyQt5.QtWidgets import QDesktopWidget
#获得屏幕尺寸
screen = QDesktopWidget().screenGeometry()
#获得屏幕的宽度
width = screen.width()
#获得屏幕的高度
height = screen.height()
3. Access to the Properties window
from PyQt5.QtWidgets import QWidget
app = QApplication(sys.argv)
firstwindow = QWidget()
print(firstwindow.width()) #获取窗口宽度
print(firstwindow.height()) #获取窗口高度
print(firstwindow.x()) #获取窗口左上角的X坐标
print(firstwindow.y()) #获取窗口左上角的Y坐标
4. Add a small window to an icon
from PyQt5.QtGui import QIcon
firstwindow = QWidget()
firstwindow.setWindowIcon(QIcon(r"图标路径"))
The message window
#关闭窗口时弹出确认窗口
from PyQt5.QtWidgets import QMessageBox
def closeEvent(self, event):
reply = QMessageBox.question(self,"确认退出","你确定要退出么?",QMessageBoxYes,
QMessageBoxNo)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
Targeting
- Targeting into absolute layout, box layout, layout grid
1. Absolute positioning mode
- QMainWindow () and QWidget () The window class can use absolute positioning mode
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
class FirstWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUi()
def initUi(self):
self.setWindowTitle("第一窗口")
self.setGeometry(300, 300, 400, 400)
QLabel('Couldn\'t', self).move(15, 10)
QLabel('care', self).move(35, 40)
QLabel('less', self).move(55, 65)
QLabel('and', self).move(115, 65)
QLabel('then', self).move(135, 45)
QLabel('you', self).move(115, 25)
QLabel('kiss', self).move(145, 10)
QLabel('me', self).move(215, 10)
def guiMainLoop():
app = QApplication(sys.argv)
firstwindow = FirstWindow()
firstwindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
guiMainLoop()
2. Box Layout
- The QMainWindow () can not use the class window box layout, QWidget () class layout window box can be used
- After the can () member class center window The QMainWindow, setCentralWidget method by adding the QWidget () window class, a box layout
- QHBoxLayout () horizontal layout
- QVBoxLayout, () vertical layout
- .addWidget () member Add
- .addStretch () to add spacing element
- .setLayout () to generate the overall layout
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QVBoxLayout
class FirstWindow(QWidget):
def __init__(self):
super().__init__()
self.initUi()
def initUi(self):
self.setWindowTitle("第一窗口")
self.setGeometry(300, 300, 400, 400)
self.b1 = QPushButton("按钮1")
self.b2 = QPushButton("按钮2")
#建立一个水平布局
self.h_box = QHBoxLayout()
#从左往右添加一个间隔,两个按钮
self.h_box.addStretch(1)
self.h_box.addWidget(self.b1)
self.h_box.addWidget(self.b2)
#建立一个垂直布局
self.v_box = QVBoxLayout()
#从上往下添加一个间隔,上面的水平布局
self.v_box.addStretch(1)
self.v_box.addLayout(self.h_box)
#生成总体布局
self.setLayout(self.v_box)
def guiMainLoop():
app = QApplication(sys.argv)
firstwindow = FirstWindow()
firstwindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
guiMainLoop()
3. Grid Layout
- The QMainWindow () can not use the class window box layout, QWidget () class layout window box can be used
- After the can () member class center window The QMainWindow, setCentralWidget method by adding the QWidget () window class, using the grid layout
- The layout window space is divided into a number of rows and columns
- QGridLayout () Creates a grid layout
- .addWidget () member in the grid was added
- .setLayout () to generate the overall layout
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QGridLayout, QMainWindow
class FirstWindow(QWidget):
def __init__(self):
super().__init__()
self.initUi()
def initUi(self):
self.setWindowTitle("第一窗口")
self.setGeometry(300, 300, 400, 400)
self.b1 = QPushButton("按钮1")
self.b2 = QPushButton("按钮2")
self.b3 = QPushButton("按钮3")
self.b4 = QPushButton("按钮4")
self.b5 = QPushButton("按钮5")
self.b6 = QPushButton("按钮6")
self.b7 = QPushButton("按钮7")
self.b8 = QPushButton("按钮8")
self.b9 = QPushButton("按钮9")
#实例化一个网格布局
self.grid = QGridLayout()
#给网格布局添加部件
self.grid.addWidget(self.b1, 0, 0)
self.grid.addWidget(self.b2, 0, 1)
self.grid.addWidget(self.b3, 0, 2)
self.grid.addWidget(self.b4, 0, 3)
self.grid.addWidget(self.b5, 1, 0)
self.grid.addWidget(self.b6, 1, 1)
self.grid.addWidget(self.b7, 1, 2)
self.grid.addWidget(self.b8, 2, 0)
#参数为 行,列,行跨度,列跨度
self.grid.addWidget(self.b9, 2, 1, 5, 2)
#生成总体布局
self.setLayout(self.grid)
def guiMainLoop():
app = QApplication(sys.argv)
firstwindow = FirstWindow()
firstwindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
guiMainLoop()