Sunday, May 29, 2022

PyQt6 Dynamic Programming

This demo program shows how to update a python file and reload it to update the running program without restarting the whole python app.

The output:


The code:

inputs.py:

1
2
3
A = 1.6
B = 7.5
C = 1.5

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
import inputs
import importlib
import sys
import os
from PyQt6.QtWidgets import QApplication,  QWidget, QPushButton, QLabel

class Window(QWidget):

    def __init__(self):
        super(Window, self).__init__()

        self.initUI()

    def initUI(self):
        D = inputs.A + inputs.B + inputs.C
        self.lbl1 =  QLabel(('Inputs:  A:  ' + str(inputs.A) + '  B: ' + str(inputs.B) + '  C: ' + str(inputs.C)), self)
        self.lbl1.setGeometry(25, 25, 150, 35)
        
        self.lbl2 =  QLabel('Sum: '+ str(D), self)
        self.lbl2.setGeometry(25, 50, 150, 35)
        
        pb1 = QPushButton('Update', self)
        pb1.setGeometry(25, 100, 120, 30)
        
        pb1.clicked.connect(self.onClick_pb1)
        
        pb2 = QPushButton('Recompile', self)
        pb2.setGeometry(150, 100, 120, 30)        
        pb2.clicked.connect(self.onClick_pb2)
        
        

        self.setGeometry(25, 45, 350, 150)
        self.setWindowTitle('Post 37')
        self.show()
        
 
    def onClick_pb2(self):
 
       importlib.reload(inputs)
       
    def onClick_pb1(self):
 
       D = inputs.A + inputs.B + inputs.C
       self.lbl2.setText('Sum: '+ str(D))
       self.lbl1.setText('Inputs:  A:  ' + str(inputs.A) + '  B: ' + str(inputs.B) + '  C: ' + str(inputs.C))
        



    
def onClick_pb1():
    exit()

def main():

    app = QApplication(sys.argv)
    ex = Window()
    sys.exit(app.exec())


if __name__ == '__main__':
    main()

No comments:

Post a Comment