当前位置:   article > 正文

Qt界面制作简单教程,调用python代码_python qt设计抑郁症答题界面

python qt设计抑郁症答题界面

利用Qt5design或Qt5Creator制作界面的布局,包括设置按钮的个数、布局和大小等,注意记得为每个按钮添加槽函数。保存生成.ui文件。

利用pyuic5 xxx.ui >xxx.py或pyuic5 xxx.ui -o xxx.py命令生成.py文件如下:

  1. # -*- coding: utf-8 -*-
  2. # Form implementation generated from reading ui file 'mainwindow.ui'
  3. #
  4. # Created by: PyQt5 UI code generator 5.9.2
  5. #
  6. # WARNING! All changes made in this file will be lost!
  7. from PyQt5 import QtCore, QtGui, QtWidgets
  8. class Ui_MainWindow(object):
  9. def setupUi(self, MainWindow):
  10. MainWindow.setObjectName("MainWindow")
  11. MainWindow.resize(800, 600) #界面大小
  12. self.centralwidget = QtWidgets.QWidget(MainWindow)
  13. self.centralwidget.setObjectName("centralwidget")
  14. self.pushButton = QtWidgets.QPushButton(self.centralwidget)
  15. self.pushButton.setGeometry(QtCore.QRect(175, 75, 150, 50)) #按钮(x,y,w,h)
  16. self.pushButton.setObjectName("pushButton")
  17. self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
  18. self.pushButton_2.setGeometry(QtCore.QRect(175, 275, 150, 50))
  19. self.pushButton_2.setObjectName("pushButton_2")
  20. self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
  21. self.pushButton_3.setGeometry(QtCore.QRect(175, 475, 150, 50))
  22. self.pushButton_3.setObjectName("pushButton_3")
  23. self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
  24. self.pushButton_4.setGeometry(QtCore.QRect(475, 75, 150, 50))
  25. self.pushButton_4.setObjectName("pushButton_4")
  26. #self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
  27. #self.pushButton_5.setGeometry(QtCore.QRect(340, 90, 89, 25))
  28. #self.pushButton_5.setObjectName("pushButton_5")
  29. self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
  30. self.pushButton_6.setGeometry(QtCore.QRect(475, 475, 150, 50))
  31. self.pushButton_6.setObjectName("pushButton_6")
  32. '''MainWindow.setCentralWidget(self.centralwidget)
  33. self.menubar = QtWidgets.QMenuBar(MainWindow)
  34. self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 28))
  35. self.menubar.setObjectName("menubar")
  36. MainWindow.setMenuBar(self.menubar)
  37. self.statusbar = QtWidgets.QStatusBar(MainWindow)
  38. self.statusbar.setObjectName("statusbar")
  39. MainWindow.setStatusBar(self.statusbar)'''
  40. self.retranslateUi(MainWindow)
  41. self.pushButton.clicked.connect(MainWindow.loadimg) #loadimg为槽函数名
  42. self.pushButton_2.clicked.connect(MainWindow.tag)
  43. self.pushButton_3.clicked.connect(MainWindow.cal)
  44. self.pushButton_4.clicked.connect(MainWindow.reset)
  45. #self.pushButton_5.clicked.connect(MainWindow.resetall)
  46. self.pushButton_6.clicked.connect(MainWindow.calall)
  47. QtCore.QMetaObject.connectSlotsByName(MainWindow)
  48. def retranslateUi(self, MainWindow):
  49. _translate = QtCore.QCoreApplication.translate
  50. MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
  51. self.pushButton.setText(_translate("MainWindow", "载入图片"))
  52. self.pushButton_2.setText(_translate("MainWindow", "标注"))
  53. self.pushButton_3.setText(_translate("MainWindow", "计算当前"))
  54. self.pushButton_4.setText(_translate("MainWindow", "重置当前"))
  55. #self.pushButton_5.setText(_translate("MainWindow", "全部重置"))
  56. self.pushButton_6.setText(_translate("MainWindow", "计算均值"))

如果后续需要对界面进行改动,可以直接在这个.py进行修改。

新建一个.py文件,写入如下代码:

  1. # -*- coding: utf-8 -*-
  2. from PyQt5 import QtWidgets, QtGui
  3. import sys
  4. from PIL import Image
  5. import cv2
  6. import numpy as np
  7. import pylab as pl
  8. import matplotlib.pyplot as plt
  9. import scipy.io as sio
  10. from PIL import Image
  11. import os
  12. from mainwindow import Ui_MainWindow # 导入生成mainwindow.py里生成的类
  13. class mywindow(QtWidgets.QWidget,Ui_MainWindow):
  14. def __init__(self):
  15. super(mywindow,self).__init__()
  16. self.setupUi(self)
  17. self.num=0 #image number
  18. self.count=0
  19. #定义槽函数
  20. def loadimg(self):
  21. if self.count==0:
  22. print("start loading image!")
  23. else:
  24. print('please execute in order!')
  25. def tag(self):
  26. if self.count==1:
  27. print("start tagging!")
  28. else:
  29. print('please execute in order!')
  30. def cal(self):
  31. if self.count==2:
  32. print("start calculating!")
  33. else:
  34. print('please execute in order!')
  35. def reset(self):
  36. print("start with tagging!")
  37. self.count=1
  38. '''def resetall(self):
  39. print("resetting all!")'''
  40. def calall(self):
  41. if self.num==10:
  42. print("start calculating all!")
  43. else:
  44. print('please execute in order!')
  45. app = QtWidgets.QApplication(sys.argv)
  46. window = mywindow()
  47. window.show()
  48. sys.exit(app.exec_())

可以在类中写入自己需要使用的函数,每个槽函数对应每个按钮被按下后会调用的内容。(槽函数名需与ui转换来的py文件里的一致)

在终端里启动新写的.py文件,即可打开界面,点击相应按钮,实现相应的功能。效果如下(做的比较丑):

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/315904
推荐阅读
相关标签
  

闽ICP备14008679号