1

I am making a GUI and have a code with several files and uses a controller file to switch between the files. However, I need several of the variables to be available in the other files and also want an own file where I can keep track of the values for all the variables. I have now instantiated the variables on top of the file, and tried to change the values in the class below, but if I then import to another file it will only give the value which was instantiated first (which is fair since I did not call the class, but is a problem). Please help. Under i some of the code:

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

And in the controller file

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

And in the MainWindow file where I want to use LEVELS

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()
AuroraS
  • 59
  • 8

2 Answers2

1

Avoid abusing the global variables(1), and in this case it is not necessary, you must make the dynamic creation of widgets a moment before making the change in the show_main method:

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) Why are global variables evil?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
0

Ended up doing this, which worked! Instantiated all variables from login window before calling login.close. Also passed in variables needed in next function. In this way I was also able to create a function which prints out the parameters.

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()
AuroraS
  • 59
  • 8