PyQt界面控件常用积累

初始化

  1. QtGui.QApplication(sys.argv) #QApplication 类初始化
  2. sys.exit(app.exec_()) #进入消息循环,等待窗体退出

创建主界面的两种方法

  1. # 1.通过继承QtGui.QMainWindow创建类
  2. QtGui.QMainWindow.__init__(self) # 调用父类初始化方法
  3. # 2.通过继承QtGui.QWidget创建类
  4. QtGui.QWidget.__init__(self) # 调用父类初始化方法
  5. QPushButton # 按钮
  6. setFlat(True) #设置文件样式按钮

连接事件信号的两种方法

  1. # 1.利用主界面self的connect方法
  2. self.connect(self.button1, # Button1事件
  3. QtCore.SIGNAL('clicked()'), # clicked()信号
  4. self.OnButton1 # 插槽函数
  5. )
  6. # 2.利用控件本身connect方法
  7. button.clicked.connect(showdata)

对话窗体基本用法

  1. # 定义
  2. class MyDialog(QtGui.QDialog): # 继承QtGui.QDialog
  3. ...
  4. self.done(1) # 结束对话框返回1
  5. # 调用
  6. dialog = MyDialog() # 创建对话框对象
  7. r = dialog.exec_() # 运行对话框
  8. if r:
  9. self.button.setText(dialog.text)
  10. # 文本标签控件QLabel
  11. QtGui.QLabel('PyQt\nLabel') # 创建标签
  12. label1.setAlignment(QtCore.Qt.AlignCenter) # 居中对齐
  13. # 单行文本框控件QLineEdit
  14. edit1 = QtGui.QLineEdit() # 创建单行文本框
  15. edit2.setEchoMode(QtGui.QLineEdit.Password) # 将其设置为密码框
  16. # 多行文本控件QTextEdit
  17. edit = QtGui.QTextEdit() # 创建多行文本框
  18. edit.setText('Python\nPyQt') # 设置文本框中的文字
  19. # 表格式布局gridlayout
  20. gridlayout.setRowMinimumHeight (1, 180) # 设置第二行的最小高度为108
  21. # 窗体菜单栏控件menuBar的基本用法
  22. class MyWindow(QtGui.QMainWindow): # 通过继承QtGui.QMainWindow创建类
  23. menubar = self.menuBar() # 获得窗口的菜单条
  24. file = menubar.addMenu('&File') # 添加File菜单
  25. file.addAction('Open') # 添加Open命令
  26. open = self.file.addAction('Open') # 添加Open命令
  27. self.connect(open, QtCore.SIGNAL('triggered()'), self.OnOpen) # 菜单信号
  28. # 退出主窗体界面
  29. self.close()
  30. # 界面右键菜单用法
  31. def contextMenuEvent(self, event): # 重载弹出式菜单事件
  32. self.file.exec_(event.globalPos())
  33. # 常用消息框用法
  34. QtGui.QMessageBox.about(self, 'PyQt', 'About') # 创建About消息框
  35. QtGui.QMessageBox.aboutQt(self, 'PyQt') # 创建AboutQt消息框
  36. # 创建Ctitical消息框
  37. r = QtGui.QMessageBox.critical(self, 'PyQt', 'Critical',
  38. QtGui.QMessageBox.Abort,
  39. QtGui.QMessageBox.Retry,
  40. QtGui.QMessageBox.Ignore
  41. )
  42. # 返回状态判断
  43. if r == QtGui.QMessageBox.Abort:
  44. self.label.setText('Abort')
  45. elif r == QtGui.QMessageBox.Retry:
  46. self.label.setText('Retry')
  47. else:
  48. self.label.setText('Ignore')
  49. QtGui.QMessageBox.information(self, 'PyQt', 'Information') # 创建Information消息框
  50. # 创建Question消息框
  51. r = QtGui.QMessageBox.question(self, 'PyQt',
  52. 'Question',
  53. QtGui.QMessageBox.Yes,
  54. QtGui.QMessageBox.No,
  55. QtGui.QMessageBox.Cancel
  56. )
  57. # 创建Warning消息框
  58. r = QtGui.QMessageBox.warning(self, 'PyQt',
  59. 'Warning',
  60. QtGui.QMessageBox.Yes,
  61. QtGui.QMessageBox.No
  62. )
  63. # 单选按钮复选按钮的用法
  64. self.radio1 = QtGui.QRadioButton('Radio1') # 创建单选框
  65. self.radio2 = QtGui.QRadioButton('Radio2')
  66. self.check = QtGui.QCheckBox('check') # 创建复选框
  67. self.check.setChecked(True) # 将复选框选中
  68. # 状态获取
  69. if self.radio1.isChecked():
  70. if self.check.isChecked():
  71. # xml界面文件的用法
  72. from PyQt4 import QtCore, QtGui, uic
  73. class MyDialog(QtGui.QDialog): # 继承QtGui.QDialog
  74. def__init__(self):
  75. QtGui.QWidget.__init__(self)
  76. uic.loadUi("res.ui", self)
  77. def OnButton(self): # 处理按钮事件
  78. dialog = MyDialog() # 创建对话框对象
  79. r = dialog.exec_() # 运行对话框
  80. if r:
  81. self.button.setText(dialog.lineEdit.text())#获取对话框中控件元素的值
  82. # 空白项控件QSpacerItem的用法
  83. spacer1 = QtGui.QSpacerItem(300,40) # 创建空白项
  84. gridlayout.addItem(spacer1, 0 ,0) # 添加空白项

标准系统对话框用法

  1. filename = QtGui.QFileDialog.getOpenFileName(self, 'Open') #创建文件打开对话框
  2. if not filename.isEmpty():
  3. self.label.setText(filename)
  4. font, ok = QtGui.QFontDialog.getFont() # 创建字体选中对话框
  5. if ok:
  6. self.label.setText(font.key())
  7. color = QtGui.QColorDialog.getColor() # 创建颜色选择对话框
  8. if color.isValid():
  9. self.label.setText(color.name())

Related