PYQT5常用控件
一、利用QT设计师上面的旋钮和显示框来组合,用旋钮操作,用显示框显示数字
旋钮的信号和槽选择如图,函数名称随便写
需要注意的地方就是,写曹函数的时候,带一个参数,名称随意,然后赋值给显示器lcdNumber,代码如下:
def valueC(self,aa): print(aa) self.lcdNumber.display(aa)
效果如下:
二、按钮和输入框,搞两个按钮一个输入框举例:第一个框输入字符串,点击按钮,清空第一个输入框,显示到第二个数据框里面去,只需要一个按钮点击信号,来对输入框清空和赋值操作
信号和槽函数如图;
代码如下:
def but_one(self): self.lineEdit_2.setText(self.lineEdit.text()) self.lineEdit.setText('')
效果如下:
三、各种提示框:
搞N个按钮,各个连接不同的信号和槽函数名称,转py文件之后,导入包
from PyQt5.Qt import QMessageBox
在按钮槽函数写对应的代码就是对应的弹框
def but_one(self):
# QMessageBox.information(self.pushButton,'通知','我是通知对话框')
mybut = QMessageBox.question(self.pushButton,'询问','真的要这么做吗?')
if mybut == QMessageBox.Yes:
print('我选自了是')
else:
print('我选择了否,或者我关掉了')
# QMessageBox.warning(self.pushButton,'警告','我是警告对话框!')
# QMessageBox.critical(self.pushButton,'严重警告','我是严重警告对话框!')
# QMessageBox.about(self.pushButton,'消息','我是消息对话框')
效果:
四、弹出输入框:
前提一样,在次导入from PyQt5.QtWidgets import QInputDialog
# my_str,ok = QInputDialog.getText(self.pushButton,'标题','我是字符串输入框',QLineEdit.Normal,'我是默认值') # my_str,ok = QInputDialog.getInt(self.pushButton,'标题','我是整形数据输入框',30,0,100) my_list = ['Python','Java','C','C#'] my_str, ok = QInputDialog.getItem(self.pushButton, '标题', '我是下拉框',my_list) print(my_str,ok)
效果:
五、弹框多个输入框:弹多个对话框的时候就需要重新画个界面来充当弹出的对话框了
首先画一个这样的界面,提交和取消搞好信号曹
转py之后要改变头部代码。参考https://www.cnblogs.com/Mr-Simple001/p/15907930.html
然后要搞一个新的文件来加载主界面和弹窗界面,代码如下:
汇总代码
# *_*coding:utf-8 *_* # @Author : zyb from PyQt5.Qt import * from PyQt5 import QtWidgets from bash_ui.inof import Ui_Dialog from bash_ui.test import Ui_Form import sys class Main_business(): #初始化两个界面 def __init__(self): self.app = QApplication(sys.argv) self.Ui_Form = Ui_Form() self.Dialog = QtWidgets.QDialog() self.ui = Ui_Dialog() self.ui.setupUi(self.Dialog) #登录界面的逻辑 def click_tan(self): self.Dialog.exec_() #弹窗按钮逻辑 def tijiao(self): print(self.ui.myname.text(),self.ui.myage.text()) self.ui.myage.setText('') self.ui.myname.setText('') self.Dialog.close() def quxiao(self): self.ui.myage.setText('') self.ui.myname.setText('') self.Dialog.close() #主函数,和按钮调用 def main(self): self.Ui_Form.bt_tan.connect(self.click_tan) self.Ui_Form.show() self.ui.btn_tijiao.connect(self.tijiao) self.ui.btn_quxiao.connect(self.quxiao) sys.exit(self.app.exec_()) if __name__ == '__main__': mymain = Main_business() mymain.main()
主界面代码:
# -*- coding: utf-8 -*- from PyQt5 import QtCore,QtWidgets from PyQt5.Qt import * class Ui_Form(QtWidgets.QMainWindow): bt_tan = pyqtSignal() def __init__(self, parent=None, *args, **kwargs): super(Ui_Form, self).__init__(parent, *args, **kwargs) self.setupUi(self) def setupUi(self, Form): Form.setObjectName("Form") Form.resize(540, 373) self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setGeometry(QtCore.QRect(40, 40, 75, 23)) self.pushButton.setObjectName("pushButton") self.retranslateUi(Form) self.pushButton.clicked.connect(self.but_one) QtCore.QMetaObject.connectSlotsByName(Form) def but_one(self): self.bt_tan.emit() def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.pushButton.setText(_translate("Form", "提示弹框"))
弹窗代码:
# -*- coding: utf-8 -*- from PyQt5 import QtCore,QtWidgets from PyQt5.QtCore import pyqtSignal class Ui_Dialog(QtWidgets.QMainWindow): btn_tijiao = pyqtSignal() btn_quxiao = pyqtSignal() def __init__(self, parent=None, *args, **kwargs): super(Ui_Dialog, self).__init__(parent, *args, **kwargs) self.setupUi(self) def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(400, 300) self.label = QtWidgets.QLabel(Dialog) self.label.setGeometry(QtCore.QRect(60, 60, 54, 12)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(Dialog) self.label_2.setGeometry(QtCore.QRect(60, 110, 54, 12)) self.label_2.setObjectName("label_2") self.myname = QtWidgets.QLineEdit(Dialog) self.myname.setGeometry(QtCore.QRect(100, 60, 113, 20)) self.myname.setObjectName("myname") self.myage = QtWidgets.QLineEdit(Dialog) self.myage.setGeometry(QtCore.QRect(100, 100, 113, 20)) self.myage.setObjectName("myage") self.pushButton = QtWidgets.QPushButton(Dialog) self.pushButton.setGeometry(QtCore.QRect(100, 200, 75, 23)) self.pushButton.setObjectName("pushButton") self.pushButton_2 = QtWidgets.QPushButton(Dialog) self.pushButton_2.setGeometry(QtCore.QRect(220, 200, 75, 23)) self.pushButton_2.setObjectName("pushButton_2") self.retranslateUi(Dialog) self.pushButton.clicked.connect(self.tijiao) self.pushButton_2.clicked.connect(self.quxiao) QtCore.QMetaObject.connectSlotsByName(Dialog) def tijiao(self): self.btn_tijiao.emit() def quxiao(self): self.btn_quxiao.emit() def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog")) self.label.setText(_translate("Dialog", "姓名:")) self.label_2.setText(_translate("Dialog", "年龄:")) self.pushButton.setText(_translate("Dialog", "提交")) self.pushButton_2.setText(_translate("Dialog", "取消"))
效果:
六、点击按钮打开浏览器百度网址:
转py之后导入import webbrowser
槽函数如下:
webbrowser.open('https://www.baidu.com')
七、菜单:新建选择Main Window
随便加几个菜单
连接槽函数使用triggered
self.myopen.triggered.connect(self.my_open)