1、第一个gui程序

  1. import sys
  2. from PyQt5.QtWidgets import QApplication
  3. from PyQt5.QtWidgets import QMainWindow
  4. from PyQt5.QtWidgets import QWidget
  5.  
  6. if __name__ == '__main__':
  7. app = QApplication(sys.argv)
  8.  
  9. # 创建一个窗口
  10. w = QWidget()
  11.  
  12. # 设置窗口的高度和宽度
  13. w.resize(500,200)
  14.  
  15. # 移动窗口的位置
  16. w.move(400,100)
  17.  
  18. # 设置窗口的title
  19. w.setWindowTitle("第一个gui程序")
  20.  
  21. # 显示窗口
  22. w.show()
  23.  
  24. # 设置窗口退出,程序在退出
  25. sys.exit(app.exec_())

  

2、设置图标

  1. import sys
  2. from PyQt5.QtWidgets import QApplication
  3. from PyQt5.QtWidgets import QMainWindow
  4. from PyQt5.QtWidgets import QWidget
  5.  
  6. # 设置图标的类
  7. from PyQt5.QtGui import QIcon
  8.  
  9. if __name__ == '__main__':
  10. app = QApplication(sys.argv)
  11.  
  12. # 创建一个窗口
  13. w = QWidget()
  14.  
  15. # 设置窗口的高度和宽度
  16. w.resize(500,200)
  17.  
  18. # 移动窗口的位置
  19. w.move(400,100)
  20.  
  21. # 设置窗口的title
  22. w.setWindowTitle("第一个gui程序")
  23.  
  24. # 设置窗口的图标
  25. w.setWindowIcon(QIcon("test.jpg"))
  26.  
  27. # 显示窗口
  28. w.show()
  29.  
  30. # 设置窗口退出,程序同时退出
  31. sys.exit(app.exec_())

  

3、设置窗口的浮动提示框和按钮的浮动提示框

  1. import sys
  2. from PyQt5.QtWidgets import QApplication
  3. from PyQt5.QtWidgets import QMainWindow
  4. from PyQt5.QtWidgets import QWidget
  5. from PyQt5.QtGui import QIcon
  6.  
  7. # 处理提示框的一个类
  8. from PyQt5.QtWidgets import QToolTip
  9.  
  10. # 处理按钮的一个类
  11. from PyQt5.QtWidgets import QPushButton
  12.  
  13. # 处理字体的一个类
  14. from PyQt5.QtGui import QFont
  15.  
  16. if __name__ == '__main__':
  17. app = QApplication(sys.argv)
  18.  
  19. # 创建一个窗口
  20. w = QWidget()
  21.  
  22. # 设置窗口的高度和宽度
  23. w.resize(500,200)
  24.  
  25. # 移动窗口的位置
  26. w.move(400,100)
  27.  
  28. # 设置窗口的title
  29. w.setWindowTitle("提示框")
  30.  
  31. # 设置窗口的图标
  32. w.setWindowIcon(QIcon("test.jpg"))
  33.  
  34. # 创建窗口的提示文本信息
  35. w.setToolTip("这是一个窗口的提示框\n设计者:张三")
  36.  
  37. # 设置字体和字号
  38. QToolTip.setFont(QFont("SansSerif", 15))
  39.  
  40. # 创建一个按钮,这个按钮防在我们前面创建的窗口中
  41. b = QPushButton("Button",w)
  42.  
  43. # 设置按钮的提示文本信息
  44. b.setToolTip("这是一个按钮的提示框\n设计者:张三")
  45.  
  46. # 改变按钮的尺寸
  47. b.resize(b.sizeHint())
  48.  
  49. # 移动按钮
  50. b.move(50,50)
  51. # 显示窗口
  52. w.show()
  53.  
  54. # 设置窗口退出,程序在退出
  55. sys.exit(app.exec_())

  

