I want to use QTimer to update the GUI with sensor values.
I tried the following very simple code:
from pyQt4 import QtCore
def f():
try :
print ("text")
finally :
QtCore.QTimer.singleShot(5000, f)
f()
It's not working.
import sys
from PyQt4 import QtCore, QtGui
class MyApp(QtGui.QWidget):
def __init__(self):
self.print_hello()
def print_hello(self):
print 'hello'
QtCore.QTimer.singleShot(3000, self.print_hello)
qapp = QtGui.QApplication(sys.argv)
app = MyApp()
qapp.exec_()
I'm not sure why this works, but it has something to do with that the timer needs to be in running thread. I guess it's created with app object...
Related
I have searched for how to change QLabel text however I could not find anything which was not in html. Their was a few things claiming to do this but I could not get it to work really hoping for somthing I can copy and past then work out how it works by playing around with it.
Thank you
This is the code
import sys
from PyQt5 import QtWidgets, QtGui
class Program(QtWidgets.QWidget):
def __init__(self):
super().__init__()
"""expierment"""
test = QtWidgets.QLabel(self)
test.setText("I am trying to make this red?")
self.show()
app = QtWidgets.QApplication(sys.argv)
tradingApp = Program()
sys.exit(app.exec())
Use QPalette:
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
class Program(QtWidgets.QWidget):
def __init__(self):
super().__init__()
"""expierment"""
test = QtWidgets.QLabel(self)
pal = test.palette()
pal.setColor(QtGui.QPalette.WindowText, QtGui.QColor("red"))
test.setPalette(pal)
test.setText("I am trying to make this red?")
self.resize(test.sizeHint())
self.show()
app = QtWidgets.QApplication(sys.argv)
tradingApp = Program()
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_()
How come the textChanged event is not happening whenever I input some data in the QLineEdit?
from PyQt4.Qt import Qt, QObject,QLineEdit
from PyQt4.QtCore import pyqtSlot,SIGNAL,SLOT
from PyQt4 import QtGui, QtCore
import sys
class DirLineEdit(QLineEdit, QtCore.QObject):
"""docstring for DirLineEdit"""
#pyqtSlot(QtCore.QString)
def textChanged(self, string):
QtGui.QMessageBox.information(self,"Hello!","Current String is:\n"+string)
def __init__(self):
super(DirLineEdit, self).__init__()
self.connect(self,SIGNAL("textChanged(QString&)"),
self,SLOT("textChanged(QString *)"))
app = QtGui.QApplication(sys.argv)
smObj = DirLineEdit()
smObj.show()
app.exec_()
everything seems to be correct to me, what am I missing ?
Replace following line:
self.connect(self,SIGNAL("textChanged(QString&)"),
self,SLOT("textChanged(QString *)"))
with:
self.connect(self,SIGNAL("textChanged(QString)"),
self,SLOT("textChanged(QString)"))
Or you can use self.textChanged.connect (handler should be renamed because the name conflicts):
class DirLineEdit(QLineEdit, QtCore.QObject):
def on_text_changed(self, string):
QtGui.QMessageBox.information(self,"Hello!","Current String is:\n"+string)
def __init__(self):
super(DirLineEdit, self).__init__()
self.textChanged.connect(self.on_text_changed)
I am learning PyQt and coming from webdesign, so excuse this question that must have very obvious answer.So I am building a PyQt application and I would like to spread methods to several files to correspond different parts of GUI. How can I access textbox locating in fileA.py from fileB.py. :
#fileA.py
import sys
from PyQt4 import QtGui, QtCore
from gui1 import Ui_MainWindow
import fileB
class MyApp(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyApp()
window.show()
#This works all fine
def pressed():
window.plainTextEdit.appendPlainText("Hello")
window.pushButton.pressed.connect(pressed)
window.button2.pressed.connect(fileB.func3)
sys.exit(app.exec_())
Now, in this file I would like to use textbox from fileA.py
#fileB.py
import fileA
#How do I access window.plainTextEdit from fileA.py
def func3():
print "hello"
fileA.window.plainTextEdit.appendPlainText("Hello")
What am I doing wrong? What would be best way to spread functionality to multiple files if not this?
Thank you for taking time to read this.
You can take advantage of Python's class inheritance, like so:
fileA.py:
import sys
from PyQt4 import QtGui, QtCore
from gui1 import Ui_MainWindow
import fileB
class MyApp(fileB.MyApp, QtGui.QMainWindow):
def __init__(self):
self.MyMethod()
# Should print 'foo'
fileB.py:
import sys
from PyQt4 import QtGui, QtCore
from gui1 import Ui_MainWindow
class MyApp(QtGui.QMainWindow):
def MyMethod(self):
print 'foo'
Well, first off, the code under if __name__ == "__main__" will never be run when you are importing fileA.py, and so fileA.window does not exist. That's what it should do: run only when __name__ is "__main__", i.e. run as a top-level program. Instead, you should import fileA.py, create the QApplication and window again, then access window.plainTextEdit. However, this creates a very tight coupling between the code, as you are directly accessing a widget in MyApp from fileB. It might be better if instead, you expose a method in MyApp that appends to the text box instead of having fileB.py do it directly. So you may want to think about what you want to do and how to structure your program.
Of course, you don't have to structure your code that way; you could simply do
window = MyApp()
window.plainTextEdit.appendPlainText("Hello")
in fileB if you wanted.
Why this code does not work ?
I want save dom after js execute at this page and i want use qt without gui.
Sorry for my English.
#coding:utf-8
from PyQt4 import QtCore, QtWebKit
class Sp():
def save(self):
print "call"
data = self.webView.page().currentFrame().documentElement().toInnerXml()
open("htm","w").write(data)
def main(self):
self.webView = QtWebKit.QWebPage()
self.webView.load(QtCore.QUrl("http://www.google.com"))
QtCore.QObject.connect(self.webView,QtCore.SIGNAL("loadFinished(bool)"),self.save)
s = Sp()
s.main()
You have to create a QApplication before executing other stuff.
Add this:
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
s = Sp()
s.main()
sys.exit(app.exec_())
UPDATED: Also, change the code, because QWebPage doesn't have a load method:
import sys
from PyQt4 import QtGui, QtCore, QtWebKit
class Sp():
def save(self):
print "call"
data = self.webView.page().currentFrame().documentElement().toInnerXml()
open("htm","w").write(data)
print 'finished'
def main(self):
self.webView = QtWebKit.QWebView()
self.webView.load(QtCore.QUrl("http://www.google.com"))
QtCore.QObject.connect(self.webView,QtCore.SIGNAL("loadFinished(bool)"),self.save)
app = QtGui.QApplication(sys.argv)
s = Sp()
s.main()
sys.exit(app.exec_())