class QmyDialog(QDialog): ''' text()是获取文本内容 setText()是设置文本内容 value()是获取数值int或者float型 setValue()是赋值 ''' def __init__(self, parent=None): super().__init__(parent) #调用父类构造函数,创建窗体 self.ui = Ui_Dialog() #创建Ui对象 self.ui.setupUi(self) #构造UI ##==========自定义功能函数========== ##==========事件处理函数=========== ##==========由connectSlotsByName()自动关联的槽函数==== def on_pushButton_clicked(self): num = int(self.ui.lineEdit.text()) price = float(self.ui.lineEdit_2.text()) total = num * price self.ui.lineEdit_3.setText('{}'.format(total)) @pyqtSlot(int) def on_spinBox_valueChanged(self, count): price = self.ui.doubleSpinBox.value() #doubleSpinBox是单价spin框的名称 self.ui.doubleSpinBox_2.setValue(count * price) #doubleSpinBox_2是自动计价的显示框 @pyqtSlot(float) def on_doubleSpinBox_valueChanged(self, price): count = self.ui.spinBox.value() #spinBox是数量的spin框的名称 self.ui.doubleSpinBox_2.setValue(count * price)
class QmyDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) #调用父类构造函数,创建窗体 self.ui = Ui_Dialog() #创建UI对象 self.ui.setupUi(self) #构造UI界面 pass ## 由connectSlotsByName() 自动与组件的信号连接的槽函数 ## @pyqtSlot() def on_btnClear_clicked(self): pass ## @pyqtSlot() def on_chkBoxBold_toggled(self, checked): #"Bold" pass ## @pyqtSlot() def on_chkBoxUnder_clicked(self): #"Underline" pass @pyqtSlot(bool) #修饰符指定参数类型,用于overload型的信号 def on_chkBoxItalic_clicked(self, checked): pass ## 自定义槽函数 def do_setTextColor(self): pass
def __init__(self, parent=None): super().__init__(parent) self.ui = Ui_Dialog() self.ui.setupUi(self) self.ui.radioButton_black.clicked.connect(self.do_setTextColor) self.ui.radioButton_red.clicked.connect(self.do_setTextColor) self.ui.radioButton_blue.clicked.connect(self.do_setTextColor)
def __init__(self): super().__init__() #调用父类构造函数,创建窗体 self.ui = Ui_Dialog() #创建UI对象 self.ui.setupUi(self) #构造UI #将clicked()信号与统一个槽函数do_set_TextColor()关联 self.ui.radioBlack.clicked.connect(self.do_set_TextColor) self.ui.radioRed.clicked.connect(self.do_set_TextColor) self.ui.radioBlue.clicked.connect(self.do_set_TextColor)
def __init__(self, parent=None): super().__init__(parent) # 调用父类构造函数,创建窗体 self.ui=Ui_Dialog() #创建UI对象 self.ui.setupUi(self) #构造UI界面 self.ui.radioBlack.clicked.connect(self.do_setTextColor) self.ui.radioRed.clicked.connect(self.do_setTextColor) self.ui.radioBlue.clicked.connect(self.do_setTextColor)
class QMyDialog(QDialog): def __init__(self, parent=None): # 调用父类构造函数,创建窗口 super().__init__(parent) # 创建UI对象 self.ui = Ui_Dialog() # 构造UI self.ui.setupUi(self) self.ui.radioBlue.clicked.connect(self.do_setTextColor) self.ui.radioRed.clicked.connect(self.do_setTextColor) self.ui.radioBlack.clicked.connect(self.do_setTextColor) def on_btnClear_clicked(self): self.ui.textEdit.clear() pass def on_chkBoxBold_toggled(self, checked): font = self.ui.textEdit.font() font.setBold(checked) self.ui.textEdit.setFont(font) pass def on_chkBoxUnder_toggled(self, checked): font = self.ui.textEdit.font() font.setUnderline(checked) self.ui.textEdit.setFont(font) pass #def on_chkBoxItelic_clicked(self): # checked = self.ui.chkBoxItelic.isChecked() # font = self.ui.textEdit.font() # font.setItalic(checked) # self.ui.textEdit.setFont(font) # pass @pyqtSlot(bool) def on_chkBoxItelic_clicked(self, checked): font = self.ui.textEdit.font() font.setItalic(checked) self.ui.textEdit.setFont(font) pass def do_setTextColor(self): plet = self.ui.textEdit.palette() if (self.ui.radioBlack.isChecked()): plet.setColor(QPalette.Text, Qt.black) elif (self.ui.radioBlue.isChecked()): plet.setColor(QPalette.Text, Qt.blue) elif (self.ui.radioRed.isChecked()): plet.setColor(QPalette.Text, Qt.red) self.ui.textEdit.setPalette(plet) pass pass
class QmyDialog(QDialog): def __init__(self): super().__init__() #调用父类构造函数,创建窗体 self.ui = Ui_Dialog() #创建UI对象 self.ui.setupUi(self) #构造UI #将clicked()信号与统一个槽函数do_set_TextColor()关联 self.ui.radioBlack.clicked.connect(self.do_set_TextColor) self.ui.radioRed.clicked.connect(self.do_set_TextColor) self.ui.radioBlue.clicked.connect(self.do_set_TextColor) def on_btnClear_clicked(self): #清空 self.ui.TextEdit.clear() #注意命名要和ui_Dialog内的命名一致 def on_checkBoxBold_toggled(self, checked): #加粗,checked是勾选状态是一个bool型 print(checked) font = self.ui.TextEdit.font() # print(font) font.setBold(checked) self.ui.TextEdit.setFont(font) def on_checkBoxUnder_clicked(self): #下划线 checked = self.ui.checkBoxUnder.isChecked() #获取勾选状态,bool型 # print(checked) font = self.ui.TextEdit.font() font.setUnderline(checked) self.ui.TextEdit.setFont(font) @QtCore.pyqtSlot(bool) #书上未说明要导入QtCore #该修饰符的作用是将函数的参数类型声明清楚,让函数明确使用的是哪一种信号(clicked()或者是clicked(bool),这里用于区分overload型信号的选择 def on_checkBoxItalic_clicked(self, checked): #添加新功能 # print(checked) font = self.ui.TextEdit.font() # print(font) font.setItalic(checked) # print(12) self.ui.TextEdit.setFont(font) # print(13) def do_set_TextColor(self): #添加自定义槽函数 plet = self.ui.TextEdit.palette() #获取调色板palette() if (self.ui.radioBlack.isChecked()): #isChecked是否选取 plet.setColor(QPalette.Text, Qt.black) #black elif (self.ui.radioRed.isChecked()): plet.setColor(QPalette.Text, Qt.red) elif (self.ui.radioBlue.isChecked()): plet.setColor(QPalette.Text, Qt.blue) self.ui.TextEdit.setPalette(plet) #设置颜色
class QmyDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) #调用父类构造函数,创建窗体 self.ui = Ui_Dialog() #创建UI对象 self.ui.setupUi(self) #构造UI界面 self.ui.radioBlack.clicked.connect(self.do_setTextColor) self.ui.radioRed.clicked.connect(self.do_setTextColor) self.ui.radioBlue.clicked.connect(self.do_setTextColor) ## 由connectSlotsByName() 自动与组件的信号连接的槽函数 ## @pyqtSlot() def on_btnClear_clicked(self): #"清空"按钮 self.ui.textEdit.clear() ## @pyqtSlot() def on_chkBoxBold_toggled(self, checked): #"Bold"复选框 font = self.ui.textEdit.font() font.setBold(checked) #函数参数checked表示了勾选状态 self.ui.textEdit.setFont(font) ## @pyqtSlot() def on_chkBoxUnder_clicked(self): #"Underline"复选框 checked = self.ui.chkBoxUnder.isChecked() #读取勾选状态 font = self.ui.textEdit.font() font.setUnderline(checked) self.ui.textEdit.setFont(font) @pyqtSlot(bool) #修饰符指定参数类型,用于overload型的信号 def on_chkBoxItalic_clicked(self, checked): #"Italic"复选框 font = self.ui.textEdit.font() font.setItalic(checked) self.ui.textEdit.setFont(font) ## 自定义槽函数 def do_setTextColor(self): plet = self.ui.textEdit.palette() #获取 palette if (self.ui.radioBlack.isChecked()): plet.setColor(QPalette.Text, Qt.black) #black elif (self.ui.radioRed.isChecked()): plet.setColor(QPalette.Text, Qt.red) #red elif (self.ui.radioBlue.isChecked()): plet.setColor(QPalette.Text, Qt.blue) #blue self.ui.textEdit.setPalette(plet) #设置palette
class QnewDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) self.ui = Ui_Dialog() self.ui.setupUi(self) self.ui.radioButton_black.clicked.connect(self.do_setTextColor) self.ui.radioButton_red.clicked.connect(self.do_setTextColor) self.ui.radioButton_blue.clicked.connect(self.do_setTextColor) def on_pushButton_clear_clicked(self): self.ui.textEdit.clear() def on_checkBox_bold_toggled(self, checked): font = self.ui.textEdit.font() font.setBold(checked) #表示選取狀態 self.ui.textEdit.setFont(font) def on_checkBox_underline_clicked(self): checked = self.ui.checkBox_underline.isChecked() font = self.ui.textEdit.font() font.setUnderline(checked) self.ui.textEdit.setFont(font) @pyqtSlot(bool) def on_checkBox_italic_clicked(self, checked): ##def on_checkBox_italic_clicked(self): ##checked=self.ui.checkBox_italic.isChecked() font = self.ui.textEdit.font() font.setItalic(checked) self.ui.textEdit.setFont(font) def do_setTextColor(self): plet = self.ui.textEdit.palette() if (self.ui.radioButton_black.isChecked()): plet.setColor(QPalette.Text, Qt.black) elif (self.ui.radioButton_red.isChecked()): plet.setColor(QPalette.Text, Qt.red) elif (self.ui.radioButton_blue.isChecked()): plet.setColor(QPalette.Text, Qt.blue) self.ui.textEdit.setPalette(plet) def GetOutCome(self): print(self.ui.textEdit.toPlainText())
class QmyDialog(QDialog): def __init__(self, parrent=None): super().__init__(parrent) self.ui = Ui_Dialog() self.ui.setupUi(self) self.ui.radioBlack.clicked.connect(self.do_setTextColor) self.ui.radioRed.clicked.connect(self.do_setTextColor) self.ui.radioBlue.clicked.connect(self.do_setTextColor) def on_btnClear_clicked(self): self.ui.textEdit.clear() def on_chkBoxBold_toggled(self, checked): font = self.ui.textEdit.font() font.setBold(checked) self.ui.textEdit.setFont(font) def on_chkBoxItalic_toggled(self, checked): font = self.ui.textEdit.font() font.setItalic(checked) self.ui.textEdit.setFont(font) def on_chkBoxUnder_toggled(self, checked): font = self.ui.textEdit.font() font.setUnderline(checked) self.ui.textEdit.setFont(font) def do_setTextColor(self): plet = self.ui.textEdit.palette() # 获取调色板palette if (self.ui.radioBlack.isChecked()): plet.setColor(QPalette.Text, Qt.black) elif (self.ui.radioRed.isChecked()): plet.setColor(QPalette.Text, Qt.red) elif (self.ui.radioBlue.isChecked()): plet.setColor(QPalette.Text, Qt.blue) self.ui.textEdit.setPalette(plet)
def __init__(self, parent=None): super().__init__(parent) #调用父类构造函数,创建窗体 self.ui = Ui_Dialog() #创建UI对象 self.ui.setupUi(self) #构造UI界面 pass
class QmyDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) #调用父类构造函数,创建窗体 self.ui=Ui_Dialog() #创建UI对象 self.ui.setupUi(self) #构造UI界面 ## ==============自定义功能函数============ ## ==========由connectSlotsByName() 自动连接的槽函数================== @pyqtSlot() ##"打开一个文件" def on_btnOpen_clicked(self): pass @pyqtSlot() ##"打开多个文件" def on_btnOpenMulti_clicked(self): pass @pyqtSlot() ##“选择已有目录 ” def on_btnSelDir_clicked(self): pass @pyqtSlot() ##“保存文件” def on_btnSave_clicked(self): pass @pyqtSlot() ##"选择颜色" def on_btnColor_clicked(self): pass @pyqtSlot() ##"选择字体" def on_btnFont_clicked(self): pass @pyqtSlot() ##"进度对话框" def on_btnProgress_clicked(self): pass @pyqtSlot() ##“输入字符串” def on_btnInputString_clicked(self): pass @pyqtSlot() ##“输入整数” def on_btnInputInt_clicked(self): pass @pyqtSlot() ##“输入浮点数” def on_btnInputFloat_clicked(self): pass @pyqtSlot() ##"条目选择输入" def on_btnInputItem_clicked(self): pass @pyqtSlot() ##"question" def on_btnMsgQuestion_clicked(self): pass @pyqtSlot() ##"information" def on_btnMsgInformation_clicked(self): pass @pyqtSlot() ##"warning" def on_btnMsgWarning_clicked(self): pass @pyqtSlot() ##"critical" def on_btnMsgCritical_clicked(self): pass @pyqtSlot() ##"about" def on_btnMsgAbout_clicked(self): pass @pyqtSlot() ##"About Qt" def on_btnMsgAboutQt_clicked(self): pass ## =============自定义槽函数=============================== def do_progress_canceled(self): ##取消进度对话框 pass
class QmyDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) #调用父类构造函数,创建窗体 self.ui = Ui_Dialog() #创建UI对象 self.ui.setupUi(self) #构造UI界面 ## ==============自定义功能函数============ ## ==========由connectSlotsByName() 自动连接的槽函数================== @pyqtSlot() ##"打开一个文件" def on_btnOpen_clicked(self): curPath = QDir.currentPath() #获取系统当前目录 dlgTitle = "选择一个文件" #对话框标题 filt = "所有文件(*.*);;文本文件(*.txt);;图片文件(*.jpg *.gif *.png)" #文件过滤器 filename, filtUsed = QFileDialog.getOpenFileName( self, dlgTitle, curPath, filt) self.ui.plainTextEdit.appendPlainText(filename) self.ui.plainTextEdit.appendPlainText("\n" + filtUsed) @pyqtSlot() ##"打开多个文件" def on_btnOpenMulti_clicked(self): curPath = QDir.currentPath() #获取系统当前目录 dlgTitle = "选择一个文件" #对话框标题 filt = "所有文件(*.*);;文本文件(*.txt);;图片文件(*.jpg *.gif *.png)" #文件过滤器 fileList, filtUsed = QFileDialog.getOpenFileNames( self, dlgTitle, curPath, filt) for i in range(len(fileList)): self.ui.plainTextEdit.appendPlainText(fileList[i]) self.ui.plainTextEdit.appendPlainText("\n" + filtUsed) @pyqtSlot() ##“选择已有目录 ” def on_btnSelDir_clicked(self): curPath = QDir.currentPath() #获取系统当前目录 dlgTitle = "选择一个目录" #对话框标题 selectedDir = QFileDialog.getExistingDirectory( self, dlgTitle, curPath, QFileDialog.ShowDirsOnly) self.ui.plainTextEdit.appendPlainText("\n" + selectedDir) @pyqtSlot() ##“保存文件” def on_btnSave_clicked(self): curPath = QDir.currentPath() #获取系统当前目录 dlgTitle = "保存文件" #对话框标题 filt = "所有文件(*.*);;文本文件(*.txt);;图片文件(*.jpg *.gif *.png)" #文件过滤器 filename, filtUsed = QFileDialog.getSaveFileName( self, dlgTitle, curPath, filt) self.ui.plainTextEdit.appendPlainText(filename) self.ui.plainTextEdit.appendPlainText("\n" + filtUsed) @pyqtSlot() ##"选择颜色" def on_btnColor_clicked(self): pal = self.ui.plainTextEdit.palette() #获取现有 palette iniColor = pal.color(QPalette.Text) #现有的文字颜色 color = QColorDialog.getColor(iniColor, self, "选择颜色") if color.isValid(): #选择有效 pal.setColor(QPalette.Text, color) #palette 设置选择的颜色 self.ui.plainTextEdit.setPalette(pal) #设置 palette @pyqtSlot() ##"选择字体" def on_btnFont_clicked(self): iniFont = self.ui.plainTextEdit.font() #获取文本框的字体 font, OK = QFontDialog.getFont(iniFont) #选择字体, 注意与C++版本不同 if (OK): #选择有效 self.ui.plainTextEdit.setFont(font) @pyqtSlot() ##"进度对话框" def on_btnProgress_clicked(self): labText = "正在复制文件..." #文字信息 btnText = "取消" #"取消"按钮的标题 minV = 0 maxV = 200 dlgProgress = QProgressDialog(labText, btnText, minV, maxV, self) dlgProgress.canceled.connect(self.do_progress_canceled) #canceled信号 dlgProgress.setWindowTitle("复制文件") dlgProgress.setWindowModality(Qt.WindowModal) #模态对话框 dlgProgress.setAutoReset( True) #calls reset() as soon as value() equals maximum() dlgProgress.setAutoClose( True) #whether the dialog gets hidden by reset() msCounter = QTime() #计时器 for i in range(minV, maxV + 1): dlgProgress.setValue(i) dlgProgress.setLabelText("正在复制文件,第 %d 个" % i) msCounter.start() #计时器重新开始 while (msCounter.elapsed() < 30): #延时 30ms None if (dlgProgress.wasCanceled()): #中途取消 break @pyqtSlot() ##“输入字符串” def on_btnInputString_clicked(self): dlgTitle = "输入文字对话框" txtLabel = "请输入文件名" defaultInput = "新建文件.txt" echoMode = QLineEdit.Normal #正常文字输入 ## echoMode=QLineEdit.Password #密码输入 text, OK = QInputDialog.getText(self, dlgTitle, txtLabel, echoMode, defaultInput) if (OK): self.ui.plainTextEdit.appendPlainText(text) @pyqtSlot() ##“输入整数” def on_btnInputInt_clicked(self): dlgTitle = "输入整数对话框" txtLabel = "设置字体大小" defaultValue = self.ui.plainTextEdit.font().pointSize() #现有字体大小 minValue = 6 maxValue = 50 stepValue = 1 inputValue, OK = QInputDialog.getInt(self, dlgTitle, txtLabel, defaultValue, minValue, maxValue, stepValue) if OK: font = self.ui.plainTextEdit.font() font.setPointSize(inputValue) self.ui.plainTextEdit.setFont(font) @pyqtSlot() ##“输入浮点数” def on_btnInputFloat_clicked(self): dlgTitle = "输入浮点数对话框" txtLabel = "输入一个浮点数" defaultValue = 3.65 minValue = 0 maxValue = 10000 decimals = 2 inputValue, OK = QInputDialog.getDouble(self, dlgTitle, txtLabel, defaultValue, minValue, maxValue, decimals) if OK: text = "输入了一个浮点数:%.2f" % inputValue self.ui.plainTextEdit.appendPlainText(text) @pyqtSlot() ##"条目选择输入" def on_btnInputItem_clicked(self): dlgTitle = "条目选择对话框" txtLabel = "请选择级别" curIndex = 0 editable = True items = ["优秀", "良好", "合格", "不合格"] text, OK = QInputDialog.getItem(self, dlgTitle, txtLabel, items, curIndex, editable) if OK: self.ui.plainTextEdit.appendPlainText(text) @pyqtSlot() ##"question" def on_btnMsgQuestion_clicked(self): dlgTitle = "Question消息框" strInfo = "文件已被修改,是否保存修改?" defaultBtn = QMessageBox.NoButton #缺省按钮 result = QMessageBox.question( self, dlgTitle, strInfo, QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel, defaultBtn) if (result == QMessageBox.Yes): self.ui.plainTextEdit.appendPlainText("Question消息框: Yes 被选择") elif (result == QMessageBox.No): self.ui.plainTextEdit.appendPlainText("Question消息框: No 被选择") elif (result == QMessageBox.Cancel): self.ui.plainTextEdit.appendPlainText("Question消息框: Cancel 被选择") else: self.ui.plainTextEdit.appendPlainText("Question消息框: 无选择") @pyqtSlot() ##"information" def on_btnMsgInformation_clicked(self): dlgTitle = "information消息框" strInfo = "文件已经被正确打开." QMessageBox.information(self, dlgTitle, strInfo) @pyqtSlot() ##"warning" def on_btnMsgWarning_clicked(self): dlgTitle = "warning消息框" strInfo = "文件内容已经被修改." QMessageBox.warning(self, dlgTitle, strInfo) @pyqtSlot() ##"critical" def on_btnMsgCritical_clicked(self): dlgTitle = "critical消息框" strInfo = "出现严重错误,程序将关闭." QMessageBox.critical(self, dlgTitle, strInfo) @pyqtSlot() ##"about" def on_btnMsgAbout_clicked(self): dlgTitle = "about消息框" strInfo = "Python Qt GUI与数据可视化编程\n保留所有版权" QMessageBox.about(self, dlgTitle, strInfo) @pyqtSlot() ##"About Qt" def on_btnMsgAboutQt_clicked(self): dlgTitle = "aboutQt消息框" QMessageBox.aboutQt(self, dlgTitle) ## =============自定义槽函数=============================== def do_progress_canceled(self): ##取消进度对话框 self.ui.plainTextEdit.appendPlainText("**进度对话框被取消了**")
class QmyDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) #调用父类构造函数,创建窗体 self.ui = Ui_Dialog() #创建Ui对象 self.ui.setupUi(self) #构造UI