4、设置一个按钮,点击该按钮会关闭整个窗口的效果

  1. import sys
  2. from PyQt5.QtWidgets import QApplication
  3. from PyQt5.QtWidgets import QMainWindow
  4. from PyQt5.QtWidgets import QWidget
  5. from PyQt5.QtGui import QIcon
  6.  
  7. # 处理提示框的一个类
  8. from PyQt5.QtWidgets import QToolTip
  9.  
  10. # 处理按钮的一个类
  11. from PyQt5.QtWidgets import QPushButton
  12.  
  13. # 处理字体的一个类
  14. from PyQt5.QtGui import QFont
  15.  
  16. # 需要导入一个新的模块
  17. from PyQt5.QtCore import QCoreApplication
  18.  
  19. if __name__ == '__main__':
  20. app = QApplication(sys.argv)
  21.  
  22. # 创建一个窗口
  23. w = QWidget()
  24.  
  25. # 设置窗口的高度和宽度
  26. w.resize(500,200)
  27.  
  28. # 移动窗口的位置
  29. w.move(400,100)
  30.  
  31. # 设置窗口的title
  32. w.setWindowTitle("关闭窗口测试")
  33.  
  34. # 设置窗口的图标
  35. w.setWindowIcon(QIcon("test.jpg"))
  36.  
  37. # 创建窗口的提示文本信息
  38. w.setToolTip("这是一个窗口的提示框\n设计者:崔跃荣")
  39.  
  40. # 设置字体和字号
  41. QToolTip.setFont(QFont("SansSerif", 15))
  42.  
  43. # 创建一个按钮,这个按钮防在我们前面创建的窗口中
  44. b = QPushButton("点击关闭窗口",w)
  45.  
  46. # 设置按钮的提示文本信息
  47. b.setToolTip("这是一个关闭按钮的提示框\n设计者:崔跃荣")
  48.  
  49. # 为这个按钮绑定一个事件
  50. b.clicked.connect(QCoreApplication.instance().quit)
  51.  
  52. # 改变按钮的尺寸
  53. b.resize(b.sizeHint())
  54.  
  55. # 移动按钮
  56. b.move(50,50)
  57. # 显示窗口
  58. w.show()
  59.  
  60. # 设置窗口退出,程序在退出
  61. sys.exit(app.exec_())

  

5、做个捕获关闭事件的对话框

  1. # 重写QWidget类中的窗口关闭事件,先捕获窗口的关闭的事件,然后在进行判断
  2. import sys
  3.  
  4. from PyQt5.QtWidgets import QWidget
  5. from PyQt5.QtWidgets import QMessageBox
  6. from PyQt5.QtWidgets import QApplication
  7.  
  8. # 封装窗口代码的类
  9. class Mymessagebox(QWidget):
  10. def __init__(self):
  11. super().__init__()
  12. self.initUI()
  13.  
  14. def initUI(self):
  15. self.setGeometry(300,300,300,300)
  16. self.setWindowTitle("对话框")
  17. self.show()
  18.  
  19. # 重写窗口关闭事件
  20. def closeEvent(self,event):
  21. # 显示询问的对话框,这里会阻塞
  22.  
  23. # 显示两个框,一个是YES 一个NO,默认选中no
  24. reply = QMessageBox.question(self,"消息","确定要退出?",QMessageBox.Yes | QMessageBox.No,QMessageBox.No)
  25.  
  26. if reply == QMessageBox.Yes:
  27.  
  28. # 接受事件
  29. event.accept()
  30. else:
  31. # 忽略事件
  32. event.ignore()

6、做一个窗口居中的示例

  1. # 通过得到屏幕宽度和高度和窗口的高度和宽度来计算得到居中的位置
  2.  
  3. from PyQt5.QtWidgets import QApplication
  4. from PyQt5.QtWidgets import QMainWindow
  5. from PyQt5.QtWidgets import QWidget
  6. from PyQt5.QtWidgets import QMessageBox
  7. import sys
  8.  
  9. class Centerwindowbox(QWidget):
  10. def __init__(self):
  11. super().__init__()
  12. self.initUI()
  13.  
  14. def initUI(self):
  15. self.resize(300,200)
  16. self.setWindowTitle("窗口居中的对话框")
  17. self.center()
  18. self.show()
  19.  
  20. def center(self):
  21. desktop = app.desktop()
  22. # print(dir(desktop))
  23.  
  24. # 获取整个屏幕的高度
  25. print(desktop.height())
  26.  
  27. # 获取整个屏幕的宽度
  28. print(desktop.width())
  29.  
  30. # 获取窗口的宽度
  31. print(self.width())
  32.  
  33. # 获取窗口的高度
  34. print(self.height())
  35. self.move((desktop.width() - self.width())/2,(desktop.height() - self.height())/2)
  36.  
  37. if __name__ == '__main__':
  38. app = QApplication(sys.argv)
  39. cw = Centerwindowbox()
  40. sys.exit(app.exec_())

