Pyqt5 loads dynamically without code prompting - python

I use PyQt5 to design a GUI and I load it dynamically using PyQt5.uic.loadui()method.But in this way code prompt do not operate(have installed qt5-stubs). Is it normal?I mean I have to remember those objects' name, kind of laborious.
following is part of my code.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QLabel, QLineEdit, QPushButton, \
QGridLayout, QVBoxLayout, QHBoxLayout, QMessageBox,QMainWindow
from PyQt5 import uic
class LoginWindow(QMainWindow):
def __init__(self):
super().__init__()
self.registerwindow = RegisterWindow()
self.ui = uic.loadUi("login.ui")
def lineedit_init(self):
self.user_line.textchanged.connect(self.check_input_func)
self.password_line.textchanged.connect(self.check_input_func)
def pushbutton_init(self):
self.loginButton.setEnabled(False)
self.registerButton.setEnabled(True)
self.loginButton.clicked.connect(self.check_login_func)
self.registerButton.clicked.connect(self.open_register_window)
def check_login_func(self):
def check_input_func(self):
if self.user_line.tetx() and self.password_line.text():
self.loginButton.setEnabled(True)
else:
self.loginButton.setEnabled(False)
def open_register_window(self):
self.registerwindow = RegisterWindow()
self.registerwindow.ui.exec_()
class RegisterWindow(QDialog):
def __init__(self):
super().__init__()
self.ui = uic.loadUi("reg.ui")
self.ui.pushButton.clicked.connect(self.click_reg)
def click_reg(self):
QMessageBox.information(self, '12', '34', QMessageBox.Yes)
self.ui.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
login = LoginWindow()
login.ui.show()
sys.exit(app.exec_())
any solution will be helpful

Related

How to use QTest.keySequence?

I'm trying to test if a shortcut is working using PyQt5 and QTest. Here is my code:
Main.py
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QApplication, QPushButton
class Window(QWidget):
def __init__(self):
super().__init__()
self.more_btn = QPushButton("More")
self.more_btn.clicked.connect(self.on_clicked)
self.more_btn.setShortcut(QKeySequence.Undo)
vbox = QVBoxLayout()
vbox.addWidget(self.more_btn)
self.setLayout(vbox)
def on_clicked(self):
print("Item clicked")
test_main.py
import sys
import unittest
from PyQt5.QtGui import QKeySequence
from PyQt5.QtTest import QTest
from PyQt5.QtWidgets import QApplication
from Main import Window
class MainTestCase(unittest.TestCase):
def test_main(self):
app = QApplication(sys.argv)
form = Window()
QTest.keySequence(form, QKeySequence.Undo)
When I run test_main, item_clicked should be printed, but it doesn't. I tried to debug the program and set the breatpoint at the print("Item clicked") line, it didn't stop. It seems that QTest.keySequence didn't work. Why? How should I make it work?

PyQt connect PushButtton to next() slot in a Wizard

just created a wizard in PyQt. Now I want to be redirected to the next page by clicking a button. I have read that you need QCommandLinkButton for this. But I can't connect the signal from the button with the slot of the next() function.
from PyQt5.QtWidgets import (QApplication, QCheckBox, QGridLayout, QGroupBox,
QLabel, QLineEdit, QMessageBox, QRadioButton, QVBoxLayout, QWizard,
QWizardPage, QPushButton, QCommandLinkButton)
from PyQt5.QtCore import pyqtSlot
from ClassWizard import *
class Hauptseite(QWizardPage):
def __init__(self, parent=None):
super(Hauptseite, self).__init__(parent)
self.setTitle("Hauptseite")
self.pybutton2 = QPushButton('Transport', self)
self.pybutton2.resize(100,32)
self.pybutton2.move(240, 20)
#self.pybutton2.clicked.connect(self.next)
# self.pybutton2.clicked.connect(ClassWizard.next())
self.cl_button = QCommandLinkButton("Next", self)
self.cl_button.clicked.connect(QWizard.next)
def nextId(self):
return ClassWizard.Saegen

How to control the checkbox using the Python itself, and not by a mouse click?

