Python Forum

Full Version: Integrate/import a gui code to my main.py
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, need some help here.

I wrote a code that has a GUI base on pysimplegui.

This gui was a class in my maincode.py:

class TelaPython:
    
    def __init__(self):
        with open('./weapon_data/parameter1.json', 'r') as f:
            config = json.load(f)
        sg.change_look_and_feel('Black')
        layout = [
            [sg.Text('', size=(15, 1)), key='text1')]            
        ]

        janela = sg.Window('Recoil Control System', layout, location=(0,0), icon='./gui/test1.ico')        
        janela.Close()

    def Iniciar(self):
        #read values of buttons     
        print('ok')


tela = TelaPython()
tela.Iniciar()
after the gui read the value false/positive of a check box the code pass to the main function of the code (after gui is destroyed/closed).

now I paid a guy to make a modern gui based on pyqt5 and and he made it like the main code. But my code just call the gui once and the gui dont need do run while the main loop is running. In other words the gui is just the front door that can be closed after the user "enters the house" and do the main thing.

[Image: Capturar.png]

This is the image of pyqt5 gui. The main code is the App.py that I would like to integrate to my main code just to call it once

like """tela.Iniciar()"""

Can someone help? ty

Here is the pyqt5 gui core (App.py):

from  modules import *
from PyQt5.QtWidgets import *
from functools import partial
from PyQt5.QtGui import *
from PyQt5 import *
from PyQt5.QtCore import *
import os
import sys
from PyQt5.QtGui import *
from Uiapp import Ui_Form
from functools import partial
from splash_screen import Ui_SplashScreen
counter = 0
class MainWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.dick={
            "main_configuration":{
                "Reconhecer_slot" : False,
                "res1":True,
                "res2":False,
                "overlay":False,
                "sistema_agach":False,
                "medio":True,
                "sons" :False ,
                "alto":False,
                "baixo":False,
                "baixissimo" : False,
                "calibragrem" : 100
            }
                #############################
            }
        ############################################################# them and init home 
        Theme.inint_ui(self)
        Theme.actionbutton(self)
        ############################################################# main init  and action button  
        Main.inint_ui(self)
        Main.actionbutton(self)
        ############################################################# wp init and action button 
        Wp.inint_ui(self)
        Wp.actionbutton(self)
        ############################################################# about init and action button
        About.inint_ui(self)
        About.actionbutton(self)
class Spalch_screen(QMainWindow ):
    def __init__(self):
        QMainWindow.__init__(self)
        self.counter = 0
        self.counter2 = 0
        self.ui = Ui_SplashScreen()
        self.ui.setupUi(self)
        self.timer = QTimer()
        self.timer.setInterval(1000)
        self.timer.start()
        # remove titel and fram 
        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.shadow = QGraphicsDropShadowEffect(self)
        self.shadow.setBlurRadius(20)
        self.shadow.setXOffset(5)
        self.shadow.setYOffset(5)
        self.shadow.setColor(QColor(0, 0, 0, 60))
        self.ui.dropShadowFrame.setGraphicsEffect(self.shadow)
        self.main = MainWindow()
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.progress2)
        self.timer.start(25)
        self.ui.label_loading.setText("<strong>WELCOME</strong> TO MY APPLICATION")
        QtCore.QTimer.singleShot(1500, lambda: self.ui.label_loading.setText("<strong>LOADING</strong> ..."))
        QtCore.QTimer.singleShot(3000, lambda: self.ui.label_loading.setText("<strong>LOADING</strong> USER INTERFACE"))
        self.show()
    def progress2(self):
        global counter
        self.ui.progressBar.setValue(counter)
        if counter > 100:
            self.timer.stop()
            self.main.show()
            self.close()
        counter +=1
        
if __name__ == "__main__":
    app = QApplication(sys.argv)  
    window = Spalch_screen()
    sys.exit(app.exec_())
remove the bracket in line 35, unmatched
(Feb-03-2021, 07:45 PM)Axel_Erfurt Wrote: [ -> ]remove the bracket in line 35, unmatched

Thank you!

Any tips to integrate it to maind code "like" the pysimplegui based gui?
This is not possible without knowledge of the other modules Uiapp, splash_screen ...
What you describe is not how pysimplegui works either. You should have had some code in your pysimplegui app that looked like this:
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel
        break
    ...
This is what QApplication.exec_() does.

It sounds like what you had with pysimplegui was a gui program that didn't work properly and Qt is making it more difficult for you to do that.
You're absolutly right. The simplegui was working but not properly.
How you go about doing the "main" part depends on what "main" is doing. If main can be done quickly, you can put the main processing in a function and bind a control to call the function. If main takes a long time but can be broken up into little pieces of work you can implement main as a state machine and attach it to an event to be worked on whenever the GUI is not busy processing other events. If main is some a monolithic bunch of processing you might want to do main as a subprocess or do it in a separate thread. As Axel_Erfurt says, nobody can offer any useful suggestions without knowing what main does.