7、做一个绝对布局的例子

  1. from PyQt5.QtWidgets import QApplication
  2. from PyQt5.QtWidgets import QMainWindow
  3. from PyQt5.QtWidgets import QWidget
  4. from PyQt5.QtWidgets import QMessageBox
  5. from PyQt5.QtWidgets import QLabel
  6. import sys
  7.  
  8. class Test(QWidget):
  9. def __init__(self):
  10. super().__init__()
  11. self.initUI()
  12.  
  13. def initUI(self):
  14. label1 = QLabel("姓名",self)
  15. label1.move(15,20)
  16. label2 = QLabel("年龄", self)
  17. label2.move(25,30)
  18. label3 = QLabel("身高", self)
  19. label3.move(35,40)
  20.  
  21. self.setGeometry(300,200,400,400)
  22. self.setWindowTitle("窗口绝对布局")
  23. self.show()
  24.  
  25. if __name__ == '__main__':
  26. app = QApplication(sys.argv)
  27. w = Test()
  28. sys.exit(app.exec_())

8、做一个盒布局的例子

  1. # 水平盒布局和垂直盒布局,有更强的适应性,会随着窗口的尺寸的变化,位置也会变化,绝对布局,如果窗口的尺寸变化,控件的位置是不会变化的
  2.  
  3. # 水平盒布局:沿水平方向摆放
  4.  
  5. # 垂直盒布局:沿垂直方向摆放
  6.  
  7. from PyQt5.QtWidgets import QApplication
  8. from PyQt5.QtWidgets import QMainWindow
  9. from PyQt5.QtWidgets import QWidget
  10. from PyQt5.QtWidgets import QMessageBox
  11. from PyQt5.QtWidgets import QPushButton
  12.  
  13. # 用到的新类
  14. from PyQt5.QtWidgets import QHBoxLayout
  15. from PyQt5.QtWidgets import QVBoxLayout
  16. import sys
  17.  
  18. # 我们这个例子是把两个控件放在水平盒布局中,然后把水平盒布局放在垂直盒布局中
  19.  
  20. class Boxlagout(QWidget):
  21. def __init__(self):
  22. super().__init__()
  23. self.initUI()
  24.  
  25. def initUI(self):
  26. # 创建2个按钮
  27. button1 = QPushButton("确定")
  28. button2 = QPushButton("取消")
  29.  
  30. # 创建水平盒布局
  31. hbox = QHBoxLayout()
  32. vbox = QVBoxLayout()
  33.  
  34. # 将水平盒布局放在窗口的右侧
  35. hbox.addStretch()
  36.  
  37. # 把两个按钮添加到水平盒布局中
  38. hbox.addWidget(button1)
  39. hbox.addWidget(button2)
  40.  
  41. # 将垂直盒布局放在窗口的下边
  42. vbox.addStretch()
  43.  
  44. # 把水平盒布局添加到垂直盒布局中
  45. vbox.addLayout(hbox)
  46.  
  47. # 让垂直盒布局生效,这样操作,水平盒布局也会同时生效
  48. self.setLayout(vbox)
  49. self.setGeometry(300,200,400,400)
  50. self.setWindowTitle("盒布局")
  51. self.show()
  52.  
  53. if __name__ == '__main__':
  54. app = QApplication(sys.argv)
  55. b = Boxlagout()
  56.  
  57. sys.exit(app.exec_())

  

