tooltip提示可以使用html
btn = QPushButton(QIcon('./favicon.ico'),"demo",self)
btn.resize(70,30)
btn.move(10,10)
btn.setToolTip('<p style="color:red">点击这里猜数字</p>')
设置窗口图标
w=QWidget()
w.setWindowIcon(QIcon('./favicon.ico'))
组件widget的大小与位置
setGeometry(300, 300, 300, 220) #(x,y,w,h)
退出事件与按钮
def closeEvent(self, event):
reply = QMessageBox.question(self, '确认', '确认退出吗', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
监听键盘按键
def keyPressEvent(self, e):
if e.key() == Qt.Key_Up:
elif e.key() == Qt.Key_Down:
elif e.key() == Qt.Key_Left:
else:
鼠标跟踪setMouseTracking
class Example(QWidget):
distance_from_center = 0
def __init__(self):
super().__init__()
self.initUI()
self.setMouseTracking(True)
有了这句self.setMouseTracking(True),不需要按住鼠标左键,只需要移动鼠标,mouseMoveEvent(self, event)就会被触发。
update()什么作用?
def mouseMoveEvent(self, event):
...
self.update()
paintEvent什么作用?
def paintEvent(self, event):
布局
hbox = QHBoxLayout()
hbox.addStretch(1) # 把按钮推到右边.QSpacerItem=1
hbox.addWidget(bt1)
hbox.addWidget(bt2)
hbox.addWidget(bt3)
vbox = QVBoxLayout()
vbox.addStretch(1) # 把按钮推到底部
vbox.addLayout(hbox)
addStretch
hbox = QHBoxLayout()
hbox.addStretch(1) #增加伸缩量 QSpacerItem=1
hbox.addWidget(bt1)
hbox.addStretch(1)#增加伸缩量
hbox.addWidget(bt2)
hbox.addStretch(1)#增加伸缩量
hbox.addWidget(bt3)
hbox.addStretch(6)#增加伸缩量 QSpacerItem=6
上面代码的意思就是将button以外的空白地方按设定的比例等分为9份并按照设定的顺序放入布局器中。
这行代码的中括号没看懂。slider。
self.sld1.valueChanged[int].connect(self.changevalue)
时间¶
>>> from PyQt5.QtCore import QDate,QTime,QDateTime,Qt
>>> now = QDate.currentDate()
>>> now
PyQt5.QtCore.QDate(2022, 11, 7)
>>> now.toString()
'周一 十一月 7 2022'
>>> now.toString(Qt.ISODate)
'2022-11-07'
>>> Qt.ISODate
1
>>> now.toString(0)
'周一 十一月 7 2022'
>>> now.toString(2)
'2022/11/7'
>>> now.toString(Qt.DefaultLocaleLongDate)
'2022年11月7日'
>>> Qt.DefaultLocaleLongDate
7
>>> datetime = QDateTime.currentDateTime()
>>> datetime
PyQt5.QtCore.QDateTime(2022, 11, 7, 20, 48, 26, 12)
>>> datetime.toString()
'周一 十一月 7 20:48:26 2022'
>>> time = QTime.currentTime()
>>> time
PyQt5.QtCore.QTime(20, 49, 1, 51)
>>> time.toString()
'20:49:01'
#UTC时间。比实际-8小时
>>> now = QDateTime.currentDateTime()
>>> now
PyQt5.QtCore.QDateTime(2022, 11, 7, 20, 50, 33, 326)
>>> now.toUTC()
PyQt5.QtCore.QDateTime(2022, 11, 7, 12, 50, 33, 326, PyQt5.QtCore.Qt.TimeSpec(1))
>>> now.toUTC().toString(Qt.ISODate)
'2022-11-07T12:50:33Z'
信号和槽¶
The constructor of Signal
takes a tuple or a list of Python types and C types:
clicked = Signal(Qt.MouseButton)
signal1 = Signal(int) # Python types
signal2 = Signal(QUrl) # Qt Types
signal3 = Signal(int, str, int) # more than one type
signal4 = Signal((float,), (QDate,)) # optional types
名字
# TODO
signal5 = Signal(int, name='rangeChanged')
# ...
rangeChanged.emit(...)
signal5 = Signal(int)
# ...
signal5.emit(...)
Slot()
also accepts a name
and a result
keyword. The result
keyword defines the type that will be returned and can be a C or Python type.
@Slot(str)
def slot_function(self, s):
...