Appearance
控件
控件
按键
PyQt5创建按钮和触发点击事件_pyqt5 clicked.connect csdn-CSDN博客
PyQt5-按钮控件使用 - zyg_100 - 博客园 (cnblogs.com)
python
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton
# 创建一个app对象, 参数是这一个程序运行的变量
app = QApplication(sys.argv)
btn = QPushButton("按钮")
widget = QWidget()
widget.resize(640, 480)
widget.setWindowTitle("Hello, PyQt5!")
# 把这一个按钮安装
btn.setParent(widget)
widget.show()
# 执行这一个app
sys.exit(app.exec())
文本Qlabel
python
label = QLabel('你好', widget)
# 设置位置x, y, w, h Geometry:几何图形
label.setGeometry(70, 70, 30, 30)
输入框QLineEdit
python
edit = QLineEdit(widget)
# 设置提示信息
edit.setPlaceholderText("请输入账号:")
edit.setGeometry(55, 20, 300, 20)
常用事件
python
self.lineEdit.returnPressed.connect(self.send)
回车事件, 可以用于回车发送信息
标题组框
QGroupBox小部件提供带有标题的组框框架。
一个组框提供一个框架,一个标题,一个快捷键,并在其内部显示其他各种小部件。键盘快捷键将键盘焦点移到组框的子窗口小部件之一。
A group box provides a frame, a title on top, a keyboard shortcut, and displays various other widgets inside itself. The keyboard shortcut moves keyboard focus to one of the group box's child widgets.
QGroupBox doesn't automatically lay out the child widgets (which are often QCheckBoxes or QRadioButtons but can be any widgets). 这一个不会自动有一个布局, 需要使用函数setLayout进行关联一个布局
python
gender_box = QGroupBox("性别")
h_layout = QHBoxLayout()
btn4 = QRadioButton("男")
btn5 = QRadioButton("女")
h_layout.addWidget(btn4)
h_layout.addWidget(btn5)
gender_box.setLayout(h_layout)
常用控制函数
设置位置
python
# x, y, w, h
edit.setGeometry(55, 20, 300, 20)
设置标题
python
widget.setWindowTitle("Hello, PyQt5!")
设置大小
python
widget.resize(640, 480)
设置窗口位置
python
widget.move(640, 480)
python
x = center_point.x()
y = center_point.y()
pos_x = int(x - widget.width()/2)
pos_y = int(y - widget.height()/2)
# print(x, y, pos_x, pos_y, widget.width()/2)
widget.move(pos_x, pos_y)
获取中心点的位置
获取窗口位置大小
python
print("方法一")
print("widget.x() = %d" % widget.x()) # 250 (窗口横坐标)
print("widget.y() = %d" % widget.y()) # 200 (窗口纵坐标)
print("widget.width() = %d" % widget.width()) # 300(工作区宽度)
print("widget.height() = %d" % widget.height()) # 240 (工作区高度)
print("方法二") # This property holds the geometry of the widget relative to its parent and excluding the window frame
print("widget.geometry().x() = %d" % widget.geometry().x()) # 250 (工作区横坐标)
print("widget.geometry().y() = %d" % widget.geometry().y()) # 222 (工作区纵坐标)
print("widget.geometry().width() = %d" % widget.geometry().width() ) # 300(工作区宽度)
print("widget.geometry().height() = %d" % widget.geometry().height()) # 240 (工作区高度)
print("方法三") # geometry of the widget relative to its parent including any window frame
print("widget.frameGeometry().x() = %d" % widget.frameGeometry().x()) # 250 (窗口横坐标)
print("widget.frameGeometry().y() = %d" % widget.frameGeometry().y()) # 200 (窗口纵坐标)
print("widget.frameGeometry().width() = %d" % widget.frameGeometry().width() ) # 300(窗口宽度)
print("widget.frameGeometry().height() = %d" % widget.frameGeometry().height()) # 262(窗口高度)
print("widget.frameGeometry().getRect() = %d" % widget.frameGeometry().getRect()) # 获取一个元组
【pyqt5】课时27.获取屏幕坐标系 获取窗口和工作区的尺寸(w,h)、坐标(x,y)_pyqt5获取窗口尺寸-CSDN博客
设置图标
python
widget.setWindowIcon(QIcon('favicon.png'))
隐藏标题栏
python
from PyQt5.QtCore import Qt
widget.setWindowFlag(Qt.FramelessWindowHint)