Monday, May 2, 2022

Global Variables Handling across PyQt6 Windows

This demo program shows how to handle global variables across files or PyQt6 windows and execute a function from another file.


The output:




The code:

config.py:

1
2
a = 0
b = "empty"

update.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import config

config.a = 10
config.b = "alphabet"

def update(arg1, arg2):
    config.a = arg1
    config.b = arg2
    
def add(arg1, arg2):
    return arg1 + arg2

main.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from imports import *
from main1 import WindowApp1
import config
import update



class WindowApp(a.QMainWindow):
    def __init__(self, parent=None):
        super().__init__()
        self.setWindowTitle("Post 31")
        self.setFixedSize(320, 245)
        
        lbla =  a.QLabel('Global a: ', self)        
        lbla.setGeometry(25, 55, 100, 30) 
        
        self.txta = a.QLineEdit(str(config.a), self)        
        self.txta.setGeometry(100, 55, 50, 30) 
        
        self.lbla_r =  a.QLabel(str(config.a), self)        
        self.lbla_r.setGeometry(170, 55, 50, 30)
        
        lblb =  a.QLabel('Global b: ', self)        
        lblb.setGeometry(25, 100, 100, 30) 
        
        self.txtb = a.QLineEdit(config.b, self) 
        self.txtb.setGeometry(100, 100, 50, 30) 
        
        self.lblb_r =  a.QLabel(config.b, self)        
        self.lblb_r.setGeometry(170, 100, 50, 30) 
        
        self.pblogin = a.QPushButton('Login', self)
        self.pblogin.setGeometry(50, 195, 65, 25)
        self.pblogin.clicked.connect(self.onClick_pb3)
        
        pbcanc = a.QPushButton('Update', self)
        pbcanc.setGeometry(125, 195, 65, 25)
        pbcanc.clicked.connect(self.onClick_pb4)
        
        pbcalc = a.QPushButton('Function', self)
        pbcalc.setGeometry(200, 195, 65, 25)
        pbcalc.clicked.connect(self.onClick_pb5)
        
        lblf =  a.QLabel('Function: ', self)        
        lblf.setGeometry(25, 140, 75, 30) 
        
        self.txtfa = a.QLineEdit(self) 
        self.txtfa.setGeometry(100, 140, 40, 30)
        
        lblfp =  a.QLabel('+', self)        
        lblfp.setGeometry(150, 140, 10, 30) 
        
        self.txtfb = a.QLineEdit(self) 
        self.txtfb.setGeometry(165, 140, 40, 30)
        
        self.lblf_r =  a.QLabel( self)        
        self.lblf_r.setGeometry(215, 140, 40, 30)
        
    def onClick_pb3(self):
        self.mdiwindow = WindowApp1()
        self.mdiwindow.show()
        self.hide()
    def onClick_pb4(self): 
          
        a = int(self.txta.text())
        b = self.txtb.text()

        update.update(a,b)
        
        self.lbla_r.setText(str(config.a))
        self.lblb_r.setText(config.b)
 
    def onClick_pb5(self):
        self.lblf_r.setText('= ' + str(update.add(int(self.txtfa.text()),int(self.txtfb.text())))) 

if __name__ == "__main__":       
    app = a.QApplication(s.argv)
 
    splash = WindowApp()
    splash.show()
    s.exit(app.exec())        

main1.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from imports import *
import config




class WindowApp1(a.QMainWindow):
    def __init__(self, parent=None):
        super().__init__()
        self.setWindowTitle("Post 31")
        self.setFixedSize(320, 245)
        self.lbla =  a.QLabel('Global a: ', self)        
        self.lbla.setGeometry(25, 55, 100, 30) 
        
        self.lblb =  a.QLabel('Global b: ', self)        
        self.lblb.setGeometry(25, 100, 100, 30)
        
        self.pblogin = a.QPushButton('End', self)
        self.pblogin.setGeometry(100, 165, 65, 25)
        self.pblogin.clicked.connect(self.onClick_pb3)
        pbcanc = a.QPushButton('Print', self)
        pbcanc.setGeometry(180, 165, 65, 25)
        pbcanc.clicked.connect(self.onClick_pb4)
        
        
    def onClick_pb3(self):       
        self.close()
        
    def onClick_pb4(self): 
     
        self.lbla.setText('Global a: ' + str(config.a))
        self.lblb.setText('Global b: ' + config.b)
if __name__ == "__main__":       
    app = a.QApplication(s.argv)
 
    splash = WindowApp1()
    splash.show()
    s.exit(app.exec())          

No comments:

Post a Comment