-1

how do I create a main.py file, where it will access the child files (screen, login, registration).

first it will open the login screen, and then it will open the main screen.

I made the whole project in pyqt6, and converted to py file.

The way I tried to do it below, it just gives an undefined variable error

from PySide6.QtCore import QCoreApplication
from PySide6.QtWidgets import (QApplication, QMainWindow)
from PyQt6.QtWidgets import QApplication, QMessageBox, QDialog, QMainWindow, QPushButton
from PyQt6.QtCore import QProcess
import os,sys,re,time,requests,pywhatkit,smtplib

from tela import Ui_MainWindow
from additem import Ui_MainWindowIsertBox
from login import Ui_MainWindowAcesso

#MAIN SCREEN CLASS
class telaprincipal(QMainWindow, Ui_MainWindow): #tela
    def __init__(self,*args,**argvs):
        super(telaprincipal,self).__init__(*args,**argvs)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.actionProduto.triggered.connect(self.add)

    def add(self):
        add = cadastrar()
        add.exec_()

#CLASS REGISTER ITEMS
class cadastrar(MainWindowIsertBox): #additem
    def __init__(self,*args,**argvs):
        super(cadastrar,self).__init__(*args,**argvs)
        self.ui = Ui_MainWindowIsertBox()
        self.ui.setupUi(self)

        
#CLASS FOR USER LOG IN
class login(MainWindowAcesso): #login
    def __init__(self,*args,**argvs):
        super(login,self).__init__(*args,**argvs)
        self.ui = Ui_MainWindowAcesso()
        self.ui.setupUi(self)
        self.ui.botao_confirmar_acesso.clicked.connext(self.login)

    def login(self):
        admin = "admin"
        senha = "admin"
        
        user = self.ui.label_inserir_user.text()
        passwd = self.ui.label_inserir_senha.text()
        
        if user == admin and passwd == senha:
            QMessageBox.information(QMessageBox(),"login realizado", "Logged")
            window = telaprincipal()
            window.show()
        else:
            QMessageBox.information(QMessageBox(),"login não realizado", "Logged Failed")


#application
if __name__ == "__main__":
    enter image description here
    app = QApplication(sys.argv)
    Window = login()
    Window.show()
    sys.exit(app.exec())

script

  • I don't fully understand your question but if I understand correctly, you created your ui files using qt designer then converted these to python code then put that code in different .py files which you load into you main file? – hunter May 27 '22 at 19:14
  • if you have .ui files you can just load them directly without having to convert them into python code first – hunter May 27 '22 at 19:15
  • @hunter exactly – PeakyBlinder May 27 '22 at 19:16
  • @hunte what exactly are they called directly in my main.py file? – PeakyBlinder May 27 '22 at 19:19
  • There are several and serious problems in your code: 1. you can **not** mix PySide and PyQt; 2. there is no reference for `MainWindowIsertBox` and `MainWindowAcesso` you're trying to subclass from, which is probably the cause of your error; those should probably be QMainWindow; 3. if `cadastrar` is supposed to be a QMainWindow, `add()` will crash, since QMainWindow has no `exec()`; 4. in `login` you wrote `connext`, but it's `connect`; 5. if you subclass from both QMainWindow and the ui file, you must not create another `ui` object, just do `self.setupUi(self)`. – musicamante May 27 '22 at 22:29
  • 6. the `QMessageBox()` argument in the QMessageBox is just *wrong*; use `self` or `None`; 7. classes should always have capitalized names, functions and variables *not*; 8. the `window` created in `login` will be destroyed immediately, as it has no persistent reference. Besides, for future reference, when you get an error, you must report that exact error: just saying "it just gives an undefined variable error" is completely pointless, we must know to *what* variable the error refers to. Also, try to avoid mixing things taken from various sources without understanding what they are doing. – musicamante May 27 '22 at 22:31
  • I corrected some of the tips you gave me, but I still couldn't call the login, screen and registration files. :( @musicamante – PeakyBlinder May 28 '22 at 15:07
  • now the classes are capitalized, I removed the Pyside, and I also fixed the connext to connect – PeakyBlinder May 28 '22 at 15:10
  • @PeakyBlinder did you change the inherited classes to *actual* Qt classes? Please [edit] your question and update the code according to your changes. – musicamante May 28 '22 at 21:36

1 Answers1

0

The way I'd go is that I'd load the .ui files I created into my main file directly (skiping the convertion from .ui to .py part)

See below code for reference:

class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi('get_input.ui', self)
        self.setWindowTitle("Game")
        self.pushButton.pressed.connect(self.save)
        self.show()

    def save(self):
        self.algorithm = self.comboBox.currentText()
        self.close()

Here I have created a ui file called get_input.ui and I load directly onto my code then I interacted with it by getting some input from it. It's much easier this way.

musicamante
  • 41,230
  • 6
  • 33
  • 58
hunter
  • 36
  • 6
  • sorry I'm starting in the python area. so i have a hard time understanding how to make it work – PeakyBlinder May 27 '22 at 19:36
  • I tried it in my script by changing the value of get_input.ui. to login.ui but it opens a blank window – PeakyBlinder May 27 '22 at 19:37
  • Are you sure you created an instance of your class? (usually when a blank page is loaded it's because of that) Something similar to this: `app = QtWidgets.QApplication(sys.argv) window = Ui() app.exec_() ` – hunter May 27 '22 at 19:40
  • please make sure you use above code by typing each line in a new line (stackoverflow doesn't support new line in comments) – hunter May 27 '22 at 19:43
  • that way you taught, can I do the same way to call buttons, parameters? – PeakyBlinder May 27 '22 at 19:56
  • Yes you can. In your UI class you just call the button (or any other element) by using the element's label (defined in your .ui file in qt designer on the top right corner for example if I have button with label "button_1" I can call it using`self.button_1` and similarly you can call its parameters – hunter May 27 '22 at 20:01
  • [1]: https://i.stack.imgur.com/bnSEE.png Look ? – PeakyBlinder May 27 '22 at 20:16
  • Are you currently facing any problems? – hunter May 27 '22 at 20:19
  • if I proceed with this model you taught, I need to configure again the functions I created for the model I converted from ui. ? another question, once it's ready how would I create the executable file? – PeakyBlinder May 27 '22 at 20:29
  • All functions are related to design (background color, fonts, size, position on the screen etc.) will be automatically loaded from your UI files you don't need to write them, the only functions you'll need to write are the ones controlling the behaviour of your program. – hunter May 27 '22 at 20:32
  • As for creating executable files, try using `pyinstaller` it's easy and quick – hunter May 27 '22 at 20:33
  • Also before proceeding with the model I explained, please make sure that it works and you're comfortable working with it before converting the rest of the Ui files. – hunter May 27 '22 at 20:34
  • 1
    @hunter the preferred signal for buttons is [`clicked`](//doc.qt.io/qt-5/qabstractbutton.html#clicked), not `pressed`. The difference is important: `pressed` is called when the mouse button is *pressed* on the push button (or returned after leaving it with the mouse button still pressed), not released, but the general convention for "click" is when the button is released and the mouse is still *inside* the push button. This is to allow unwanted "clicks" when pressing the button by mistake: you can "cancel" the pressure by keeping the mouse button pressed and *moving* the mouse outside of it. – musicamante May 27 '22 at 22:17
  • @hunter how do you create a returnpressed on the login screen? – PeakyBlinder May 29 '22 at 21:53
  • Can you please explain what you're trying to return? – hunter May 30 '22 at 00:37