1

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

来自 Qt 设计师

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

3 回答 3

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)在代码中执行类似操作你只需要做一次。

于 2012-08-16T05:07:58.203 回答
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 和您的通用业务逻辑来运行应用程序。

于 2012-08-16T05:38:18.570 回答
0

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

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

下面的脚本将示例中的两段代码链接在一起。将其另存为projectile.py,然后使用重新生成您的 ui 模块pyuic并将其另存为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_())
于 2012-08-16T17:20:02.410 回答