9、做一个网格布局的例子

  1. import sys
  2. from PyQt5.QtWidgets import QApplication
  3. from PyQt5.QtWidgets import QMainWindow
  4. from PyQt5.QtWidgets import QWidget
  5. from PyQt5.QtWidgets import QMessageBox
  6. from PyQt5.QtWidgets import QPushButton
  7.  
  8. # 用到的新类
  9. from PyQt5.QtWidgets import QLabel
  10. from PyQt5.QtWidgets import QLineEdit
  11. from PyQt5.QtWidgets import QTextEdit
  12. from PyQt5.QtWidgets import QGridLayout
  13.  
  14. class Test(QWidget):
  15. def __init__(self):
  16. super().__init__()
  17. self.initUI()
  18.  
  19. def initUI(self):
  20.  
  21. title = QLabel("标题")
  22. auther = QLabel("作者")
  23. summary = QLabel("摘要")
  24.  
  25. titleedit = QLineEdit()
  26. autheredit = QLineEdit()
  27. summaryedit = QTextEdit()
  28.  
  29. # 创建网格布局的对象
  30. grid = QGridLayout()
  31.  
  32. # 创建单元格之间的距离
  33. grid.setSpacing(20)
  34.  
  35. # 添加到第二行的第一列
  36. grid.addWidget(title,1,0)
  37.  
  38. # 添加到第二行的第二列
  39. grid.addWidget(titleedit,1,1)
  40.  
  41. # 添加到第三行第一列
  42. grid.addWidget(auther,2,0)
  43.  
  44. # 添加到第三行第二列
  45. grid.addWidget(autheredit,2,1)
  46.  
  47. grid.addWidget(summary,3,0)
  48.  
  49. # 占据(3,1)(4,1)(5,1)(6,1)
  50. grid.addWidget(summaryedit,3,1,6,1)
  51.  
  52. # 应用当前的网格布局
  53. self.setLayout(grid)
  54.  
  55. self.setGeometry(300,200,400,400)
  56. self.setWindowTitle("网格布局")
  57. self.show()
  58.  
  59. if __name__ == '__main__':
  60. app = QApplication(sys.argv)
  61. b = Test()
  62.  
  63. sys.exit(app.exec_())

  

10、做一个按钮空间的例子

  1. # 按钮支持两种状态,一种是normal,一种是checked
  2. # normal:按钮正常的状态
  3. # checked:按钮被按下的状态
  4.  
  5. import sys
  6. from PyQt5.QtWidgets import QApplication
  7. from PyQt5.QtWidgets import QMainWindow
  8. from PyQt5.QtWidgets import QWidget
  9. from PyQt5.QtWidgets import QMessageBox
  10. from PyQt5.QtWidgets import QPushButton
  11.  
  12. # 用到新的类
  13. from PyQt5.QtWidgets import QFrame
  14.  
  15. # 设置颜色的类
  16. from PyQt5.QtGui import QColor
  17.  
  18. class Test(QWidget):
  19. def __init__(self):
  20. super().__init__()
  21. self.initUI()
  22.  
  23. def initUI(self):
  24. # 创建初始的颜色,黑色
  25. self.color = QColor(0,0,0)
  26.  
  27. # 创建红色的按钮
  28. redbutton = QPushButton("红",self)
  29.  
  30. # 设置按钮被选中
  31. redbutton.setCheckable(True)
  32.  
  33. # 用绝对定位
  34. redbutton.move(10,10)
  35.  
  36. # 点击会执行self.setColor的函数
  37. redbutton.clicked[bool].connect(self.setColor)
  38.  
  39. # 创建绿色的按钮
  40. greenbutton = QPushButton("绿", self)
  41.  
  42. # 设置按钮被选中
  43. greenbutton.setCheckable(True)
  44.  
  45. # 用绝对定位
  46. greenbutton.move(10, 30)
  47.  
  48. # 点击会执行self.setColor的函数
  49. greenbutton.clicked[bool].connect(self.setColor)
  50.  
  51. # 创建蓝色的按钮
  52. bluebutton = QPushButton("蓝", self)
  53.  
  54. # 设置按钮被选中
  55. bluebutton.setCheckable(True)
  56.  
  57. # 用绝对定位
  58. bluebutton.move(10, 50)
  59.  
  60. # 点击会执行self.setColor的函数
  61. bluebutton.clicked[bool].connect(self.setColor)
  62.  
  63. # 创建显示颜色的对象
  64. self.square = QFrame(self)
  65. # 设置尺寸
  66. self.square.setGeometry(150,20,100,100)
  67.  
  68. # 设置背景颜色
  69. self.square.setStyleSheet('QWidget{background-color:%s}'%self.color.name())
  70.  
  71. self.setGeometry(300,200,400,400)
  72. self.setWindowTitle("按钮控件")
  73. self.show()
  74.  
  75. def setColor(self,presssed):
  76. # presssed这个参数的意思是当前的按钮是否被按下
  77.  
  78. # 获取当前点击的按钮
  79. source = self.sender()
  80.  
  81. if presssed:
  82. var = 255
  83. else:
  84. var = 0
  85. if source.text() == "红":
  86. self.color.setRed(var)
  87. elif source.text() == "绿":
  88. self.color.setGreen(var)
  89. else:
  90. self.color.setBlue(var)
  91. self.square.setStyleSheet('QWidget{background-color:%s}'%self.color.name())
  92.  
  93. if __name__ == '__main__':
  94. app = QApplication(sys.argv)
  95. b = Test()
  96.  
  97. sys.exit(app.exec_())

  