I created a checkbox using PyQT, which normally works using mouse clicks.
I want to know if there is a way with which I can uncheck and check the checkbox using the program itself, and not a mouse click.
Basically I want to check and uncheck the box 10 times in 20 seconds and display it happening.
Here is my code for just the checkbox:
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QCheckBox, QWidget
from PyQt5.QtCore import QSize
class ExampleWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(140, 40))
self.setWindowTitle("Checkbox")
self.b = QCheckBox("Yes",self)
self.b.move(20,20)
self.b.resize(320,40)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = ExampleWindow()
mainWin.show()
sys.exit( app.exec_() )
checked : bool
This property holds whether the button is checked
Only checkable buttons can be checked. By default, the button is unchecked.
The QTimer class provides repetitive and single-shot timers.
More ... https://doc.qt.io/qt-5/qtimer.html
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QCheckBox, QWidget
from PyQt5.QtCore import QSize
class ExampleWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(140, 40))
self.setWindowTitle("Checkbox")
self.b = QCheckBox("Yes",self)
self.b.move(20,20)
self.b.resize(320,40)
self.num = 0
self.timer = QtCore.QTimer()
self.timer.setInterval(2000) # msec
self.timer.timeout.connect(self.update_now)
self.timer.start()
def update_now(self):
self.b.setChecked(not self.b.isChecked()) # +++
self.num += 1
if self.num == 10: self.timer.stop()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = ExampleWindow()
mainWin.show()
sys.exit( app.exec_() )

PyQt5 opening new window w/ if else

I'm designing window with PyQt5 and QtDesigner. I made maindemo.py, maindemo.ui, mainfail.py, mainfail.ui.
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QGraphicsView, QLabel, QMenuBar, QMenu, QStatusBar, QAction, qApp, QMessageBox
from PyQt5 import uic, QtCore
form_class = uic.loadUiType("maindemo.ui")[0]
class OpeningWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.initUI()
def initUI(self):
self.setWindowTitle('Qomics')
self.btn_survival.setToolTip('Survival Analysis')
self.btn_drug.setToolTip('Drug Analysis')
self.btn_CRISPR.setToolTip('CRISPR Analysis')
self.btn_cellline.setToolTip('Cell Line')
self.btn_survival.clicked.connect(self.open_SurvivalMainWindow)
self.btn_drug.clicked.connect(self.open_DrugWindow)
self.btn_CRISPR.clicked.connect(self.open_sgRNAWindow)
self.btn_cellline.clicked.connect(self.open_CellLineWindow)
actionExit = QAction('&Exit', self)
actionExit.setShortcut('Ctrl+Q')
actionExit.setStatusTip('Exit Application')
actionExit.triggered.connect(qApp.quit)
self.statusBar().showMessage('abcd')
self.setGeometry(200, 100, 800, 530)
self.show()
def openSurvivalMainWindow(self):
open_SurvivalMainWindow = SurvivalMainWindow()
open_SurvivalMainWindow.show()
def openDrugWindow(self):
open_DrugWindow = DrugWindow()
open_DrugWindow.show()
def opensgRNAWindow(self):
open_sgRNAWindow = sgRNAWindow()
open_sgRNAWindow.show()
def openCellLineWindow(self):
open_CellLineWindow = scatterWindow()
open_CellLineWindow.show()
above code is maindemo.py
what I want to do is clicking btn_drug, btn_sgRNA, btn_cellline connects to new window(with mainfail.py, mainfail.ui)
Only btn_survival connects to the real function and other buttons connects to mainfail window.
I tried to use if, else... but I couldn't write proper code..
I wrote a code but it doesn't work.
if openSurvivalMainWindow():
else:
openMainFailWindow.show()
Not clear what you want, but here an example of a multi window
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QGraphicsView, QLabel, QMenuBar, QMenu, QStatusBar, QAction, qApp, QMessageBox
from PyQt5 import uic, QtCore
from PyQt5.uic import loadUiType
login, _ = loadUiType('login.ui')
registration,_ = loadUiType('registration.ui')
class Register(QMainWindow, registration):
def __init__(self):
QWidget.__init__(self)
self.setupUi(self)
self.pushButton_24.clicked.connect(self.adding_users)
self.pushButton_25.clicked.connect(self.return_login)
def return_login(self):
self.window2 = Login()
self.close()
self.window2.show()
class Login(QWidget, login):
def __init__(self):
QWidget.__init__(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.Handel_Login)
self.pushButton_2.clicked.connect(self.registrations)
def registrations(self):
self.window2 = Register()
self.close()
self.window2.show()
######
#....
######
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle('Fusion')
window = Login()
#window = MainApp()
window.show()
sys.exit(app.exec_())
With this your application will go first to the login page and if you push the button 2 you will go to the registration page and of course the log in page will be closed

