PyQt5 does not redrawing widget - python-3.x

Force repainting does not repaint PyQt5 widget (Qlabel, QTextEdit, even QProgressBar and etc)
Tested platforms: Linux, MacOS
PyQt5 version: 5.15.7
Installed from pip
As example I created simple app that updating text in QLabel widget in for loop. Force repainting doesnt working
import sys
from time import sleep
from PyQt5.QtWidgets import (QWidget, QApplication, QPushButton, QLabel)
class Example(QWidget):
def __init__(self):
super().__init__()
self.text = QLabel('Test', self)
self.text.move(10, 10)
self.text.resize(60,20)
self.button = QPushButton('Run', self)
self.button.move(17,40)
self.button.clicked.connect(self.some_activity)
self.setGeometry(300, 300, 100, 80)
self.show()
def some_activity(self):
for i in range(100):
text = f'i = {i}'
self.text.setText(text)
# self.text.update() -> Nothing happens (it shouldnt: https://doc.qt.io/qt-5/qwidget.html#update)
self.text.repaint() # -> Nothing happens
self.repaint() # -> Nothing happens
print(f'Text updated: {text}')
sleep(0.03)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
Video demonstration: link

Just needed to use QThread to use for loop in my program
Thanks #musicamante for helping.
import sys
from time import sleep
from PyQt5 import QtCore
from PyQt5.QtWidgets import (QWidget, QApplication, QPushButton, QLabel)
class Thread(QtCore.QThread):
signal = QtCore.pyqtSignal(str)
def __init__(self, parent=None): QtCore.QThread.__init__(self, parent)
def run(self):
for i in range(100):
text = f'i = {i}'
print(f'Text updated: {text}')
self.signal.emit(text)
sleep(.3)
class Example(QWidget):
def __init__(self):
super().__init__()
self.text = QLabel('Test', self)
self.text.move(10, 10)
self.text.resize(60,20)
self.thread = Thread()
self.thread.signal.connect(self.signal, QtCore.Qt.QueuedConnection)
self.button = QPushButton('Run', self)
self.button.move(17,40)
self.button.clicked.connect(self.thread.start)
self.setGeometry(300, 300, 100, 80)
self.show()
def signal(self, text): self.text.setText(text)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

Related

Issue in Pyqt with next and previous Pages

