我目前正在研究一种与Beagle Bone配合使用的热电偶的程序。代码在python中,当运行时,.py工作正常,但我的教授希望程序有一个GUI,以便低年级学生更容易使用该程序。
import xlsxwriter
import Adafruit_BBIO.ADC as ADC
import time
ADC.setup()
workbook=xlsxwriter.Workbook('Volt.xlsx')
worksheet=workbook.add_worksheet()
row = 0
col=0
reading = 1
number = input("Enter number of measurements: ")
interv = input("Enter interval time: ")
while number > 0:
worksheet.write(row, col, reading)
fraction = ADC.read("AIN4")
fraction = ADC.read("AIN4")
volts = fraction * 1.8
print volts
worksheet.write(row, col + 1, volts)
time.sleep(interv)
number=number-1
row = row + 1
reading=reading+1
worksheet.write(row, 0, 'Reading Number')
worksheet.write(row, 1, 'Voltage')
workbook.close()
现在,当你按下按钮1(在下面的代码上)时,我怎么能这样做呢?
GUI代码:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.setEnabled(True)
Form.resize(633, 378)
self.widget = QtGui.QWidget(Form)
self.widget.setGeometry(QtCore.QRect(10, 10, 611, 341))
self.widget.setObjectName(_fromUtf8("widget"))
self.gridLayout = QtGui.QGridLayout(self.widget)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.pushButton_2 = QtGui.QPushButton(self.widget)
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.gridLayout.addWidget(self.pushButton_2, 2, 1, 1, 1)
self.pushButton = QtGui.QPushButton(self.widget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.gridLayout.addWidget(self.pushButton, 2, 0, 1, 1)
self.label = QtGui.QLabel(self.widget)
self.label.setAutoFillBackground(True)
self.label.setFrameShape(QtGui.QFrame.NoFrame)
self.label.setLineWidth(0)
self.label.setMidLineWidth(1)
self.label.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
self.label.setWordWrap(False)
self.label.setOpenExternalLinks(False)
self.label.setObjectName(_fromUtf8("label"))
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.label_2 = QtGui.QLabel(self.widget)
self.label_2.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignTop)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.gridLayout.addWidget(self.label_2, 0, 1, 1, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Beagle Bone Thermocouple Program- Coded by Joshua Baney", None))
self.pushButton_2.setText(_translate("Form", "Press for use of two thermocouples", None))
self.pushButton.setText(_translate("Form", "Press for use of one thermocouple", None))
self.label.setText(_translate("Form", "<html><head/><body><p align=\"center\"><span style=\" font-size:12pt; font-weight:600; text-decoration: underline;\">Beagle Bone thermocuple program</span></p></body></html>", None))
self.label_2.setText(_translate("Form", "<html><head/><body><p align=\"center\"><span style=\" font-size:9pt;\">Coded by Joshua Baney</span></p><p align=\"center\"><span style=\" font-size:9pt;\">support: joshnbaney@gmail.com</span></p><p align=\"center\"><span style=\" font-size:9pt;\"><br/></span></p><p align=\"center\"><span style=\" font-size:9pt;\"><br/></span></p></body></html>", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
我建议使用一个初始化GUI类的新class
:
from ui_Form import Ui_Form
class Widget(QWidget):
def __init__(self):
super(Widget, self).__init__(parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
# here you connect a button to
self.ui.pushButton_2.clicked.connect(self.run_code)
# execute the code you want
def run_code(self):
code.py # but better would be to create a method/function for this code or even a class with method to tun