"Python has stopped working" on PyQt5 GUI exit

I've been pulling my hairs on this since a few hours. I have the following simple example:
#!/usr/bin/env python
import math
from PyQt5.QtCore import (pyqtSignal, QLineF, QPointF, QRect, QRectF, QSize,
QSizeF, Qt)
from PyQt5.QtGui import (QBrush, QColor, QFont, QIcon, QIntValidator, QPainter,
QPainterPath, QPen, QPixmap, QPolygonF)
from PyQt5.QtWidgets import (QAction, QApplication, QButtonGroup, QComboBox,
QFontComboBox, QGraphicsItem, QGraphicsLineItem, QGraphicsPolygonItem,
QGraphicsScene, QGraphicsTextItem, QGraphicsView, QGridLayout,
QHBoxLayout, QLabel, QMainWindow, QMenu, QMessageBox, QSizePolicy,
QToolBox, QToolButton, QWidget)
class DiagramScene(QGraphicsScene):
def __init__(self, parent=None):
super(DiagramScene, self).__init__(parent)
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.scene = DiagramScene()
self.scene.setSceneRect(QRectF(0, 0, 5000, 5000))
layout = QHBoxLayout()
self.view = QGraphicsView(self.scene)
layout.addWidget(self.view)
self.widget = QWidget()
self.widget.setLayout(layout)
self.setCentralWidget(self.widget)
self.setWindowTitle("Diagramscene")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.setGeometry(100, 100, 800, 500)
mainWindow.show()
sys.exit(app.exec_())
This program works OK. Now if you replace
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.setGeometry(100, 100, 800, 500)
mainWindow.show()
sys.exit(app.exec_())
by this (which is what you do when you want to start your program with console_scripts):
def mainFunc():
import sys
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.setGeometry(100, 100, 800, 500)
mainWindow.show()
sys.exit(app.exec_())
if __name__ == '__main__':
mainFunc()
I get a segfault on exit. Althout both programs are (apparently ?) IDENTICALS.
It should be noted that if you remove the QGraphicsView from the application, the bug goes away. No more segfault on exit.
Is the issue coming from my code ? Or is it a PyQt5 bug ?
As pointed-out by #user3419537, the catch here is to always provide a parent on widget construction.
Otherwise, deallocation goes south and you end up with a nice segfault on program termination.
The following modified code works correctly:
#!/usr/bin/env python
import math
from PyQt5.QtCore import (QLineF, QPointF, QRect, QRectF, QSize,
QSizeF, Qt)
from PyQt5.QtGui import (QBrush, QColor, QFont, QIcon, QIntValidator, QPainter,
QPainterPath, QPen, QPixmap, QPolygonF)
from PyQt5.QtWidgets import (QAction, QApplication, QButtonGroup, QComboBox,
QFontComboBox, QGraphicsItem, QGraphicsLineItem, QGraphicsPolygonItem,
QGraphicsScene, QGraphicsTextItem, QGraphicsView, QGridLayout,
QHBoxLayout, QLabel, QMainWindow, QMenu, QMessageBox, QSizePolicy,
QToolBox, QToolButton, QWidget)
class DiagramScene(QGraphicsScene):
def __init__(self, parent=None):
super(DiagramScene, self).__init__(parent)
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
# Build Widgets, from top to bottom
# Always assigning a parent to it
## widget is attached to MainWindow
self.widget = QWidget(self)
## view is attached to widget (main area of the MainWindow)
self.view = QGraphicsView(self.widget)
## scene is attached to the view
self.scene = DiagramScene(self.view)
# Configure the widgets
self.view.setScene(self.scene)
# Configure the layout
layout = QHBoxLayout()
layout.addWidget(self.view)
self.widget.setLayout(layout)
self.setCentralWidget(self.widget)
self.setWindowTitle("Diagramscene")
def mainFunc():
import sys
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.setGeometry(100, 100, 800, 500)
mainWindow.show()
sys.exit(app.exec_())
if __name__ == '__main__':
mainFunc()

Categories

Resources