The following code is just for testing the speed of the pyqtgraph. What I was expected is to get alternating graph forever. However, nothing is shown in the widget after executing this code. What is the problem?
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from random import randint, uniform
from math import *
import pyqtgraph as pg
import time
class Example(QWidget):
def __init__(self):
super().__init__()
self.x=pg.PlotWidget(self)
self.x.setMinimumHeight(400)
self.x.setMinimumWidth(400)
self.setWindowState(Qt.WindowMaximized)
self.u=[i+uniform(1,30) for i in range(1000)]
self.v=[-i+uniform(1,30) for i in range(1000)]
self.show()
def Run(self):
while 1:
self.x.clear()
self.x.plot(self.u)
self.x.clear()
self.x.plot(self.v)
app=QApplication(sys.argv)
ex=Example()
ex.Run()
sys.exit(app.exec_())
It generally a bad idea to use a while loop in a GUI. The problem is that it's preventing the GUI to remain responsive and handle all GUI events.
An option is to use a timer instead, e.g. a simple QTimer. In order to switch between the two different datasets to plot you would also introduce some mechanism as to which one should be shown.
import sys
#from PyQt5.QtWidgets import *
#from PyQt5.QtCore import *
from PyQt4 import QtGui, QtCore
from random import randint, uniform
import pyqtgraph as pg
class Example(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.x=pg.PlotWidget(self)
self.x.setMinimumHeight(400)
self.x.setMinimumWidth(400)
self.setWindowState(QtCore.Qt.WindowMaximized)
self.u=[i+uniform(1,30) for i in range(1000)]
self.v=[-i+uniform(1,30) for i in range(1000)]
self.switch = True
self.show()
def start(self):
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.run)
self.timer.start(500)
def run(self):
if self.switch:
self.x.clear()
self.x.plot(self.u)
else:
self.x.clear()
self.x.plot(self.v)
self.switch = not self.switch
app=QtGui.QApplication(sys.argv)
ex=Example()
ex.start()
sys.exit(app.exec_())
Related
I use Qtdesigner and then convert it to py file..I want to take input text through the QLineEdit and put it to pyautogui.typewrite. facing problem to solve it.
I write the the code bleow
import pyautogui
import time
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QMovie
from PyQt5.QtCore import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUiType
from messageRHR import Ui_MainWindow
class MainThread(QThread):
def __init__(self,message):
super(MainThread,self).__init__()
# self.message = QLineEdit(self)
def run(self):
self.taskExecution()
def taskExecution(self):
pyautogui.typewrite(message)
time.sleep(1)
pyautogui.press('Enter')
startExecution = MainThread()
class Main(QMainWindow):
def __init__(self):
super(Main, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.startTask)
self.ui.pushButton_2.clicked.connect(self.close)
def startTask(self):
self.ui.movie = QtGui.QMovie("00545cb7179c504433d4c8f5e845f286.gif")
self.ui.label_2.setMovie(self.ui.movie)
self.ui.movie.start()
self.ui.movie = QtGui.QMovie("00545cb7179c504433d4c8f5e845f286.gif")
self.ui.label_3.setMovie(self.ui.movie)
self.ui.movie.start()
message = QLineEdit(self)
startExecution.start()
app = QApplication(sys.argv)
rsn = Main()
rsn.show()
exit(app.exec_())
I am trying to send signal and receive it between two windows, i followed exactly the documentation.
While running the program : No error Nor receiving the signal in the slot
While Debugging in #pycharm: i receive below error :
TypeError: connect() failed between started() and _signal()
Two files Codes are below:
First file name: Signals.py
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
import slots
class aaa(qtw.QWidget,qtc.QObject):
submitted = qtc.pyqtSignal(str)
def __init__(self):
super().__init__()
self.setLayout(qtw.QVBoxLayout())
self.edit = qtw.QLineEdit()
self.submit=qtw.QPushButton('Submit',clicked=self.onSubmit)
self.layout().addWidget(self.edit)
self.layout().addWidget(self.submit)
self.show()
def onSubmit(self):
self.submitted.emit(self.edit.text())
self.close()
self.myDialog2 = slots.bbb()
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
mw = aaa()
sys.exit(app.exec())
Second file name: slots.py
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
import Signals
class bbb(qtw.QWidget,qtc.QThread):
#qtc.pyqtSlot()
def __init__(self):
super().__init__()
self.setLayout(qtw.QVBoxLayout())
self.label = qtw.QLabel(self)
self.x=Signals.aaa()
self.x.submitted.connect(self.label.setText)
print(self.label.text())
self.layout().addWidget(self.label)
self.show()
I do not know what documentation refers to the OP so I could not indicate if the tutorial you are following is correct or not. If you are referring to the official docs then you clearly have not followed it correctly.
Your code has many errors so I will only mention the most important ones:
The connection must be used before emitting the signal,
You should not inherit from QObject and QWidget since they will have conflicts, in addition to that there is no justification.
You shouldn't use pyqtSlot unnecessarily, setting it in the contstructor doesn't make sense.
You are creating a circular import.
"self.x" is not "mw" but another object.
A possible solution is the following:
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
class bbb(qtw.QWidget):
def __init__(self):
super().__init__()
lay = qtw.QVBoxLayout(self)
self.label = qtw.QLabel()
lay.addWidget(self.label)
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
import slots
class aaa(qtw.QWidget, qtc.QObject):
submitted = qtc.pyqtSignal(str)
def __init__(self):
super().__init__()
self.edit = qtw.QLineEdit()
self.submit = qtw.QPushButton("Submit", clicked=self.onSubmit)
lay = qtw.QVBoxLayout(self)
lay.addWidget(self.edit)
lay.addWidget(self.submit)
self.myDialog2 = slots.bbb()
self.submitted.connect(self.myDialog2.label.setText)
def onSubmit(self):
self.submitted.emit(self.edit.text())
self.close()
self.myDialog2.show()
if __name__ == "__main__":
app = qtw.QApplication(sys.argv)
mw = aaa()
mw.show()
sys.exit(app.exec())
I am new in multi threading. The video file I use work fine and the progress-bar at the start shows a percent in progress-bar. When I want to change the progress-bar value continuously for example the current cpu usage value the below code keeps crashing when i run the code. Why is the code not working??
I think the problem is emit and connect. If so what can i do? And how do I correct this? Thanks in advance for the help.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QObject, pyqtSignal
import sysinfo
from multi import Ui_Form
class main(QtWidgets.QWidget ,Ui_Form):
cpu_value = pyqtSignal()
def __init__(self, parent = None):
super(main,self).__init__(parent)
self.setupUi(self)
self.threadclass = ThreadClass()
self.threadclass.start()
self.cpu_value.connect(self.updateProgressBar)
def updateProgressBar(self):
val = sysinfo.getCPU()
self.progressBar.setValue(val)
class ThreadClass(QtCore.QThread):
def __init__(self, parent = None):
super(ThreadClass,self).__init__(parent)
def run(self):
while 1:
val = sysinfo.getCPU()
self.cpu_value.emit(val)
if __name__ == '__main__':
a = QtWidgets.QApplication(sys.argv)
app = main()
app.show()
a.exec_()
env: python3.5 + pyqt5.9
I use a timer to update the CPU info like that:
#!/usr/bin/python3
# 2017.12.10 13:20:11 CST
# 显示 CPU
import sys
from PyQt5 import QtCore, QtWidgets
import psutil
class CPU(QtWidgets.QProgressBar):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("CPU Info")
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(lambda : self.setValue(psutil.cpu_percent()))
self.timer.start(1000)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
win = CPU()
win.show()
app.exec_()
I copied a PyQt example from the Web into a file and opened it up in PyCharm. Below is the code:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from math import *
class Calculator(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.browser = QTextBrowser()
self.expression = QLineEdit()
self.expression.setPlaceholderText("Type an expression and press Enter.")
self.expression.selectAll()
layout = QVBoxLayout()
layout.addWidget(self.browser)
layout.addWidget(self.expression)
self.someWidget = QWidget()
self.someWidget.setLayout(layout)
self.setCentralWidget(self.someWidget)
self.expression.setFocus()
self.expression.returnPressed.connect(self.updateUi)
self.setWindowTitle("Calculator")
def updateUi(self):
try:
text = self.expression.text()
self.browser.append("%s = %s", (text, eval(text)))
except:
self.browser.append("<font color=red>Invalid Expression</font>")
def main():
import sys
app = QApplication(sys.argv)
window = Calculator()
window.show()
sys.exit(app.exec_())
main()
The problem is that the code run well even without adding the following import statements:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from math import *
I have seen this example being used in many videos and books. If the code works good without the above statements, then why did the author of the example write the statements.
From PyQt4 to PyQt5, a lot of things moved from QtGui, QtCore to QtWidgets. To write a simple application in PyQt5, you're likely to need only QtWidgets.
My guess is that the code was originally written for PyQt4, and "adapted" to PyQt5, without removing useless imports.
The proper way to import would be import PyQt5.QtWidgets as QtWidgets ( see Should wildcard import be avoided ?).
The code then becomes:
class Calculator(QtWidgets.MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.browser = QtWidgets.QTextBrowser()
self.expression = QtWidgets.QLineEdit()
...
I am trying to connect a push button signal to a callable I created, but for some reason this error keeps on popping up. I've checked to make sure QtCore is imported ... what else am I missing?
Sample code:
from PyQt4 import QtCore
from PyQt4 import QtGui
import sys
class guiFindFiles(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
#Create window
self.setFixedSize(400,180)
self.setWindowTitle("Choose the files to use")
#Create all layouts to be used by window
self.windowLayout = QtGui.QVBoxLayout()
self.fileLayout1 = QtGui.QHBoxLayout()
self.fileLayout2 = QtGui.QHBoxLayout()
self.fileLayout3 = QtGui.QHBoxLayout()
#Create the prompt for user to load in the q script to use
self.qFileTF = QtGui.QLineEdit("Choose the q script file to use")
self.qFileButton = QtGui.QPushButton("Open")
self.qFileButton.setFixedSize(100,27)
self.fileLayout1.addWidget(self.qFileTF)
self.fileLayout1.addWidget(self.qFileButton)
#Connect all the signals and slots
self.connect(self.qFileButton, SIGNAL("pressed()"), self.loadFile)
def loadFile():
fileName = []
selFile = QtGui.QFileDailog.getOpenFileName(self)
print selFile
SIGNAL is inside QtCore, so the line should be:
self.connect(self.qFileButton, QtCore.SIGNAL("pressed()"), self.loadFile)
but you really should use the new style connections:
self.qFileButton.pressed.connect(self.loadFile)
And, unless you meant to differentiate a click from press/release couple, you'd better use clicked signal:
self.qFileButton.clicked.connect(self.loadFile)
SIGNAL is defined inside QtCore, so you must use it within QtCore namespace if you've imported QtCore as a whole. So use:
QtCore.SIGNAL(...)
instead of:
SIGNAL(...)
Or you can import SIGNAL from QtCore explicitly:
from PyQt4.QtCore import SIGNAL