Opening another window through pushbutton in GUI in PyQt5 - python

I am new to Python and to PyQt. I have designed GUIs in MATLAB so its been a frustrating new experience. Right now, I have a main window and I want to open another login window on button push. What I have not decided is whether it should return the value of login to the parent window or another window. But either way I need to be able to open the login window through the pushbutton. I have created 2 files declaring the GUI one as a mainwindow and another as Dialog (does not seem like it is inheriting from QDialog though). The other 2 py files are classed calling the UIs separately and work fine. Each have their functions. I am pasting the main.py (mainwindow init and onOpen function code) along with logindialog.py init function code. Kindly help or else I will have to go back to Matlab where I can do this quite easily.`
import sys
from PyQt5 import QtWidgets, QtGui
from LoginDialog import LoginDialog
import UI_MSLDB_Main
from UI_LoginDialog import Ui_LoginDialog
#import Helper
#import Auth
#import TestFeature01
class Main(QtWidgets.QMainWindow, UI_MSLDB_Main.Ui_MSLDatabase):
def _init_(self, parent = None):
QtWidgets.QMainWindow._init_(self)
self.setupUi(self)
self.child = LoginDialog(self)
self.child.setupUi(QtWidgets.QDialog())
# self.createConnections()
self.pushButton_OpenPrimarylist.clicked.connect(self.onOpen)
def onOpen(self):
# window = QtWidgets.QDialog
# self.child = LoginDialog(window, Ui_LoginDialog)
# self.child.show()
# exec('LoginDialog.py')
self.child.show
# self.actionStudy.triggered.connect()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
# LoginDialog(QDialog, Ui_LoginDialog)
# loginDialog = LoginDialog(QDialog, UI_LoginDialog)
# username = loginDialog.username
# password =loginDialog.password
# Helper.dbConnect(username,password)
# isAuth = False
# result = -1
# while not isAuth:
# result = loginDialog.exec()
# if result == LoginDialog.Success or result == LoginDialog.Rejected:
# isAuth = True
# else:
# isAuth = False
result = 1
if result == 1:#LoginDialog.Success:
MSLDatabase = QtWidgets.QMainWindow()
ui = UI_MSLDB_Main.Ui_MSLDatabase()
ui.setupUi(MSLDatabase)
MSLDatabase.show()
# w.show()
app.exec_()
sys.exit(app.exec_())
There were multiple things I tried not knowing which would work. The commented codes are the ones I have tried and failed. LoginDialog class is below.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QDialog
from PyQt5.QtWidgets import QMessageBox
import Auth
import Helper
from UI_LoginDialog import Ui_LoginDialog
class LoginDialog(QDialog, Ui_LoginDialog):
Success, Failed, Rejected, username, password = range(0,5)
def _init_(self):
QDialog._init_(self)
self.setupUi(self)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL
(_fromUtf8("accepted()")),self.onAccept)
QtCore.QObject.connect(self.buttonBox,
QtCore.SIGNAL(_fromUtf8("rejected()")),
self.onReject)

Related

PYQT6 and QT designer, after opening second window, buttons wont work

So i am making a GUI to identify seagulls.
I have made some QMainWindow's and would like the user to nagviate through these using buttons.
The first window works fine, and the user gets to the next page.
However, when clicking on buttons on page 2, nothing works, not even printing a simple statement. I am new to this and i am doing something wrong. I tried messing with init but am at a loss.
I am using a function
def openNewPage(self,b):
NewPage = b.text()
uic.loadUi(f"{NewPage}.ui",self)
self.show
to open a new page by having the buttons clicked on by the user have the name of the next file to be opened.
For example: the button for white head has the text "WhiteHead"
and will the open the file WhiteHead.ui
example:
self.bWhiteHead.clicked.connect(lambda:self.openNewPage(self.bWhiteHead))
Please help me understand why buttons work in one window and not in the next, thank you.
Here is the code:
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow
from PyQt6.QtGui import QIcon
from PyQt6.QtCore import Qt
from PyQt6 import uic
import time
class MyApp(QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi("SeagullPage1.ui",self)
self.setWindowTitle("SEAGULL IDENTIFIER")
self.bBlackHead.clicked.connect(lambda:self.openNewPage(self.bBlackHead))
self.bWhiteHead.clicked.connect(lambda:self.openNewPage(self.bWhiteHead))
def openNewPage(self,b):
NewPage = b.text()
uic.loadUi(f"{NewPage}.ui",self)
self.show
class WhiteHead(QMainWindow):
def __init__(self):
super().__init__
self.setWindowTitle("SEAGULL IDENTIFIER2")
self.BlackFeetButton.clicked.connect(lambda:self.openNewPage(self.BlackFeetButton))
self.PinkFeetButton.clicked.connect(lambda:self.openNewPage(self.PinkFeetButton))
self.YellowFeetButton.clicked.connect(lambda:self.openNewPage(self.YellowFeetButton))
def openNewPage(self,b):
print("hey")
NewPage = b.text()
uic.loadUi(f"{NewPage}.ui",self)
self.show
class YellowFeet(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("SEAGULL IDENTIFIER3")
self.RedBeakButton.clicked.connect(lambda:self.openNewPage(self.RedBeakButton))
self.NonReadBeakButton.clicked.connect(lambda:self.openNewPage(self.NonReadBeakButton))
def openNewPage(self,b):
print("hey")
NewPage = b.text()
uic.loadUi(f"{NewPage}.ui",self)
self.show
if __name__ == "__main__":
app = QApplication(sys.argv)
myApp = MyApp()
myApp.show()
app.exec()
#sys.exit(app.exec_())

Why dont my buttons work in my pyqt5 GUI?

When I made a GUI i used different classes to construct the main UI Screen.
My code has the following structure:
This is the GUI itself:
The bottum_buttons.py creates the 3 buttons at the buttom. This is the code that is inside bottum_buttons.py:
import Advanced_window
from PyQt5 import QtCore
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from datetime import datetime
import calendar
import sys
class bottum_buttons(QWidget):
def __init__(self):
QWidget.__init__(self)
# Create Layout
self.bottum_box = QHBoxLayout()
# Creating Buttons
self.cancel_button = QPushButton("Cancel")
self.run_button = QPushButton("Run")
self.advanced_button = QPushButton("Advancend Options")
self.add_items_to_layout()
self.create_button_functions()
def add_items_to_layout(self):
self.bottum_box.addWidget(self.cancel_button)
self.bottum_box.addWidget(self.run_button)
self.bottum_box.addWidget(self.advanced_button)
def create_button_functions(self):
self.cancel_button.clicked.connect(self.close)
self.advanced_button.clicked.connect(Advanced_window.advancedwindows)
def return_bottum_buttons(self):
return self.bottum_box
My code that actually constructs the GUI is inside main_screen.py.
The following code is inside this file:
from Ui_Elements import option_box
from Ui_Elements import path_box
from Ui_Elements import bottum_buttons
from Ui_Elements import command_output
from PyQt5 import QtCore
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from datetime import datetime
import calendar
import sys
class main_screen(QDialog):
def __init__(self):
QDialog.__init__(self)
self.setWindowTitle("Robo Tool")
self.main_frame = QVBoxLayout()
# Get UI Elements
path_ui = path_box.path_box()
option_ui = option_box.option_box()
command_ui = command_output.command_box()
bottum_ui = bottum_buttons.bottum_buttons()
self.path = path_ui.return_path_box()
self.option_box = option_ui.return_options_box()
self.command_output = command_ui.return_command_box()
self.bottum_buttons = bottum_ui.return_bottum_buttons()
self.setLayout(self.add_item_to_frame(self.main_frame))
def add_item_to_frame(self, main_frame):
main_frame.addLayout(self.path)
main_frame.addLayout(self.option_box)
main_frame.addLayout(self.command_output)
main_frame.addLayout(self.bottum_buttons)
return main_frame
app = QApplication(sys.argv)
dialog = main_screen()
dialog.show()
app.exec_()
Now the problem is. When i start the main_screen.py the GUI shows up as the picture provided. But the buttons don't work. I dont get any error message. They're still clickable but they dont run the command i provided. Can somebody please help me out.
I don't know what Advanced_window.advancedwindows() is supposed to do, but your cancel button is connected to bottom_buttons.close, not to main_screen.close which I assume is what you want. Since bottom_buttons has no knowledge in advance about which window is supposed to be closed, you can't really connect the button to the close method of a predefined widget. What you could do however is to use self.window().close() instead which would close the next-level ancestor widget of bottom_buttons that has a window. For this, you would need to set the layout of bottom_bottuns to self.bottom_box and add the whole widget to the layout of main_screen rather than just the layout.
This would mean that you would get something like this for bottom_buttons:
class bottum_buttons(QWidget):
def __init__(self):
.... as before ....
# set bottom_box as layout of self
self.setLayout(self.bottom_box)
....
def create_button_functions(self):
# connect cancel button to close method of top level ancestor widget
self.cancel_button.clicked.connect(lambda: self.window().close())
....
And for main_screen:
class main_screen(QDialog):
def __init__(self):
.... as before ....
# set self.bottom_buttons to bottom_buttons widget rather than the layout
self.bottum_buttons = bottum_ui
self.setLayout(self.add_item_to_frame(self.main_frame))
def add_item_to_frame(self, main_frame):
...
# add bottom_buttons widget to the layout.
main_frame.addWidget(self.bottum_bottons)
return main_frame

How to jump from 1 qt window to other in pyqt5.?

I am working on pyqt5 project where I have login window and registration window. I have a button on login window which when clicked should open the registration window. I have designed the ui in qt desginer and have converted the .ui files to .py files using pyuic.
So I have two files login_ui.py and register_window_ui.py. To use them, I have also created two separate files i.e. login.py and register.py which contains all the functional code of the ui files.
Below is the files code:
login.py
import sys
import os
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from ui.login_ui import Ui_login_main_window
from ui.register_window_ui import Ui_register_window
curr_path = os.path.dirname(os.path.abspath(__file__))
class Login(QMainWindow, Ui_login_main_window):
def __init__(self):
QMainWindow.__init__(self)
self.login_ui = Ui_login_main_window()
self.login_ui.setupUi(self)
self.login_ui.register_settings_btn.clicked.connect(self.show_registration_window)
def show_registration_window(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_register_window()
self.ui.setupUi(self.window)
self.window.show()
app = QApplication(sys.argv)
main_window = Login()
main_window.show()
sys.exit(app.exec_())
register.py
import sys
import os
from PyQt5.QtWidgets import QApplication, QMainWindow
from ui.register_window_ui import Ui_register_window
curr_path = os.path.dirname(os.path.abspath(__file__))
class Register(QMainWindow, Ui_register_window):
def __init__(self):
QMainWindow.__init__(self)
self.register_win_ui = Ui_register_window()
self.register_win_ui.setupUi(self)
self.register_win_ui.register_trial_btn.clicked.connect(self.print_data)
def print_data(self):
print("Clicked")
app = QApplication(sys.argv)
main_window = Register()
main_window.show()
sys.exit(app.exec_())
As you can see that in login.py I have created a button click event for register_settings_btn which when clicked will show Ui_register_window() which is register_window. So when I click it, it shows me the window but the window is not functional i.e. there is no button click event happening. For ex in register.py I have created a button click event which is not working.
Can anyone please explain me why is it not working. Please help. Thanks
I have a button on login window which when clicked should open the registration window
instead of creating 2 QApplications create only 1 and show or exec 1st the login, validate the input and if everything is ok, then show the Register
i.e. instead do something like:
app = QApplication(sys.argv)
login_window = Login()
result = login_window.exec()
if result == ???:
register_window = Register()
register_window.show()
else:
showMsg('Login failed!')
sys.exit(app.exec_())
update! create another file i.e. myProject.py and add this!
#!/usr/local/bin/python
# coding: utf-8
import os, sys
add your imports here!
if __name__ == "__main__":
app = QApplication(sys.argv)
#create login view nd register view
login_window = Login()
register_window = Register()
#execute the view login and read the doc, this will block the code until the user quit the view!
result = login_window.exec()
#define a condition to proof the validity of login process
if result == ???:
#show the register
register_window.show()
else:
#or show the error msg
showMsg('Login failed!')
sys.exit(app.exec_())

How to click on QMessageBox with pytest-qt?

I am creating some unit tests for a PyQt application with pytest-qt.
And I would like to create open the graphical window, do some tests then close the window, rather than open a new window for every test, ie. use a module fixture for the window itself.
I succeeded to do this part, by calling in a local function a QtBot rather than using the default fixture, and removing the mocker ones. So I am pretty close to my objective.
However, but I am not able to close the window (and test the QMessageBox for closing event).
I red examples like
how to handle modal dialog and its git discussion, or the qmessage question; which seem to be close to my question.
It is suggested to use a timer to wait for the QMessageBox to appear then click on a button choice, but visibly I am not able to apply them correctly.
In my attempt, pytest get the closing demand, but not the click on the dialog box. So, I have to click myself to finish the test.
Here is a small example, with file GUI.py:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5 import QtGui, QtCore, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QCoreApplication, Qt, QObject
from PyQt5.QtGui import QIcon
class Example(QMainWindow):
def __init__(self, parent = None):
super().__init__()
self.initUI(self)
def initUI(self, MainWindow):
# centralwidget
MainWindow.resize(346, 193)
self.centralwidget = QtWidgets.QWidget(MainWindow)
# The Action to quit
self.toolb_action_Exit = QAction(QIcon('exit.png'), 'Exit', self)
self.toolb_action_Exit.setShortcut('Ctrl+Q')
self.toolb_action_Exit.triggered.connect(self.close)
# The Button
self.btn_prt = QtWidgets.QPushButton(self.centralwidget)
self.btn_prt.setGeometry(QtCore.QRect(120, 20, 89, 25))
self.btn_prt.clicked.connect(lambda: self.doPrint() )
self.btn_quit = QtWidgets.QPushButton(self.centralwidget)
self.btn_quit.setGeometry(QtCore.QRect(220, 20, 89, 25))
self.btn_quit.clicked.connect(lambda: self.close() )
# The textEdit
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(10, 60, 321, 81))
# Show the frame
MainWindow.setCentralWidget(self.centralwidget)
self.show()
def doPrint(self):
print('TEST doPrint')
def closeEvent(self, event):
# Ask a question before to quit.
self.replyClosing = QMessageBox.question(self, 'Message',
"Are you sure to quit?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if self.replyClosing == QMessageBox.Yes:
event.accept()
else:
event.ignore()
def main_GUI():
app = QApplication(sys.argv)
imageViewer = Example()
return app, imageViewer
if __name__ == '__main__':
app, imageViewer =main_GUI()
rc= app.exec_()
print('App end is exit code {}'.format(rc))
sys.exit(rc)
and the pytest file named test_GUI.py:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os, sys
import pytest
from PyQt5 import QtGui, QtCore, QtWidgets, QtTest
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QCoreApplication, Qt, QObject
from pytestqt.plugin import QtBot
GUI = __import__('GUI')
#pytest.yield_fixture(scope="module")
def qtbot_session(qapp, request):
print(" SETUP qtbot")
result = QtBot(qapp)
with capture_exceptions() as exceptions:
yield result
print(" TEARDOWN qtbot")
#pytest.fixture(scope="module")
def Viewer(request):
print(" SETUP GUI")
app, imageViewer = GUI.main_GUI()
qtbotbis = QtBot(app)
# qtbotbis.addWidget(imageViewer)
# qtbotbis.wait_for_window_shown(imageViewer)
QtTest.QTest.qWait(0.5 *1000)
yield app, imageViewer, qtbotbis
# EXIT
# mocker.patch.object(QMessageBox, 'question', return_value=QMessageBox.Yes)
# imageViewer.toolb_action_Exit.trigger()
def handle_dialog():
# while not imageViewer.replyClosing.isVisible():
# app.processEvents()
box = QMessageBox()
box.setStandardButtons(QMessageBox.Yes)
button = box.button(QMessageBox.Yes)
qtbotbis.mouseClick(button, QtCore.Qt.LeftButton)
QtCore.QTimer.singleShot(100, handle_dialog)
qtbotbis.mouseClick(imageViewer.btn_quit, QtCore.Qt.LeftButton, delay=1)
assert imageViewer.close()
print(" TEARDOWN GUI")
class Test_GUI() :
def test_interface(self, Viewer):
print(" beginning ")
app, imageViewer, qtbot = Viewer
qtbot.mouseClick( imageViewer.btn_prt, QtCore.Qt.LeftButton )
QtTest.QTest.qWait(0.5 *1000)
assert True
print(" Test passed")
Any idea of what I am missing ? Any other idea or suggestion would also be appreciate.
In your attempt you are creating a new QMessageBox that is different from the one created with the static method QMessageBox::question() so even if you click it will not work.
The idea is to obtain the QMessageBox shown, and in this case we will take advantage of that since it is the active window so we can obtain it using QApplication::activeWindow(). Another way to get the QMessageBox is to use the relationship between imageViewer and the QMessageBox through findChild():
#pytest.fixture(scope="module")
def Viewer(request):
print(" SETUP GUI")
app, imageViewer = GUI.main_GUI()
qtbotbis = QtBot(app)
QtTest.QTest.qWait(0.5 * 1000)
yield app, imageViewer, qtbotbis
def handle_dialog():
messagebox = QtWidgets.QApplication.activeWindow()
# or
# messagebox = imageViewer.findChild(QtWidgets.QMessageBox)
yes_button = messagebox.button(QtWidgets.QMessageBox.Yes)
qtbotbis.mouseClick(yes_button, QtCore.Qt.LeftButton, delay=1)
QtCore.QTimer.singleShot(100, handle_dialog)
qtbotbis.mouseClick(imageViewer.btn_quit, QtCore.Qt.LeftButton, delay=1)
assert imageViewer.isHidden()

(PyQt) QVBoxLayout shrink upon multiple loading of addWidget

Why is the layout shrinking like this and other times going back to normal?
I've created several separate UI files in QtDesigner, one is the MainWindow and the other is a widget for Loading Data.
In order to work with these files, I've created separate child classes of each UI file. In order to add a new widget to the MainWindow I've created a addWidget() function; it works by adding a particular widget to the scrollarea layout. You can see this function in MainWindow.py
Here is the code for __main__.py
import multiprocessing as mp
import os.path
import sys
import time
from PyQt5 import QtGui
from PyQt5 import QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import *
from point_spectra_gui.future_.functions import *
from point_spectra_gui.future_.util import delete
from point_spectra_gui.future_.util.excepthook import my_exception_hook
def new():
p = mp.Process(target=main, args=())
p.start()
def connectWidgets(ui):
ui.actionLoad_Data.triggered.connect(lambda: ui.addWidget(LoadData.Ui_Form))
def main():
sys._excepthook = sys.excepthook
sys.excepthook = my_exception_hook
app = QtWidgets.QApplication(sys.argv)
mainWindow = QtWidgets.QMainWindow()
ui = MainWindow.Ui_MainWindow()
ui.setupUi(mainWindow)
connectWidgets(ui)
mainWindow.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Here is the code for MainWindow.py
from PyQt5 import QtWidgets
from point_spectra_gui.future_.functions import *
from point_spectra_gui.future_.util import *
from point_spectra_gui.ui import MainWindow
class Ui_MainWindow(MainWindow.Ui_MainWindow):
def setupUi(self, MainWindow):
self.MainWindow = MainWindow
super().setupUi(MainWindow) # Run the basic window UI
self.menu_item_shortcuts() # set up the shortcuts
def addWidget(self, object):
widget = object()
widget.setupUi(self.scrollArea)
self.widgetLayout = QtWidgets.QVBoxLayout()
self.widgetLayout.setObjectName("widgetLayout")
self.verticalLayout_3.addLayout(self.widgetLayout)
self.widgetLayout.addWidget(widget.get_widget())
def menu_item_shortcuts(self):
self.actionExit.setShortcut("ctrl+Q")
self.actionCreate_New_Workflow.setShortcut("ctrl+N")
self.actionOpen_Workflow.setShortcut("ctrl+O")
self.actionRestore_Workflow.setShortcut("ctrl+R")
self.actionSave_Current_Workflow.setShortcut("ctrl+S")
Here is the code of the child class LoadData.py
from PyQt5 import QtWidgets
from point_spectra_gui.ui.LoadData import Ui_loadData
class Ui_Form(Ui_loadData):
def setupUi(self, Form):
super().setupUi(Form)
self.connectWidgets()
def get_widget(self):
return self.groupBox
def connectWidgets(self):
self.newFilePushButton.clicked.connect(lambda: self.on_getDataButton_clicked())
# self.get_data_line_edit.textChanged.connect(lambda: self.get_data_params())
# self.dataname.textChanged.connect(lambda: self.get_data_params())
def on_getDataButton_clicked(self):
filename, _filter = QtWidgets.QFileDialog.getOpenFileName(None, "Open Data File", '.', "(*.csv)")
self.fileNameLineEdit.setText(filename)
if self.fileNameLineEdit.text() == "":
self.fileNameLineEdit.setText("*.csv")
**Edit
Upon trying this again and then shrinking the window. The layout goes back to normal.
This to me tells me it's not a problem with my code, it's the way the Qt handles the adding of widgets. I still do not understand why this is happening though. So any insight into how this is happening is very much appreciated.
This problem is with the Form.resize() inside the generated code.
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
To fix this you'll need to go into QtDesigner and set the geometry back to it's default the Layout size by clicking the red circled item.
This, in essence, deletes the resize method call
You can then convert again with pyuic

Categories