so I'm currently working on a little project but I have an issue and all what I've tried did not work. I have 2 files :
Page1test :
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QPushButton, QWidget, QLabel
import sys
from page2test import Page2
class Page1(QWidget):
def __init__(self):
super(Page1, self).__init__()
self.setWindowTitle("Page 1")
label1 = QLabel(self)
label1.setText("\n PAGE 1")
self.btn_inMyApp = QPushButton ('Next page', self)
self.btn_inMyApp.setGeometry(1500,800,275,125)
self.btn_inMyApp.clicked.connect(self.closePage1_OpenPage2)
self.show()
def closePage1_OpenPage2(self):
self.Open = Page2()
self.Open.showMaximized()
self.close()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Page1()
window.showMaximized()
sys.exit(app.exec_())
And page2test :
from PyQt5.QtWidgets import QPushButton, QWidget, QLabel
from Page1test import Page1
class Page2(QWidget):
def __init__(self):
super(Page2, self).__init__()
self.setWindowTitle("Test window principale")
label1 = QLabel(self)
label1.setText("\n Page2")
self.btn_inMyApp = QPushButton ('previous Page', self)
self.btn_inMyApp.setGeometry(1500,800,275,125)
self.btn_inMyApp.clicked.connect(self.closePage2_OpenPage1)
self.show()
def closePage2_OpenPage1(self):
self.Open = Page1()
self.Open.showMaximized()
self.close()
I run the code of Page1test : empty window with just a Qpushbutton "Next Page", goal : we click on it and it open Page 2 (and close Page 1). And, when we are in Page 2, we have A Qpushbutton with "Previous Page" and when we click on it, it opens page 1, and close Page 2. Like a loop.
But, when I run the code, it returns an error :
cannot import name 'Page2' from partially initialized module 'page2test' (most likely due to a circular import)
and I have no idea how to fix it...
If someone had an idea, it would be really helpful.
So, I've finally found the solution, that was not so difficult in the end. Here it is (if it can help someone) :
Instead of making 2 files, I've done just 1 file. Here is the code :
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QPushButton, QWidget, QLabel, QMainWindow
import sys
class MyApp(QWidget):
def __init__(self):
super(MyApp, self).__init__()
self.setWindowTitle("Page 1")
label1 = QLabel(self)
label1.setText("\n PAGE 1")
self.btn_inMyApp = QPushButton ('Page suivante', self)
self.btn_inMyApp.setGeometry(1500,800,275,125)
self.btn_inMyApp.clicked.connect(self.closePage1_OpenPage2)
self.show()
def btn1_onClicked(self):
pass
def closePage1_OpenPage2(self):
self.Open = NewApp()
self.Open.showMaximized()
self.close()
class NewApp(QMainWindow):
def __init__(self):
super(NewApp, self).__init__()
self.setWindowTitle("Test window principale")
label1 = QLabel(self)
label1.setText("\n Page2")
self.btn_inMyApp = QPushButton ('previous Page', self)
self.btn_inMyApp.setGeometry(1500,800,275,125)
self.btn_inMyApp.clicked.connect(self.closePage2_OpenPage1)
self.show()
def closePage2_OpenPage1(self):
self.Open = MyApp()
self.Open.showMaximized()
self.close()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MyApp()
window.showMaximized()
sys.exit(app.exec_())
```

Spin box not showing up

I'm unsure why all aspects of my GUI are showing up apart from the spin box (the code for it is within the home function).
I've tried moving it to the init(self) function, but that doesn't work. I thought it would be intuitive for it to be within the home function as that is where all my other GUI (e.g. buttons) resides.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QAction, QMessageBox, QDoubleSpinBox
from temperature import MplWindow
from filament import MplWindow1
from highvoltage import MplWindow2
class window(QMainWindow):
def __init__(self):
super(window, self).__init__()
self.setGeometry(50, 50, 300, 300)
self.setWindowTitle('Temperature Control')
self.setWindowIcon(QIcon('adn.png'))
extractAction = QAction('&Quit', self)
extractAction.setShortcut('Ctrl+Q')
extractAction.setStatusTip('leave the app')
extractAction.triggered.connect(self.close_application)
self.statusBar()
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&File')
fileMenu.addAction(extractAction)
self.matplWindow = MplWindow()
self.matplWindow1 = MplWindow1()
self.matplWindow2 = MplWindow2()
self.home()
def home(self):
btn = QPushButton('quit', self)
btn.clicked.connect(self.close_application)
btn.resize(btn.sizeHint())
btn.move(200, 260)
button = QPushButton('Temperature',self)
button.clicked.connect(self.opengraph)
button.move(100,50)
button = QPushButton('Filament voltage',self)
button.clicked.connect(self.openfilament)
button.move(100,80)
button = QPushButton('High voltage',self)
button.clicked.connect(self.openhigh)
button.move(100,110)
self.doubleSpinBox = QtWidgets.QDoubleSpinBox()
self.doubleSpinBox.setGeometry(180, 110, 62, 22)
self.show()
def opengraph(self):
self.matplWindow.funAnimation()
def openfilament(self):
self.matplWindow1.funAnimation1()
def openhigh(self):
self.matplWindow2.funAnimation2()
def close_application(self):
choice = QMessageBox.question(self, 'Message',
"Are you sure to quit?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if choice == QMessageBox.Yes:
sys.exit()
else:
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
Gui = window()
sys.exit(app.exec_())
I worked it out - I moved the code to the init function.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QAction, QMessageBox, QDoubleSpinBox, QLabel, QVBoxLayout
from temperature import MplWindow # +++
from filament import MplWindow1
from highvoltage import MplWindow2
class window(QMainWindow):
def __init__(self):
super(window, self).__init__()
self.setGeometry(50, 50, 300, 300)
self.setWindowTitle('Temperature Control')
self.setWindowIcon(QIcon('adn.png'))
extractAction = QAction('&Quit', self)
extractAction.setShortcut('Ctrl+Q')
extractAction.setStatusTip('leave the app')
extractAction.triggered.connect(self.close_application)
self.statusBar()
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&File')
fileMenu.addAction(extractAction)
self.matplWindow = MplWindow() # +++
self.matplWindow1 = MplWindow1()
self.matplWindow2 = MplWindow2()
# vBoxLayout = QVBoxLayout()
self.label = QLabel("Set point Temp:", self)
self.label.move(50,150)
self.spinBox = QDoubleSpinBox(self)
self.spinBox.move(70,150)
self.home()
def home(self):
btn = QPushButton('quit', self)
btn.clicked.connect(self.close_application)
btn.resize(btn.sizeHint())
btn.move(200, 260)
button = QPushButton('Temperature',self)
button.clicked.connect(self.opengraph)
button.move(100,50)
button = QPushButton('Filament voltage',self)
button.clicked.connect(self.openfilament)
button.move(100,80)
button = QPushButton('High voltage',self)
button.clicked.connect(self.openhigh)
button.move(100,110)
self.show()
def opengraph(self):
self.matplWindow.funAnimation() # +++
def openfilament(self):
self.matplWindow1.funAnimation1()
def openhigh(self):
self.matplWindow2.funAnimation2()
def close_application(self):
choice = QMessageBox.question(self, 'Message',
"Are you sure to quit?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if choice == QMessageBox.Yes:
sys.exit()
else:
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
Gui = window()
sys.exit(app.exec_())

Back to Previous Window

i'm new in Python and PyQt4.
I want to ask "How to back to previous Window ?"
i have 2 file in here, file 'login' and 'signup'
here file login.py
import sys
from PyQt4.QtGui import QWidget, QPushButton, QLineEdit, QLabel, \
QApplication, QGridLayout
from signup import SignUp
class Login(QWidget):
def __init__(self):
super(Login, self).__init__()
self.setWindowTitle("Login")
self.login_window()
def login_window(self):
self.login_layout = QGridLayout()
self.login_button = QPushButton("Login")
self.signup_button = QPushButton("Sign Up")
self.login_layout.addWidget(self.login_button, 2, 0)
self.login_layout.addWidget(self.signup_button, 2, 1)
self.signup_button.clicked.connect(self.signup_show)
self.setLayout(self.login_layout)
self.show()
def signup_show(self):
self.signupshow = SignUp()
self.hide()
self.signupshow.show()
def check_signup(self):
SignUp.check_signup()
self.show()
def main():
app = QApplication(sys.argv)
login = Login()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
and here is signup.py
import sys
from PyQt4.QtGui import QWidget, QPushButton, QLineEdit, QLabel, \
QApplication, QGridLayout
class SignUp(QWidget):
def __init__(self):
super(SignUp, self).__init__()
self.setWindowTitle("Sign Up")
self.signup_window()
def signup_window(self):
self.signup_layout = QGridLayout()
self.signup_button = QPushButton("Sign Up")
self.signup_layout.addWidget(self.signup_button, 2, 0, 1, 0)
self.signup_button.clicked.connect(self.check_signup)
self.setLayout(self.signup_layout)
self.show()
def check_signup(self):
self.close()
def main():
app = QApplication(sys.argv)
signup = SignUp()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
my problem is, when i push button signup from signup.py, it's close but window Login is not show.
i think i don't have any trigger in signup.py for check_signup in login.py
in this code, i delete some Line Edit and Label. I think it's not a problem.
i hope someone will help, Thank you before :)
and if you answer this questtion, i hope you will explain a little bit of the logic, thank you again :)
I'm not really sure what you are trying to achieve (your description is kind of confusing), but let's start here. I put all your code in one file, since you need to import each other (which leads to cyclic import). Then, I just added these two lines
self.login = Login()
self.login.show()
into check_signup method of SignUp class, which pops-up Login window. If this is not your desired result, please let us know and provide us a better description. The code follows:
import sys
from PyQt4.QtGui import QWidget, QPushButton, QLineEdit, QLabel, \
QApplication, QGridLayout
class Login(QWidget):
def __init__(self):
super(Login, self).__init__()
self.setWindowTitle("Login")
self.login_window()
def login_window(self):
self.login_layout = QGridLayout()
self.login_button = QPushButton("Login")
self.signup_button = QPushButton("Sign Up")
self.login_layout.addWidget(self.login_button, 2, 0)
self.login_layout.addWidget(self.signup_button, 2, 1)
self.signup_button.clicked.connect(self.signup_show)
self.setLayout(self.login_layout)
self.show()
def signup_show(self):
self.signupshow = SignUp()
self.hide()
self.signupshow.show()
def check_signup(self):
SignUp.check_signup()
self.show()
class SignUp(QWidget):
def __init__(self):
super(SignUp, self).__init__()
self.setWindowTitle("Sign Up")
self.signup_window()
def signup_window(self):
self.signup_layout = QGridLayout()
self.signup_button = QPushButton("Sign Up")
self.signup_layout.addWidget(self.signup_button, 2, 0, 1, 0)
self.signup_button.clicked.connect(self.check_signup)
self.setLayout(self.signup_layout)
self.show()
def check_signup(self):
self.login = Login()
self.login.show()
self.close()
def main():
app = QApplication(sys.argv)
signup = SignUp()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You should rather use signal-slot connections:
import sys
from PyQt4 import QtGui, QtCore
class WidgetA(QtGui.QWidget):
open_b = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(WidgetA, self).__init__(parent)
self.resize(100, 100)
self.l = QtGui.QVBoxLayout()
self.close_btn = QtGui.QPushButton('Close')
self.b_btn = QtGui.QPushButton('Open B')
self.b_btn.clicked.connect(self.b_btn_clicked)
self.l.addWidget(self.close_btn)
self.l.addWidget(self.b_btn)
self.setLayout(self.l)
def b_btn_clicked(self):
self.open_b.emit()
self.hide()
class WidgetB(QtGui.QWidget):
open_a = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(WidgetB, self).__init__(parent)
self.resize(100, 100)
self.l = QtGui.QVBoxLayout()
self.close_btn = QtGui.QPushButton('Close')
self.close_btn.clicked.connect(self.hide)
self.a_btn = QtGui.QPushButton('Open A')
self.a_btn.clicked.connect(self.a_btn_clicked)
self.l.addWidget(self.close_btn)
self.l.addWidget(self.a_btn)
self.setLayout(self.l)
def a_btn_clicked(self):
self.open_a.emit()
self.hide()
def main():
app = QtGui.QApplication(sys.argv)
a = WidgetA()
b = WidgetB()
a.open_b.connect(lambda: b.show())
b.open_a.connect(lambda: a.show())
a.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

PyQt5 shortcut in menubar is not working

I'm trying to create a simple UI that has the functionality to close when I use the shortcut Ctrl+Q or when I use the Exit into the File menu, to make it clearer I've created a fairly simple UI that goes with the code:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, qApp
class MGen(QMainWindow):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
exit_action = QAction('Exit', self)
exit_action.setShortcut('Ctrl+Q')
exit_action.triggered.connect(qApp.quit)
menu_bar = self.menuBar()
file_menu = menu_bar.addMenu('&File')
file_menu.addAction(exit_action)
self.setGeometry(0, 0, 500, 500)
self.setWindowTitle('MGen')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
mgen = MGen()
sys.exit(app.exec_())
Could anyone point what I'm doing wrong?

pyqt4 event programming (signal and slots )

I am trying to change this event code to standard signal-slot format.
But it does not work.
Can I get help?
this is slide number changing to lcd display number.
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.lcd = QtGui.QLCDNumber(self)
self.sld = QtGui.QSlider(QtCore.Qt.Horizontal, self)
vbox = QtGui.QVBoxLayout()
vbox.addWidget(self.lcd)
vbox.addWidget(self.sld)
self.setLayout(vbox)
#sld.valueChanged.connect(lcd.display)
QtCore.QObject.connect(self.sld, QtCore.SIGNAL(str1), self, QtCore.SLOT("SHOW()"))
#QtCore.pyqtSlot()
def SHOW(str1):
self.lcd.display()
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Signal & slot')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You were very close with your first attempt. All you need is:
self.sld.valueChanged.connect(self.lcd.display)
(The other lines up to self.setGeometry can be deleted).

Resources