11、编写一个QlineEdit控件的例子,单行文本输入框

  1. # 这个例子是这样的,我们定义个lable控件,然后在定义QlineEdit控件,然后对QlineEdit控件绑定一个onchange的方法,这个方法就是获取
  2. # QlineEdit控件输入的值,然后把这个值赋值给label标签
  3.  
  4. import sys
  5.  
  6. from PyQt5.QtWidgets import QWidget
  7. from PyQt5.QtWidgets import QLabel
  8. from PyQt5.QtWidgets import QLineEdit
  9. from PyQt5.QtWidgets import QApplication
  10.  
  11. class LineEdit(QWidget):
  12. def __init__(self):
  13. super().__init__()
  14. self.initUI()
  15.  
  16. def initUI(self):
  17. self.label = QLabel(self)
  18. lineEdit = QLineEdit(self)
  19. lineEdit.move(80,100)
  20. self.label.move(80,40)
  21.  
  22. # 对lineEdit绑定一个changed事件
  23. lineEdit.textChanged[str].connect(self.onChanged)
  24.  
  25. self.setGeometry(300,200,280,200)
  26.  
  27. self.setWindowTitle("QlineEdit控件")
  28. self.show()
  29.  
  30. def onChanged(self,text):
  31. self.label.setText(text)
  32. self.label.adjustSize()
  33.  
  34. if __name__ == '__main__':
  35. app = QApplication(sys.argv)
  36. b = LineEdit()
  37.  
  38. sys.exit(app.exec_())

  

12、写一个Checkbox的例子

  1. # Checkbox:复选框
  2. # 这个例子是这样的,先一个复选框,然后监控这个复选框的是否改变,如果选中,设置窗口的title为A,如果未选中,则设置窗口的title为B
  3. import sys
  4. from PyQt5.QtWidgets import QWidget
  5. from PyQt5.QtWidgets import QApplication
  6. from PyQt5.QtWidgets import QCheckBox
  7. from PyQt5.QtCore import Qt
  8.  
  9. class Checkbox(QWidget):
  10. def __init__(self):
  11. super().__init__()
  12. self.initUI()
  13.  
  14. def initUI(self):
  15. cb = QCheckBox("请选择",self)
  16. cb.move(20,20)
  17.  
  18. # 复选框状态改变,就会触发执行self.changeTitle这个方法,同时会把复选框的说法选中的状态传递到self.changeTitle这个方法中
  19. cb.stateChanged.connect(self.changeTitle)
  20.  
  21. self.setGeometry(100,200,300,200)
  22.  
  23. self.setWindowTitle("还没有选择我")
  24. self.show()
  25.  
  26. # Qt.Checked 这个是布尔类型
  27. def changeTitle(self,state):
  28. if state == Qt.Checked:
  29. self.setWindowTitle("已结选择了")
  30. else:
  31. self.setWindowTitle("还没有选择")
  32.  
  33. if __name__ == '__main__':
  34. app = QApplication(sys.argv)
  35. b = Checkbox()
  36.  
  37. sys.exit(app.exec_())

  

13、写一个Qslider滑块控件的例子

  1. # QSlider:滑块控件,我们这里做一个这样的例子,写一个滑块,设置这个滑块最大值和最小值,然后监控这个滑块的valuechange事件,然后把滑块的value的值
  2. # 赋值给一个lable标签
  3.  
  4. import sys
  5. from PyQt5.QtWidgets import QApplication
  6. from PyQt5.QtWidgets import QLabel
  7. from PyQt5.QtWidgets import QSlider
  8. from PyQt5.QtWidgets import QWidget
  9. from PyQt5.QtCore import Qt
  10.  
  11. class slider(QWidget):
  12. def __init__(self):
  13. super().__init__()
  14. self.initUI()
  15.  
  16. def initUI(self):
  17. # Qt.Horizontal:这个的意思是水平显示
  18. # Vertical:这个意思是垂直显示
  19. # slider = QSlider(Qt.Horizontal,self)
  20. slider = QSlider(Qt.Vertical, self)
  21.  
  22. slider.setMinimum(60)
  23. slider.setMaximum(300)
  24. slider.setGeometry(30,40,50,300)
  25.  
  26. slider.valueChanged[int].connect(self.changevalue)
  27. self.label = QLabel(self)
  28. self.label.setGeometry(160,40,80,30)
  29. self.setGeometry(100,200,300,1000)
  30. self.setWindowTitle("QSlider控件")
  31. self.show()
  32. def changevalue(self,value):
  33. self.label.setText(str(value))
  34.  
  35. if __name__ == '__main__':
  36. app = QApplication(sys.argv)
  37. b = slider()
  38.  
  39. sys.exit(app.exec_())

  

