我正在制作一个GUI,并具有包含多个文件的代码,并使用控制器文件在这些文件之间切换。但是,我需要几个变量在其他文件中可用,并且还需要一个自己的文件,在其中可以跟踪所有变量的值。 现在,我已经实例化了文件顶部的变量,并尝试更改以下类中的值,但是如果我随后将其导入另一个文件,它将仅给出首先实例化的值(这很公平,因为我没有调用类,但有问题)。 请帮忙。 在我下面的一些代码:
来自文件firstwindow
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
LEVELS = 2
NUM_ICE = 4
NUM_CONES = 8
class Login(QtWidgets.QWidget):
switch_window = QtCore.pyqtSignal()
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)
self.setWindowTitle('First')
def setupUi(self, FirstWindow):
FirstWindow.setObjectName("First")
FirstWindow.setEnabled(True)
FirstWindow.resize(675,776)
FirstWindow.setFocusPolicy(QtCore.Qt.TabFocus)
layout = QtWidgets.QGridLayout()
self.spinBoxNUM_ICE = QtWidgets.QSpinBox()
self.spinBoxNUM_CONES = QtWidgets.QSpinBox()
self.spinBoxLEVELS = QtWidgets.QSpinBox()
layout.addWidget(self.spinBoxNUM_MASTERS,1,2)
layout.addWidget(self.spinBoxNUM_SLAVES,2,2)
layout.addWidget(self.spinBoxPRIORITY_LEVELS,11,2)
#CONTINUE AND QUIT BUTTON
self.QuitButton = QtWidgets.QPushButton("Quit")
self.QContinueButton = QtWidgets.QPushButton("Continue")
#actions
self.QuitButton.clicked.connect(FirstWindow.close)
self.QContinueButton.clicked.connect(self.login)
def login(self):
#global NUM_ICE
self.NUM_ICE = self.spinBoxNUM_ICE.value()
global NUM_CONES
NUM_CONES = self.spinBoxNUM_CONES.value()
global LEVELS
LEVELS = self.spinBoxLEVELS.value()
self.switch_window.emit()
并在控制器文件中
class Controller:
def __init__(self):
pass
def show_login(self):
self.login = Login()
self.login.switch_window.connect(self.show_main)
self.login.show()
def show_main(self):
self.window = MainWindow()
self.window.switch_window.connect(self.show_window_two)
self.login.close()
self.window.show()
在要使用LEVELS的MainWindow文件中
import sys
from PyQt5 import QtCore, QtWidgets
from firstwindow import LEVELS
class MainWindow(QtWidgets.QWidget):
switch_window = QtCore.pyqtSignal()
#switch_window = QtCore.pyqtSignal(str)
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self, LEVELS)
self.setWindowTitle('PriorityMap')
def setupUi(self, PriorityMap, LEVELS):
PriorityMap.setObjectName("First")
PriorityMap.setEnabled(True)
PriorityMap.resize(675,776)
PriorityMap.setFocusPolicy(QtCore.Qt.TabFocus)
layout = QtWidgets.QGridLayout()
#CREATING ELEMENTS
for i in range(0,LEVELS+2):
for j in range(0,5):
if (i==0 and j!=0):
layout.addWidget(QtWidgets.QLabel(str(j-1)),i,j)
elif (j==0 and i!=0):
layout.addWidget(QtWidgets.QLabel("LEVEL"+str(i-1)),i,j)
else:
layout.addWidget(QtWidgets.QPushButton(str(i)+","+str(j)),i,j)
#CONTINUE AND QUIT BUTTON
self.QuitButton = QtWidgets.QPushButton("Quit")
self.QContinueButton = QtWidgets.QPushButton("Continue")
#actions
self.QuitButton.clicked.connect(PriorityMap.close)
self.QContinueButton.clicked.connect(self.switch)
#LAYOUT
layout.addWidget(self.QuitButton,15,1)
layout.addWidget(self.QContinueButton,15,2)
self.setLayout(layout)
def switch(self):
self.switch_window.emit()
答案 0 :(得分:0)
避免滥用全局变量(1),在这种情况下,没有必要,必须在show_main方法中进行更改之前先动态创建小部件:
class Controller:
def show_login(self):
self.login = Login()
self.login.switch_window.connect(self.show_main)
self.login.show()
def show_main(self):
self.window = MainWindow()
levels = self.login.spinBoxLEVELS.value()
self.window.setLevels(levels)
self.window.switch_window.connect(self.show_window_two)
self.login.close()
self.window.show()
class MainWindow(QtWidgets.QWidget):
switch_window = QtCore.pyqtSignal()
# switch_window = QtCore.pyqtSignal(str)
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)
self.setWindowTitle('PriorityMap')
def setupUi(self, PriorityMap):
PriorityMap.setObjectName("First")
PriorityMap.setEnabled(True)
PriorityMap.resize(675,776)
PriorityMap.setFocusPolicy(QtCore.Qt.TabFocus)
layout = QtWidgets.QVBoxLayout(self)
self.m_content_widget = QtWidgets.QWidget()
layout.addWidget(self.m_content_widget, stretch=1)
#CONTINUE AND QUIT BUTTON
self.QuitButton = QtWidgets.QPushButton("Quit")
self.QContinueButton = QtWidgets.QPushButton("Continue")
#actions
self.QuitButton.clicked.connect(PriorityMap.close)
self.QContinueButton.clicked.connect(self.switch)
w = QtWidgets.QWidget()
hlay = QtWidgets.QHBoxLayout(w)
hlay.addWidget(self.QuitButton)
hlay.addWidget(self.QContinueButton)
layout.addWidget(w, alignment=QtCore.Qt.AlignRight)
def setLevels(self, levels):
layout = QtWidgets.QGridLayout(self.m_content_widget)
for i in range(0,levels+2):
for j in range(0, 5):
if (i==0 and j!=0):
layout.addWidget(QtWidgets.QLabel(str(j-1)),i,j)
elif (j==0 and i!=0):
layout.addWidget(QtWidgets.QLabel("LEVEL"+str(i-1)),i,j)
else:
layout.addWidget(QtWidgets.QPushButton(str(i)+","+str(j)),i,j)
def switch(self):
self.switch_window.emit()
答案 1 :(得分:0)
最终这样做了,结果奏效了! 在调用login.close之前,从登录窗口实例化所有变量。还传递了下一个函数所需的变量。这样,我还可以创建一个输出参数的函数。
class Controller:
def __init__(self):
pass
def show_login(self):
self.login = Login()
self.login.switch_window.connect(self.show_main)
self.login.show()
def show_main(self):
self.LEVELS = self.login.LEVELS
self.window = MainWindow(self.LEVELS)
self.window.switch_window.connect(self.show_window_two)
self.login.close()
self.window.show()
def writetofile(Controller):
f = open("f.txt", "w+")
f.write("int LEVELS = %d;\n\n" %Controller.LEVELS)
f.close()