1

I am currently working on a program to work with thermocouples that work with a Beagle Bone. The code is in python and when ran as a .py works fine, but my professor wants the program to have a GUI so the underclassmen have an easier time using the program.

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()

Now, how would I make it so when you press button 1 (on the code below) it runs?

Code for 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: [email protected]</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 Answer 1

1

I suggest to use a new class that will initialize a 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.