Python Forum
Integrate/import a gui code to my main.py
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Integrate/import a gui code to my main.py
#1
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_())
Reply
#2
remove the bracket in line 35, unmatched
penahuse likes this post
Reply
#3
(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?
Reply
#4
This is not possible without knowledge of the other modules Uiapp, splash_screen ...
Reply
#5
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.
penahuse likes this post
Reply
#6
You're absolutly right. The simplegui was working but not properly.
Reply
#7
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  try catch question ,get data from main code korenron 7 827 Nov-03-2020, 09:28 AM
Last Post: korenron
  Code import .CSV file to MySQL table rtakle 4 1,072 Apr-30-2020, 03:16 PM
Last Post: anbu23
  Error in python code 'import callbot' Lou_Reed 3 1,086 Jan-07-2020, 09:33 PM
Last Post: Clunk_Head
  Why is from code import * different from entering that code directly into the REPL? sebi 5 1,496 Apr-03-2019, 11:40 AM
Last Post: sebi
  code keeps running if i use from threading import timer? birddseedd 3 1,155 Jan-25-2019, 05:00 AM
Last Post: stullis
  ImportError: cannot import name 'Main' => Cannot install module with pip Oby4455 5 16,923 Jan-09-2018, 06:51 PM
Last Post: Gribouillis
  project/main.py import from project/bin/APP_NAME Myersj281 3 2,482 Jul-19-2017, 04:41 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020