Pyqt save dom to file - python

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_())

Related

Change QLabel color using python code not html?

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_())

PyQt5 method not connected to button

This is my code for running PyQt, however the selectFile method is not called by the button. The UI code is converted from QtCreator. I've checked my objectName for the button is browseCSV
import sys
from readCSV import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog
import form
from function2 import *
from function4 import *
from Function6 import *
class App(QtWidgets.QMainWindow, form.Ui_MainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self) # This is defined in design.py file automatically
self.browseCSV.clicked.connect(self.selectFile)
def selectFile(self):
print ("Hello")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = form.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
You're not actually using your App class. So you need to do this:
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = App()
window.show()
sys.exit(app.exec_()
PS: don't ever use self.__class__ in a super call. In some scenarios, it can cause an infinite regress. If you're using Python 3, you can just use super().__init__() to avoid repeating the class name.

Python Building App

So here is the problem:
I am building an app in pyqt:
frame1.py
from PyQt5 import QtWidgets, QtCore, QtGui
import runpy, sys
class LoginFrame(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.logFrameSetup(self)
def logFrameSetup(self, GuiWin):
GuiWin.setWindowTitle('GuiWin')
GuiWin.resize(450, 215)
self.pushbutton = QtWidgets.QPushButton('Go To', GuiWin)
self.pushbutton.clicked.connect(self.change)
def change(self):
try:
runpy.run_path('frame2.py', run_name="__main__")
except:
pass
finally:
self.close()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = LoginFrame()
ex.show()
x = app.exec_()
sys.exit(x)
frame2.py
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class MainTradingPlatform(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setupFrame(self)
def setupFrame(self, frame2):
frame2.setWindowTitle('2frame')
frame2.resize(1200, 1000)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = MainTradingPlatform()
ex.show()
x = app.exec_()
sys.exit(x)
Now this works almost perfect, but when i close frame2.py, the process in background is still in progress/running.
What I want to achieve is that by closing frame2.py, process is finished with exit code. (terminated)
ps: after calling frame2.py, i would also like to terminate frame1.
Thank you for your help and sorry for my response.
When you execute the code with runpy, the process is separated from the initial application, an option to close the second application is to overwrite the closeEvent method.
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class MainTradingPlatform(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setupFrame(self)
def setupFrame(self, frame2):
frame2.setWindowTitle('2frame')
frame2.resize(1200, 1000)
def closeEvent(self, e):
sys.exit(0)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = MainTradingPlatform()
ex.show()
sys.exit(app.exec_())

Qtimer not working

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...

PYQT Adding extra menu items to an existing UI before it opens

Hi I have designed a basic GUI in QT and created a .py file from it.
When the window starts up I want to add another menu item. I have tried a few pieces of code I found on google but nothing seems to work. The code will need to go in the method addAdminMenu()
from PyQt4 import QtGui
import sys
from supplypy.core.windows.main_window import Ui_MainWindow
class SRM(QtGui.QWidget):
def __init__(self):
self.app = QtGui.QApplication(sys.argv)
self.MainWindow = QtGui.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.MainWindow)
self.MainWindow.show()
sys.exit(self.app.exec_())
def addAdminMenu(self):
pass
#####Add code here to create a Admin menu####
if __name__ == '__main__':
srm = SRM()
It should be as simple as accessing the menuBar() of the QMainWindow and adding an item, for example: (I removed the Ui_MainWindow lines just because I don't know what it's for -- a Windows requirement?)
from PyQt4 import QtGui
import sys
class SRM(QtGui.QWidget):
def __init__(self):
self.app = QtGui.QApplication(sys.argv)
self.MainWindow = QtGui.QMainWindow()
self.menubar = self.MainWindow.menuBar()
self.MainWindow.show()
self.addAdminMenu()
sys.exit(self.app.exec_())
def addAdminMenu(self):
self.menubar.addMenu('&Admin');
if __name__ == '__main__':
srm = SRM()

Categories

Resources