QtGui.QApplication(sys.argv) #QApplication 类初始化sys.exit(app.exec_()) #进入消息循环,等待窗体退出创建主界面的两种方法1.通过继承QtGui.QMainWindow创建类QtGui.QMainWindow.__init__(self) # 调用父类初始化方法2.通过继承QtGui.QWidget创建类QtGui.QWidget.__init__(self) # 调用父类初始化方法QPushButton # 按钮setFlat(True) #设置文件样式按钮连接事件信号的两种方法1.利用主界面self的connect方法self.connect(self.button1, # Button1事件 QtCore.SIGNAL('clicked()'), # clicked()信号 self.OnButton1) # 插槽函数2.利用控件本身connect方法 button.clicked.connect(showdata) 对话窗体基本用法 class MyDialog(QtGui.QDialog): # 继承QtGui.QDialog ... self.done(1) # 结束对话框返回1调用 dialog = MyDialog() # 创建对话框对象 r = dialog.exec_() # 运行对话框 if r: self.button.setText(dialog.text) 文本标签控件QLabel QtGui.QLabel('PyQt/nLabel') # 创建标签label1.setAlignment(QtCore.Qt.AlignCenter) # 居中对齐单行文本框控件QLineEditedit1 = QtGui.QLineEdit() # 创建单行文本框edit2.setEchoMode(QtGui.QLineEdit.Password) # 将其设置为密码框多行文本控件QTextEditedit = QtGui.QTextEdit() # 创建多行文本框edit.setText('Python/nPyQt') # 设置文本框中的文字表格式布局gridlayoutgridlayout.setRowMinimumHeight (1, 180) # 设置第二行的最小高度为108窗体菜单栏控件menuBar的基本用法class MyWindow(QtGui.QMainWindow): # 通过继承QtGui.QMainWindow创建类 menubar = self.menuBar() # 获得窗口的菜单条 file = menubar.addMenu('&File') # 添加File菜单 file.addAction('Open') # 添加Open命令 open = self.file.addAction('Open') # 添加Open命令self.connect(open, QtCore.SIGNAL('triggered()'), self.OnOpen) # 菜单信号 退出主窗体界面self.close()界面右键菜单用法 def contextMenuEvent(self, event): # 重载弹出式菜单事件 self.file.exec_(event.globalPos()) 常用消息框用法 QtGui.QMessageBox.about(self, 'PyQt', 'About') # 创建About消息框QtGui.QMessageBox.aboutQt(self, 'PyQt') # 创建AboutQt消息框 r = QtGui.QMessageBox.critical(self, 'PyQt', # 创建Ctitical消息框 'Critical', QtGui.QMessageBox.Abort, QtGui.QMessageBox.Retry, QtGui.QMessageBox.Ignore)返回状态判断 if r == QtGui.QMessageBox.Abort: self.label.setText('Abort') elif r == QtGui.QMessageBox.Retry: self.label.setText('Retry') else: self.label.setText('Ignore') QtGui.QMessageBox.information(self, 'PyQt', 'Information') # 创建Information消息框 r = QtGui.QMessageBox.question(self, 'PyQt', # 创建Question消息框 'Question', QtGui.QMessageBox.Yes, QtGui.QMessageBox.No, QtGui.QMessageBox.Cancel) r = QtGui.QMessageBox.warning(self, 'PyQt', # 创建Warning消息框 'Warning', QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)单选按钮复选按钮的用法 self.radio1 = QtGui.QRadioButton('Radio1') # 创建单选框 self.radio2 = QtGui.QRadioButton('Radio2') self.check = QtGui.QCheckBox('check') # 创建复选框 self.check.setChecked(True) # 将复选框选中状态获取 if self.radio1.isChecked():if self.check.isChecked():xml界面文件的用法from PyQt4 import QtCore, QtGui, uic class MyDialog(QtGui.QDialog): # 继承QtGui.QDialog def __init__(self): QtGui.QWidget.__init__(self) uic.loadUi("res.ui", self) def OnButton(self): # 处理按钮事件 dialog = MyDialog() # 创建对话框对象 r = dialog.exec_() # 运行对话框 if r: self.button.setText(dialog.lineEdit.text()) #获取对话框中控件元素的值空白项控件QSpacerItem的用法 spacer1 = QtGui.QSpacerItem(300,40) # 创建空白项gridlayout.addItem(spacer1, 0 ,0) # 添加空白项标准系统对话框用法 filename = QtGui.QFileDialog.getOpenFileName(self, 'Open') # 创建文件打开对话框 if not filename.isEmpty(): self.label.setText(filename) font, ok = QtGui.QFontDialog.getFont() # 创建字体选中对话框 if ok: self.label.setText(font.key()) color = QtGui.QColorDialog.getColor() # 创建颜色选择对话框 if color.isValid(): self.label.setText(color.name())
来自〉http://hi.baidu.com/dooda/blog/item/b6bb90953c40921d7af4806b.html
转载请注明原文地址: https://ibbs.8miu.com/read-2229770.html