布局
- 垂直布局Vertical Layout:垂直摆放,间距均匀
- 横向布局Horizontal Layout:横向摆放,间距均匀
- 网格布局Grid Layout:把布局区域根据控件分成格子
- 窗体布局Form Layout
控件
Label
【目的】显示信息
【功能】
- 发射信号,配合槽函数实现特定功能
- 修改样式表,显示图片
self.labelName.setText(u"label文")
Button
编辑信号与槽–>点击控件拖拽–>配置连接:选择信号和槽对应的函数
手动连接信号与槽
self.connect(widgets, SIGAL("signal"), func) #self.connect(控件, SIGAL("信号名称"), 槽函数)
self.connect(widgets, SIGAL("signal"), instance.methodname) #self.connect(控件, SIGAL("信号"), 类.方法)
self.connect(widgets, SIGAL("signal"), instance, SLOT("slotSignature") ) #self.connect(控件,SIGAL("信号"),另一个控件,自定义的槽)
装饰器:
# 装饰器:实现静态方法
@PyQt5.QtCode.pyqtSignature("")
选择文件夹对话框
QFileDialog.getExistingDirectory() #选取文件夹
QFileDialog.getOpenFileName() #选择文件
QFileDialog.getOpenFileNames() #选择多个文件
QFileDialog.getSaveFileName() #选择保存文件
#使用例子
my_dir = QtGui.QFileDialog.getExistingDirectory(self,
u'选择文件夹', #对话框名称
self.cwd, #默认目录
"All Files (*);;PDF Files (*.pdf);;Text Files (*.txt)" # 设置文件扩展名过滤
)
print unicode(my_dir) #打印选择路径
# @返回值:filetype
选择文件
from PyQt5.QtWidgets import QFileDialog
def selectFile(self):
ret = QFileDialog.getOpenFileName(self,
u'选择md文件', # 对话框名称
project_dir,
"All Files (*);;Markdown Files (*.md)"
)
self.fpath = ret[0]
print(self.fpath)
self.selectFileLineEdit.setText(self.fpath)
Radio
def on_radioButton_clicked(self):
print('you select r1')
if self.radioButton_5.isChecked():
print ('R5 is checked')
else:
print ('R4 is checked')
# Warming:点击时会反馈三次
QtCore.QMetaObject.connectSlotsByName(MainWindow) #这句注释掉
窗口嵌套
-
再创建一个UI文件:info.ui --> 编译成 info.py
-
再继承这个UI类
class MyDialogClass(QDialog, Ui_Dialog): pass
-
在上一个MainWindows点击事件中创建这个Dialog类
my_info = MyDialogClass() my_info.exec_()
QMessageBox
QMessageBox.information #信息框
QMessageBox.question #问答框
QMessageBox.warning #警告
QMessageBox.ctitical #危险
QMessageBox.about #关于
reply = QMessageBox.information(self, "标题", "消息", QMessageBox.Yes | QMessageBox.No)