14、pyqt实现一个进度条的示例

  1. #Auther Bob
  2. #--*--conding:utf-8 --*--
  3. import sys
  4. from PyQt5.QtWidgets import QWidget
  5. from PyQt5.QtWidgets import QProgressBar
  6. from PyQt5.QtWidgets import QPushButton
  7. from PyQt5.QtWidgets import QApplication
  8.  
  9. # 定时器
  10. from PyQt5.QtCore import QBasicTimer
  11.  
  12. class ProgressBar(QWidget):
  13. def __init__(self):
  14. super().__init__()
  15. self.initUI()
  16. def initUI(self):
  17. self.pbar = QProgressBar(self)
  18. self.pbar.setGeometry(40,40,400,40)
  19. self.btn = QPushButton("开始",self)
  20. self.btn.move(40,80)
  21. self.btn.clicked.connect(self.doAction)
  22.  
  23. self.timer = QBasicTimer()
  24. self.value = 0
  25. self.setGeometry(300,300,500,500)
  26. self.setWindowTitle("进度条控件")
  27. self.show()
  28. def timerEvent(self, *args, **kwargs):
  29. if self.value >= 100:
  30. self.timer.stop()
  31. self.btn.setText("完成")
  32. return
  33. else:
  34. self.value += 1
  35. self.pbar.setValue(self.value)
  36. def doAction(self):
  37. if self.timer.isActive():
  38. self.timer.stop()
  39. self.btn.setText("开始")
  40. else:
  41. self.timer.start(100,self)
  42. self.btn.setText("停止")
  43. # self.timer.stop()
  44.  
  45. if __name__ == '__main__':
  46. app = QApplication(sys.argv)
  47. form = ProgressBar()
  48. sys.exit(app.exec_())

  

15、Pyqt实现一个下拉菜单的例子

  1. #Auther Bob
  2. #--*--conding:utf-8 --*--
  3. import sys
  4. from PyQt5.QtWidgets import QApplication
  5. from PyQt5.QtWidgets import QWidget
  6. from PyQt5.QtWidgets import QLabel
  7. from PyQt5.QtWidgets import QComboBox
  8.  
  9. class ComboBox(QWidget):
  10. def __init__(self):
  11. super().__init__()
  12. self.initUI()
  13.  
  14. def initUI(self):
  15. self.lab = QLabel("中国",self)
  16. self.lab.move(50,150)
  17. combo = QComboBox(self)
  18. combo.addItem("米国")
  19. combo.addItem("法国")
  20. combo.addItem("德国")
  21. combo.addItem("英国")
  22. combo.move(50,50)
  23.  
  24. combo.activated[str].connect(self.onactived)
  25.  
  26. combo1 = QComboBox(self)
  27.  
  28. combo1 = QComboBox(self)
  29. combo1.addItem("深圳")
  30. combo1.addItem("背景")
  31. combo1.addItem("上海")
  32. combo1.addItem("广州")
  33. combo1.move(200,50)
  34. combo1.activated[str].connect(self.onactived)
  35. self.setGeometry(300,300,300,200)
  36.  
  37. self.setWindowTitle("下拉菜单")
  38.  
  39. self.show()
  40.  
  41. def onactived(self,text):
  42. self.lab.setText(text)
  43. self.lab.adjustSize()
  44.  
  45. if __name__ == '__main__':
  46. app = QApplication(sys.argv)
  47. form = ComboBox()
  48. sys.exit(app.exec_())

  

