Issue
Trying to pass varibale from one window to another, so depending on number of this varibale(1 or 2) some buttons will be hiden or not. Was following this video https://youtu.be/u1BvQylSIo8, but when I print
self.ui=Ui_MainWindow(self.fg)
(self.fg
is variable that need to be sent) I got error - TypeError: Ui_MainWindow() takes no arguments
.
I have separate files for ui and py of both windows. If delete parts about self.fg
evrething is working fine.
My first window login.py
:
import sys
from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow, QMessageBox, QWidget
import login_screen
from window_8 import Ui_MainWindow
from login_screen import Ui_Login_screen
class LoginScreen(QMainWindow, Ui_Login_screen, Ui_MainWindow):
def __init__(self):
super(LoginScreen, self).__init__()
self.ui = login_screen.Ui_Login_screen()
self.ui.setupUi(self)
self.ui.pushButton_login.clicked.connect(self.login)
def login(self):
username=self.ui.lineEdit.text()
password=self.ui.lineEdit_2.text()
if username=="admin" and password=="admin1" :
self.transfer()
self.close()
elif username=="user1" and password=="1user1" :
self.transfer()
self.close()
else: self.ui.label_status.setText("ERROR")
def transfer(self):
self.fg=1
self.window = QMainWindow()
self.ui=Ui_MainWindow()
self.ui.setupUi(self.window)
self.window.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
win = LoginScreen()
win.show()
sys.exit(app.exec())
and after changing in def transfer
this self.ui=Ui_MainWindow()
to this self.ui=Ui_MainWindow(self.fg)
got error metioned above.
My second window app.py
:
import sys
import window_8
from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow, QMessageBox, QWidget
from PyQt5.uic import loadUi
from window_8 import Ui_MainWindow
class Window(QMainWindow, Ui_MainWindow, Ui_Help_w):
def __init__(self):
super(Window, self).__init__()
self.ui = window_8.Ui_MainWindow()
self.ui.setupUi(self)
#flag=1 #for test
#self.ui.textEdit_status_N.setPlainText(str(flag))
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())
watched many tutorials and answers here, but all of them are about when main code in the same file with ui.
Solution
The UI_MainWindow class has a constructor that takes no arguments. If you have access to that class's source code, you can add an argument. It would look like:
def __init__(self, num):
#set a variable equal to 'num' that can be used in the class
self.isWindowHidden = num
Also, as a quick tip: "1" and "0" would be better values to use than "1" or "2" simply because those are universally recognized at standing for True or False. Boolean would be appropriate, too.
If you want the value to change after the UI_MainWindow has already been created... you would use signals and slots. This is a crucial feature of PyQt5 development that should be read up on. In essence: signals are emitted by a widget when something happens, and a slot is a receiver of that signal. You can use this to transfer data between two windows, provided that an instance of each window is created and exists.
(This means window 1 can still exist (but remain hidden) after opening window2. Or you can look at QStackedWidget, which requires further reading.)
What would a signal/slot solution look like in your case?
Ideally you should already have a method that handles button display, it could look something like this depending on your description (and I am assuming it would reside within Ui_MainWindow):
def set_button_visibility(self, value):
if value == 1:
#hide buttons
elif value == 2:
#unhide buttons
That method will be our slot, and a signal has to be connected to it. Then the LoginScreen class would then look like this:
class LoginScreen(QMainWindow, Ui_Login_screen, Ui_MainWindow):
buttonToggle = pyqtSignal(int) #signal
def __init__(self):
super(LoginScreen, self).__init__()
self.ui = login_screen.Ui_Login_screen()
self.ui.setupUi(self)
self.ui.pushButton_login.clicked.connect(self.login)
#Create object that will have target slot
self.window = QMainWindow()
self.ui=Ui_MainWindow()
self.ui.setupUi(self.window)
# Connect signal to slot
buttonToggle.connect(self.ui.set_button_visibility)
def login(self):
username=self.ui.lineEdit.text()
password=self.ui.lineEdit_2.text()
if username=="admin" and password=="admin1" :
self.transfer()
self.close()
elif username=="user1" and password=="1user1" :
self.transfer()
self.close()
else: self.ui.label_status.setText("ERROR")
def transfer(self):
self.buttonToggle.emit(1) #emit signal
self.window.show()
Answered By - Unstable Andy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.