[PyQt5] 1 布局+控件+信号与槽

布局

  1. 垂直布局Vertical Layout:垂直摆放,间距均匀
  2. 横向布局Horizontal Layout:横向摆放,间距均匀
  3. 网格布局Grid Layout:把布局区域根据控件分成格子
  4. 窗体布局Form Layout

控件

Label

【目的】显示信息
【功能】

  1. 发射信号,配合槽函数实现特定功能
  2. 修改样式表,显示图片
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) #这句注释掉

窗口嵌套

  1. 再创建一个UI文件:info.ui --> 编译成 info.py

  2. 再继承这个UI类

     class MyDialogClass(QDialog, Ui_Dialog):
     	pass
    
  3. 在上一个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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

geodoer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值