16、pyqt实现一个现实图像的例子

  1. #Auther Bob
  2. #--*--conding:utf-8 --*--
  3. import sys
  4. from PyQt5.QtWidgets import QApplication
  5. from PyQt5.QtWidgets import QWidget
  6. from PyQt5.QtWidgets import QHBoxLayout
  7. from PyQt5.QtWidgets import QLabel
  8.  
  9. from PyQt5.QtGui import QPixmap
  10.  
  11. class Pixmap(QWidget):
  12. def __init__(self):
  13. super().__init__()
  14. self.initUI()
  15.  
  16. def initUI(self):
  17. hbox = QHBoxLayout(self)
  18.  
  19. pixmap = QPixmap("test.JPG")
  20.  
  21. lab = QLabel(self)
  22.  
  23. lab.setPixmap(pixmap)
  24.  
  25. hbox.addWidget(lab)
  26.  
  27. self.setLayout(hbox)
  28.  
  29. self.move(300,300)
  30.  
  31. self.setWindowTitle("显示图像")
  32. self.show()
  33.  
  34. if __name__ == '__main__':
  35. app = QApplication(sys.argv)
  36. form = Pixmap()
  37. sys.exit(app.exec_())

  

17、pyqt实现一个显示日历的例子

  1. #Auther Bob
  2. #--*--conding:utf-8 --*--
  3.  
  4. "日历控件"
  5.  
  6. import sys
  7. from PyQt5.QtWidgets import QApplication
  8. from PyQt5.QtWidgets import QWidget
  9. from PyQt5.QtWidgets import QLabel
  10. from PyQt5.QtWidgets import QCalendarWidget
  11. from PyQt5.QtWidgets import QVBoxLayout
  12.  
  13. from PyQt5.QtCore import QDate
  14.  
  15. class CalendarWidget(QWidget):
  16. def __init__(self):
  17. super().__init__()
  18. self.initUI()
  19.  
  20. def initUI(self):
  21. vbox = QVBoxLayout()
  22. cal = QCalendarWidget(self)
  23. self.lab = QLabel(self)
  24.  
  25. # 显示网个
  26. cal.setGridVisible(True)
  27.  
  28. cal.clicked[QDate].connect(self.showdate)
  29.  
  30. vbox.addWidget(cal)
  31.  
  32. date = cal.selectedDate()
  33.  
  34. self.lab.setText(date.toString())
  35.  
  36. vbox.addWidget(self.lab)
  37.  
  38. self.setLayout(vbox)
  39.  
  40. self.setGeometry(300,300,350,300)
  41. self.setWindowTitle("日历控件")
  42. self.show()
  43.  
  44. def showdate(self,data):
  45. self.lab.setText(data.toString())
  46.  
  47. if __name__ == '__main__':
  48. app = QApplication(sys.argv)
  49. form = CalendarWidget()
  50. sys.exit(app.exec_())

  

18、pyqt实现一个显示菜单的例子

  1. #Auther Bob
  2. #--*--conding:utf-8 --*--
  3.  
  4. import sys
  5. from PyQt5.QtWidgets import QApplication
  6. from PyQt5.QtWidgets import QWidget
  7. from PyQt5.QtWidgets import QLabel
  8.  
  9. from PyQt5.QtWidgets import QMainWindow
  10. from PyQt5.QtWidgets import QAction
  11. from PyQt5.QtWidgets import QMenu
  12.  
  13. class Menu(QMainWindow):
  14.  
  15. def __init__(self):
  16. super().__init__()
  17. self.initUI()
  18.  
  19. def initUI(self):
  20. menubar = self.menuBar()
  21. fileMenu = menubar.addMenu("文件")
  22. newAction = QAction("新建",self)
  23. importMenu = QMenu("导入",self)
  24. importAction1 = QAction("从pdf导入",self)
  25. importAction2 = QAction("从word导入", self)
  26. importAction1.triggered.connect(self.actionHandler1)
  27. importAction2.triggered.connect(self.actionHandler2)
  28.  
  29. importMenu.addAction(importAction1)
  30. importMenu.addAction(importAction2)
  31.  
  32. fileMenu.addAction(newAction)
  33. fileMenu.addMenu(importMenu)
  34.  
  35. self.setGeometry(300,300,300,200)
  36. self.setWindowTitle("菜单")
  37. self.show()
  38. def actionHandler1(self):
  39. print("从pdf导入")
  40. def actionHandler2(self):
  41. print("从word导入")
  42.  
  43. if __name__ == '__main__':
  44.  
  45. app = QApplication(sys.argv)
  46. form = Menu()
  47. sys.exit(app.exec_())

  

