我正在尝试创建一个将从主窗口接收整数输入的程序,如果用户在子窗口中选择“否”,我想从主窗口获取这些输入并在子窗口中计算一些值。
我尝试在主窗口中调用restemp和respres,然后在子窗口中使用它,但是它说Ui_Sgselect()(子窗口中的类)不能接受任何参数
主窗口的第一部分:
class Ui_DryGasProperties(object):
def afterselect1(self):
#this function opens up the child window after hitting select in the main window
if self.Firstselect.currentText() == 'Specific Gravity':
self.Tp = self.Restemp.text()
self.Pp = self.respres.text()
#Restemp and Respres are the integer user inputs I want to use
self.ui = Ui_Sgselect() #Sgselect is the new window class
self.window = QtWidgets.QWidget()
self.ui.setupUi(self.window)
self.window.show()
子窗口如下:
class Ui_Sgselect(object):
def secondselect(self):
#the function when submit is clicked in the child window
if self.Nonhydrselect.currentText() == 'Yes':
self.ui = Ui_Afternonhydr() #other windows that open afterwards
self.window = QtWidgets.QWidget()
self.ui.setupUi(self.window)
self.window.show()
elif self.Nonhydrselect.currentText() == "I don't know":
self.ui = Ui_Dialog()
self.window = QtWidgets.QDialog()
self.ui.setupUi(self.window)
self.window.show()
elif self.Nonhydrselect.currentText() == 'No':
#this is where the value from main window has to be used
sg1 = float(self.SG.text())
Tpc = 168 + (325 * sg1) - (12.5 * (sg1 ** 2))
Ppc = 677 + (15 * sg1) - (37.5 * (sg1 ** 2))
#Tp1 = float(self.Tp)
#Pp1 = float(self.Pp)
#Tpr = (Tp1 + 460) / Tpc
#Ppr = Pp1 / Ppc
#Tpr1 = round(Tpr, 2)
#Ppr1 = round(Ppr, 2)
print(Tpr)
print(Ppr)
编程的新手。任何帮助将不胜感激。
答案 0 :(得分:0)
老实说,我会更改您的类结构,以便在更新UI时不会更改您的方法或不小心删除它们。
因此,您从QtDesigner获得的代码就不用理会了,让另一个类继承Ui,如下所示。
您可以将窗口作为参数传递给正在初始化的Sgselect类。
这是假设所有Ui都在同一模块中。
from PyQt5 import QtCore,QtGui,QtWidgets
#ui for all windows go between here
class Dialog(QtWidgets.QDialog,Ui_Dialog):
def __init__(self,parent=None):
super().__init__()
self.setupUi(self)
class Afternonhydr(QtWidgets.QMainWindow,Ui_Afternonhydr):
def __init__(self,parent=None):
super().__init__()
self.setupUi(self)
class DryGasProperties(QtWidgets.QMainWindow,Ui_DryGasProperties)
def __init__(self,parent=None):
super().__init__()
self.setupUi(self)
def afterselect1(self):
if self.Firstselect.currentText() == 'Specific Gravity':
self.Tp = self.Restemp.text()
self.Pp = self.respres.text()
self.SgselectWindow = Sgselect(self)
self.SgselectWindow.show()
class Sgselect(QtWidgets.QMainWindow,Ui_Sgselect):
def __init__(self,window,parent=None):
super().__init__()
self.setupUi(self)
self.Tp = window.Tp
self.Pp = window.Pp
def secondselect(self):
#the function when submit is clicked in the child window
if self.Nonhydrselect.currentText() == 'Yes':
self.afternonhydrWindow = Afternonhydr()
self.afternonhydrWindow.show()
elif self.Nonhydrselect.currentText() == "I don't know":
self.dialogWindow = Dialog()
self.dialogWindow.show()
elif self.Nonhydrselect.currentText() == 'No':
sg1 = float(self.SG.text())
Tpc = 168 + (325 * sg1) - (12.5 * (sg1 ** 2))
Ppc = 677 + (15 * sg1) - (37.5 * (sg1 ** 2))
Tp1 = float(self.Tp)
Pp1 = float(self.Pp)
Tpr = (Tp1 + 460) / Tpc
Ppr = Pp1 / Ppc
Tpr1 = round(Tpr, 2)
Ppr1 = round(Ppr, 2)
print(Tpr)
print(Ppr)
或不用传递整个窗口作为参数,而只需指定要发送到另一个窗口的项目即可。例如,
将class Sgselect
中的代码部分更改为以下内容;
self.SgselectWindow = Sgselect(self.Tp,self.Pp)
和
def __init__(self,Tp,Pp,parent=None):
super().__init__()
self.setupUi(self)
self.Tp = Tp
self.Pp = Pp
要在不同的模块中使用不同的用户界面,只需将它们添加到不同的py文件中,然后将其导入顶部,例如from [insert py file name here] import [ui class name]