将PyQt输出与python文件链接

时间:2012-08-16 04:55:50

标签: python pyqt

如何将此PyQt输出与底部的python文件链接?我查看了各种教程,但我无法弄明白。如何设置单选按钮的值,如果有人点击地球r用6.4 * 10 ** 6填充,g设置为9.8?

来自Qt designer 的

import sys
from PyQt4 import QtCore, QtGui
from projectile_ui import Ui_Form


class MyForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(443, 340)
        self.Tilte = QtGui.QLabel(Form)
        self.Tilte.setGeometry(QtCore.QRect(130, 10, 131, 16))
        self.Tilte.setObjectName(_fromUtf8("Tilte"))
        self.Vo = QtGui.QDoubleSpinBox(Form)
        self.Vo.setGeometry(QtCore.QRect(20, 40, 81, 22))
        self.Vo.setMaximum(999999999.0)
        self.Vo.setObjectName(_fromUtf8("Vo"))
        self.angle = QtGui.QDoubleSpinBox(Form)
        self.angle.setGeometry(QtCore.QRect(20, 70, 81, 22))
        self.angle.setMaximum(90.0)
        self.angle.setObjectName(_fromUtf8("angle"))
        self.dt = QtGui.QDoubleSpinBox(Form)
        self.dt.setGeometry(QtCore.QRect(20, 100, 81, 22))
        self.dt.setDecimals(3)
        self.dt.setMinimum(0.0)
        self.dt.setMaximum(1.0)
        self.dt.setSingleStep(0.001)
        self.dt.setProperty("value", 0.01)
        self.dt.setObjectName(_fromUtf8("dt"))
        self.Jupiter = QtGui.QRadioButton(Form)
        self.Jupiter.setGeometry(QtCore.QRect(20, 240, 82, 17))
        self.Jupiter.setObjectName(_fromUtf8("Jupiter"))
        self.Saturn = QtGui.QRadioButton(Form)
        self.Saturn.setGeometry(QtCore.QRect(20, 260, 82, 17))
        self.Saturn.setObjectName(_fromUtf8("Saturn"))
        self.Sun = QtGui.QRadioButton(Form)
        self.Sun.setGeometry(QtCore.QRect(20, 140, 82, 17))
        self.Sun.setCheckable(True)
        self.Sun.setObjectName(_fromUtf8("Sun"))
        self.Venus = QtGui.QRadioButton(Form)
        self.Venus.setGeometry(QtCore.QRect(20, 180, 82, 17))
        self.Venus.setObjectName(_fromUtf8("Venus"))
        self.Mars = QtGui.QRadioButton(Form)
        self.Mars.setGeometry(QtCore.QRect(20, 220, 82, 17))
        self.Mars.setObjectName(_fromUtf8("Mars"))
        self.Neptune = QtGui.QRadioButton(Form)
        self.Neptune.setGeometry(QtCore.QRect(20, 300, 82, 17))
        self.Neptune.setObjectName(_fromUtf8("Neptune"))
        self.Earth = QtGui.QRadioButton(Form)
        self.Earth.setGeometry(QtCore.QRect(20, 200, 82, 17))
        self.Earth.setObjectName(_fromUtf8("Earth"))
        self.Uranus = QtGui.QRadioButton(Form)
        self.Uranus.setGeometry(QtCore.QRect(20, 280, 82, 17))
        self.Uranus.setObjectName(_fromUtf8("Uranus"))
        self.Mercury = QtGui.QRadioButton(Form)
        self.Mercury.setGeometry(QtCore.QRect(20, 160, 82, 17))
        self.Mercury.setObjectName(_fromUtf8("Mercury"))
        self.Vo_Label = QtGui.QLabel(Form)
        self.Vo_Label.setGeometry(QtCore.QRect(110, 40, 71, 21))
        self.Vo_Label.setObjectName(_fromUtf8("Vo_Label"))
        self.Angle_Label = QtGui.QLabel(Form)
        self.Angle_Label.setGeometry(QtCore.QRect(110, 70, 61, 21))
        self.Angle_Label.setObjectName(_fromUtf8("Angle_Label"))
        self.dt_Label = QtGui.QLabel(Form)
        self.dt_Label.setGeometry(QtCore.QRect(110, 100, 61, 21))
        self.dt_Label.setObjectName(_fromUtf8("dt_Label"))
        self.LCDheight = QtGui.QLCDNumber(Form)
        self.LCDheight.setGeometry(QtCore.QRect(230, 150, 181, 71))
        self.LCDheight.setObjectName(_fromUtf8("LCDheight"))
        self.lcdTotaltime = QtGui.QLCDNumber(Form)
        self.lcdTotaltime.setGeometry(QtCore.QRect(230, 250, 181, 71))
        self.lcdTotaltime.setObjectName(_fromUtf8("lcdTotaltime"))
        self.label = QtGui.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(230, 132, 181, 21))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(230, 232, 181, 21))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(230, 50, 181, 61))
        self.pushButton.setAutoFillBackground(False)
        self.pushButton.setDefault(False)
        self.pushButton.setFlat(False)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.Tilte.setText(QtGui.QApplication.translate("Form", "Projectile Motion Calculator", None, QtGui.QApplication.UnicodeUTF8))
        self.Jupiter.setText(QtGui.QApplication.translate("Form", "Jupiter", None, QtGui.QApplication.UnicodeUTF8))
        self.Saturn.setText(QtGui.QApplication.translate("Form", "Saturn", None, QtGui.QApplication.UnicodeUTF8))
        self.Sun.setText(QtGui.QApplication.translate("Form", "Sun ", None, QtGui.QApplication.UnicodeUTF8))
        self.Venus.setText(QtGui.QApplication.translate("Form", "Venus", None, QtGui.QApplication.UnicodeUTF8))
        self.Mars.setText(QtGui.QApplication.translate("Form", "Mars", None, QtGui.QApplication.UnicodeUTF8))
        self.Neptune.setText(QtGui.QApplication.translate("Form", "Neptune", None, QtGui.QApplication.UnicodeUTF8))
        self.Earth.setText(QtGui.QApplication.translate("Form", "Earth", None, QtGui.QApplication.UnicodeUTF8))
        self.Uranus.setText(QtGui.QApplication.translate("Form", "Uranus", None, QtGui.QApplication.UnicodeUTF8))
        self.Mercury.setText(QtGui.QApplication.translate("Form", "Mercury", None, QtGui.QApplication.UnicodeUTF8))
        self.Vo_Label.setText(QtGui.QApplication.translate("Form", "Initial Velocity ", None, QtGui.QApplication.UnicodeUTF8))
        self.Angle_Label.setText(QtGui.QApplication.translate("Form", "Angle", None, QtGui.QApplication.UnicodeUTF8))
        self.dt_Label.setToolTip(QtGui.QApplication.translate("Form", "<html><head/><body><p>Smaller number gives more accurate results.</p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.dt_Label.setText(QtGui.QApplication.translate("Form", "Time Step", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("Form", "Max Height ", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setText(QtGui.QApplication.translate("Form", "Total Time ", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("Form", "Calculate", None, QtGui.QApplication.UnicodeUTF8))

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())

代码我想链接到UI。

from math import*
def main():
    loopiterations = int()

    dt = eval(input("")) ##time step

    Vo = eval(input(""))
    xo = 0
    yo = 0
    angle = eval(input(""))
    angle = angle * (pi / 180)
    Vox = Vo * cos(angle)
    Voy = Vo * sin(angle)
    y =  (yo + Voy * dt)
    r=eval(input(""))  ## r= 6.4*10**6 for earth 
    g = eval(input(""))  ## m/s **2
    Vy=Voy - g * dt
    iterations = 0

    while not (Vy < 0):
        y =  (yo + Voy * dt)
        Vy= (Voy - g * dt)
        x = (xo + Vox * dt)
        Vx = Vox
        iterations = iterations + 1
        yo = y
        xo = x
        Vox = Vx
        Voy = Vy
    print (y)
    print(iterations)

3 个答案:

答案 0 :(得分:1)

如果您在QtDesigner中设计了该UI并仍然使用.ui文件,那么使用它的简单方法是实际上不打扰pyuic,只需直接使用它:

from PyQt4 import uic

...
class ProjectileObject(QtCore.QObject):
    def __init__(self):
        super(ProjectileObject, self).__init__()
        self.ui = None
        self.ui = uic.loadUi('projectile.ui', self.ui)
        self.ui.Saturn.clicked.connect(self.radioClicked)    #These lines connect your
        self.ui.Uranus.clicked.connect(self.radioClicked)    #interface to your code
        ...                                                  #you can look up other signals as well
        self.ui.show()
    def radioClicked():
        #This code can inspect the various radio buttons, and take different actions
        #depending on which one was clicked

请注意,使用QtDesigner中的动作编辑器创建“switchPlanet”动作,然后使用Signal / Slot编辑器将所有单选按钮连接到QtDesigner中的该动作更加高效,然后您只需执行类似的操作代码中self.ui.switchPlanet.triggered.connect(self.radioClicked),您只需要执行一次。

答案 1 :(得分:1)

首先,为了解决您的单选按钮,我会将它们放入代码中的QButtonGroup。它会将所有这些按钮合并为一个单一的联系点,并能够将它们排除在外:

self.planetGroup = QtGui.QButtonGroup(self)
self.planetGroup.setExclusive(True)
self.planetGroup.addButton(self.Saturn)
self.planetGroup.addButton(self.Uranus)

self.planetGroup.buttonClicked["QAbstractButton*"].connect(self.planetChanged)

self.planetChanged(self, planetButton):
    if planetButton == self.Saturn:
        # do something
    elif planetButton == self.Uranus:
        # do something

您甚至可以通过使用包含每个按钮的有线值的dict来简化:

self._planetVals = {
    self.Saturn: "saturn",
    self.Uranus: "uranus",
}

self.planetChanged(self, planetButton):
    do_stuff_with(self._planetVals[planetButton])

现在针对您的main代码存在问题。需要将其与使用input收集用户输入的依赖关系及其业务逻辑分开:

from math import *

def main():
    dt = eval(input("")) ##time step
    Vo = eval(input(""))
    angle = eval(input(""))
    r=eval(input(""))  ## r= 6.4*10**6 for earth 
    g = eval(input(""))  ## m/s **2

    print process(dt, Vo, angle, r, g)

def process(dt, Vo, angle, r, g):
    loopiterations = 0

    xo = 0
    yo = 0
    angle = angle * (pi / 180)
    Vox = Vo * cos(angle)
    Voy = Vo * sin(angle)
    y =  (yo + Voy * dt)
    Vy=Voy - g * dt
    iterations = 0

    while not (Vy < 0):
        y =  (yo + Voy * dt)
        Vy= (Voy - g * dt)
        x = (xo + Vox * dt)
        Vx = Vox
        iterations = iterations + 1
        yo = y
        xo = x
        Vox = Vx
        Voy = Vy
    # print (y)
    # print(iterations)
    return y, iterations

现在您可以自由导入该模块并使用process函数,通过传递从您的UI收集的值

这个难题的缺失部分是创建一个主要的PyQt4窗口类,它使用您的Qt Designer UI和您的通用业务逻辑来运行该应用程序。

答案 2 :(得分:0)

查看您发布的代码,您需要做的第一件事就是保留pyuic生成的模块:不要编辑它。

相反,您应该将其导入到一个单独的模块中,其中包含所有主程序逻辑。这将允许您在Qt Designer中更改内容并重新生成ui模块而不会干扰其他代码。

下面的脚本将示例中的两段代码链接在一起。保存为projectile.py,然后使用pyuic重新生成您的ui模块,并将其另存为projectile_ui.py

显然,您需要稍微调整一下脚本,以便按照您想要的方式运行,但希望它能帮助您入门。

from math import*
from PyQt4 import QtCore, QtGui
from projectile_ui import Ui_Form

constants = {
    'Sun': (0, 0),
    'Mercury': (0, 0),
    'Venus': (0, 0),
    'Earth': (6.4*10**6, 9.81),
    'Mars': (0, 0),
    'Jupiter': (0, 0),
    'Saturn': (0, 0),
    'Uranus': (0, 0),
    'Neptune': (0, 0),
    }

class MyForm(QtGui.QMainWindow, Ui_Form):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.radioButtons = QtGui.QButtonGroup(self)
        self.radioButtons.addButton(self.Sun)
        self.radioButtons.addButton(self.Mercury)
        self.radioButtons.addButton(self.Venus)
        self.radioButtons.addButton(self.Earth)
        self.radioButtons.addButton(self.Mars)
        self.radioButtons.addButton(self.Jupiter)
        self.radioButtons.addButton(self.Saturn)
        self.radioButtons.addButton(self.Uranus)
        self.radioButtons.addButton(self.Neptune)
        self.pushButton.clicked.connect(self.handleCalculate)

    def handleCalculate(self):
        loopiterations = int()

        dt = self.dt.value() # time step

        Vo = self.Vo.value()
        xo = 0
        yo = 0
        angle = self.angle.value()
        angle = angle * (pi / 180)
        Vox = Vo * cos(angle)
        Voy = Vo * sin(angle)
        y =  (yo + Voy * dt)

        iterations = y = r = g = 0

        button = self.radioButtons.checkedButton()

        if button is not None:
            # r= 6.4*10**6 for earth, g= m/s **2
            r, g = constants[str(button.objectName())]

            Vy=Voy - g * dt

            if r > 0 and g > 0:

                while not (Vy < 0):
                    y =  (yo + Voy * dt)
                    Vy= (Voy - g * dt)
                    x = (xo + Vox * dt)
                    Vx = Vox
                    iterations = iterations + 1
                    yo = y
                    xo = x
                    Vox = Vx
                    Voy = Vy

        self.LCDheight.display(y)
        self.lcdTotaltime.display(iterations)

if __name__ == "__main__":

    import sys
    app = QtGui.QApplication(sys.argv)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())