Pyqt5的事例讲解的更多相关文章

  1. Android Service组件(1)

    android service 和其他服务一样,并没有实际运行的界面,它运行在android 后台.一般通过service为应用程序提供服务(比如,从Internet下载文件,控制音乐播放器等).Se ...

  2. Solidity教程系列1 - 类型介绍

    现在的Solidity中文文档,要么翻译的太烂,要么太旧,决定重新翻译下,再加上代码事例讲解. 写在前面 Solidity是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所了解, 如果 ...

  3. 智能合约语言Solidity教程系列2 - 地址类型介绍

    智能合约语言Solidity教程系列第二篇 - Solidity地址类型介绍. 写在前面 Solidity是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所了解,如果你还不了解,建议你 ...

  4. 智能合约语言 Solidity 教程系列2 - 地址类型介绍

    Solidity教程系列第二篇 - Solidity地址类型介绍. 写在前面 Solidity是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所了解,如果你还不了解,建议你先看以太坊是 ...

  5. 现代JS中的流程控制:详解Callbacks 、Promises 、Async/Await

    JavaScript经常声称是_异步_.那是什么意思?它如何影响发展?近年来这种方法有何变化? 请思考以下代码: result1 = doSomething1(); result2 = doSomet ...

  6. .Net异步编程详解入门

    前言 今天周五,早上起床晚了.赶着挤公交上班.但是目前眼前有这么几件事情.刷牙洗脸.泡牛奶.煎蛋.在同步编程眼中.先刷牙洗脸,然后烧水泡牛奶.再煎蛋,最后喝牛奶吃蛋.毫无疑问,在时间紧促的当下.它完了 ...

  7. PyQt5中QTableView函数讲解

    如果想熟悉QTableWidget,请参考PyQt5高级界面控件之QTableWidget(四) setSpan(int, int, int, int)四个参数分别代表,起始行,列,合并的行数,全并的 ...

  8. zookeeper入门讲解事例

    zookeeper使用和原理探究(一) zookeeper介绍zookeeper是一个为分布式应用提供一致性服务的软件,它是开源的Hadoop项目中的一个子项目,并且根据google发表的<Th ...

  9. Oracle Date Function 讲解和事例

    1 year=1*12 months 1 day=24 hours=24*(1*60mins)=24*60*(1*60 seconds) 1 week =7 days 注意: 黑色字体是 oracle ...

随机推荐

  1. table布局与div布局

      DIV与TABLE本身并不存在什么优缺点,所谓web标准只是推荐的是正确的使用标签,好比说:DIV用于布局,而TABLE则本来就是转二维数据的.让TABLE做该做的事,并不是说页面里不出现TABL ...

  2. join和子查询的一点点思考

    代码和表设计过程中,为了考虑数据库的范式,通常导致需要join多张表或子查询, 如报表场景, 可此种方式在大数据量的 情况下,效率较低.  如果能做适量的数据冗余,便可以减少join或子查询,效率较高 ...

  3. mysql网课部分笔记

    mysql> \s  查看当前数据库的状态 \c  取消当前所输入的命令或字符 ------------------------------------------------------- m ...

  4. NGUI 背景图自适应

    背景图UISprite组件调整如下: UIRoot设置: 不保持比例自适应: 保持宽与屏幕宽一致,高度随宽的缩放比例进行缩放:

  5. ReactiveX 学习笔记(5)合并数据流

    Combining Observables 本文的主题为合并 Observable 的操作符. 这里的 Observable 实质上是可观察的数据流. RxJava操作符(四)Combining An ...

  6. 【366】通过 python 求解 QP 问题

    参考: 9.3 凸优化 · 如何在 Python 中利用 CVXOPT 求解二次规划问题 参考: Quadratic Programming - Official website 步骤如下: 首先安装 ...

  7. Mysql数据库如何创建用户

    创建test用户,密码是1234. MySQL u root -p CREATE USER 'test'@'localhost'  IDENTIFIED BY '1234'; #本地登录 CREATE ...

  8. [namespace]PHP命名空间的动态访问 & 使用技巧

    ----------------------------------------------------------------------------------------------- /* | ...

  9. 一个关于EasyUI超恶心的BUG。。。Cannot read property 'options' of undefined

    控制台Console抛出的异常: jquery.easyui.min.js:9148 Uncaught TypeError: Cannot read property 'options' of und ...

  10. ELK日志平台

    1.ELK平台能够完美的解决我们上述的问题,ELK由ElasticSearch.Logstash和Kibana三个开源工具组成,不过现在还新增了一个Beats,它是一个轻量级的日志收集处理工具(Age ...