In my PyQt5 Application I want to add an Import data window in order to make sure delimiter, skiprows, etc.. is correctly set before trying to import data from a file. Obviously I want this to happen in a separate window. I'm not going to post the full code there, just a quick example which is similar.
In this example there are two windows: the main and the import window. I'd like to send simple text from import window to mainwindow. My problem is I can't really access the created instance of MainWindow, because that happens in main.py.
Here is the code:
main.py
import sys
from logic import MainProgram
from PyQt5 import QtWidgets
def main():
app = QtWidgets.QApplication(sys.argv)
my_interface = MainProgram()
my_interface.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
logic.py
from PyQt5.QtWidgets import QFileDialog
from PyQt5 import QtWidgets, QtCore
from mainwindow_ui import Ui_MainWindow
from import_ui import Ui_ImportPage
class MainProgram(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainProgram, self).__init__(parent)
self.setupUi(self)
self.open.clicked.connect(self.open_import_page)
def open_import_page(self):
self.window1 = ImportPage(self)
self.window1.show()
class ImportPage(QtWidgets.QMainWindow, Ui_ImportPage):
def __init__(self, parent=None):
super(ImportPage, self).__init__(parent)
self.setupUi(self)
self.get.clicked.connect(self.get_text)
def get_text(self):
print(self.line.text()) # <--- this is want I want to send back to MainProgram class
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
my_interface = MainProgram()
my_interface.show()
sys.exit(app.exec_())
mainwindow_ui.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.open = QtWidgets.QPushButton(self.centralwidget)
self.open.setObjectName("open")
self.verticalLayout.addWidget(self.open)
self.line2 = QtWidgets.QLineEdit(self.centralwidget)
self.line2.setPlaceholderText('Recieved from import page')
self.verticalLayout.addWidget(self.line2)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.open.setText(_translate("MainWindow", "Open import page"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
import_ui.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_ImportPage(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("Import")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.get = QtWidgets.QPushButton('Send data to MainWindow', self.centralwidget)
self.verticalLayout.addWidget(self.get)
self.line = QtWidgets.QLineEdit(self.centralwidget)
self.line.setObjectName("open")
self.line.setPlaceholderText('Type here what you want to send to MainWindow')
self.verticalLayout.addWidget(self.line)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Import"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_ImportPage()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Should I change the structure of the code to achieve this? Is there anything I'm missing?
Thank you for answering.
You can access the ImportPage instance in MainProgram as you create it in that scope. So you should make the necessary connections and get the text:
class MainProgram(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainProgram, self).__init__(parent)
self.setupUi(self)
self.open.clicked.connect(self.open_import_page)
self.window1 = ImportPage(self)
self.window1.get.clicked.connect(self.on_clicked)
def open_import_page(self):
self.window1.show()
def on_clicked(self):
print(self.window1.line.text())
Related
I have recreated a problem I am encountering as a minimal example below.
The situation: I have two Qt Designer generated GUI, each being called by their own separated scripts. A third script is meant to collect information from the first script upon the click of a button on the second script. I does not, yet there is no errors.
I have also attempted to solve this by using signals, but these does not seem to communicate between scripts. I provided a simpler version here that doesn't use signals per se.
My question is: How do You get a third script to handle information of two other scripts related to GUIs in pyqt5 ?
Here is the minimal example:
The first GUI script:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(504, 223)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.TypeHere = QtWidgets.QTextEdit(self.centralwidget)
self.TypeHere.setObjectName("TypeHere")
self.verticalLayout.addWidget(self.TypeHere)
self.HelloButton = QtWidgets.QPushButton(self.centralwidget)
self.HelloButton.setObjectName("HelloButton")
self.verticalLayout.addWidget(self.HelloButton)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 504, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.HelloButton.setText(_translate("MainWindow", "Say hello"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
The second GUI script:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(282, 392)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton01 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton01.setObjectName("pushButton01")
self.verticalLayout.addWidget(self.pushButton01)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 282, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton01.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
JustSomeTextv01, the script of the first GUI:
from PyQt5 import QtWidgets
from PyQt5.QtCore import QProcess, QThreadPool
from TypingUIv01 import Ui_MainWindow
import JustSomeButtonsv01 as JSB
import sys
class Window(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setupUi(self)
self.HelloButton.pressed.connect(self.openButtons)
self.Display = JSB.Window()
self.ButtonsThread()
def openButtons(self):
self.Display.show()
def ButtonsThread(self):
self.threadpoolbutt = QThreadPool()
self.threadpoolbutt.start(self.runButtons)
def runButtons(self):
self.butt = QProcess()
print("Buttons Running")
self.butt.execute('python',['JustSomeButtonsv01.py'])
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
JustSomeButtonsv01, the script of the second GUI:
from PyQt5 import QtWidgets
from PyQt5.QtCore import QProcess, QThreadPool
from ButtonsUIv01 import Ui_MainWindow
# import JustSomeRecordv01 as JSR
import sys
class Window(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setupUi(self)
self.RecordThread()
def RecordThread(self):
self.threadpoolrec = QThreadPool()
self.threadpoolrec.start(self.runRecord)
def runRecord(self):
self.rec = QProcess()
print("Record Running")
self.rec.execute('python',['JustSomeRecordv01.py'])
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
# window.show()
sys.exit(app.exec())
And finally, JustSomeRecordv01, the third script trying to interact with the other two:
from PyQt5 import QtWidgets
import sys
from TypingUIv01 import Ui_MainWindow as JSTWin
from ButtonsUIv01 import Ui_MainWindow as ButtWin
class Record(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
# self.setupUi(self)
app2 = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
self.Win = JSTWin()
self.Win.setupUi(MainWindow)
self.Text = self.Win.TypeHere.toPlainText()
print("Running")
self.Butt = ButtWin()
self.Butt.setupUi(MainWindow)
self.Butt.pushButton01.pressed.connect(self.PrintIT)
def PrintIT(self):
print("Texting")
print(self.Text)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Record()
# window.show()
sys.exit(app.exec())
How to reproduce the problem:
You execute the JustSomeTextv01 script. Press the "Hello Button" and a second window will show up. You type anything in the QTextEdit of the first window and then click the button of the second window. The intent is that this second button would print what You wrote, but it doesn't work.
Thank You for your time,
I managed to do it within the example scripts, but the only solution was to put everything not-GUI into the same script as so:
# To test textbox related function
from PyQt5 import QtWidgets, QtCore
from TypingUIv01 import Ui_MainWindow
from ButtonsUIv01 import Ui_MainWindow as Ui2
import sys
class Window(QtWidgets.QMainWindow, Ui_MainWindow):
PatchSignal = QtCore.pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setupUi(self)
self.HelloButton.pressed.connect(self.openButtons)
self.PatchSignal.connect(self.printIT)
def openButtons(self):
self.w2 = Wintwo()
self.w2.show()
def printIT(self):
self.Text = self.TypeHere.toPlainText()
# print("PRINTING")
print(self.Text)
class Wintwo(QtWidgets.QMainWindow, Ui2):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setupUi(self)
self.pushButton01.pressed.connect(self.Emit)
self.emitter = window
def Emit(self):
# print("EMITTING")
self.emitter.PatchSignal.emit()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
It seems sending Signals between scripts is simply not possible in pyqt5!
Somehow this doesn't work in my actual script since "window" isn't recognized, but that's another question for another day.
I am trying to get the date from a calendar widget to get printed into a textbox. The calendar widget and the textbox are in the same class but the print function is in a different function. all in the same class.
here is the code:
from PyQt5 import QtCore, QtGui, QtWidgets
import calendar
I have the printDate function to print the date
class Ui_Calendar(object):
def printDate(self, qDate):
date =('{0}-{1}-{2}'.format(qDate.month(), qDate.day(), qDate.year()))
self.setupUi.textedit.setText(date)
Here in this function i have the calendar widget and the textbox:
def setupUi(self, MainWindow):
calendar widget:
self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
self.calendarWidget.setGeometry(QtCore.QRect(0, 0, 392, 236))
self.calendarWidget.setObjectName("calendarWidget")
self.calendarWidget.clicked.connect(self.printDate)
textbox:
self.textedit = QtWidgets.QLineEdit(self.centralwidget)
self.textedit.setGeometry(QtCore.QRect(20,245,80,30))
self.textedit.setObjectName("textedit")
I get the calendar widget to open when button clicked. I also can get the date printed on python shell but cannot the date on the textbox. As soon as i click the date on the calendar widget the program closes itself. i dont get any error msg.
I am new to programming and just started learning. Please help me
here is my whole code:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Calendar(object):
def printDate(self, qDate):
date =('{0}-{1}-{2}'.format(qDate.month(), qDate.day(), qDate.year()))
#print(date)
self.setupUi.textedit.setText(self.date.toStrings())
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(395, 310)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
self.calendarWidget.setGeometry(QtCore.QRect(0, 0, 392, 236))
self.calendarWidget.setObjectName("calendarWidget")
#self.calendarWidget.clicked.connect(lambda dateval:print(dateval))
self.calendarWidget.clicked.connect(self.printDate)
MainWindow.setCentralWidget(self.centralwidget)
self.textedit = QtWidgets.QLineEdit(self.centralwidget)
self.textedit.setGeometry(QtCore.QRect(20,245,80,30))
self.textedit.setObjectName("textedit")
#self.printDate(self.label.setText(date))
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
#self.label.setText(_translate("Mainwindow","date"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_Calendar()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Do not modify the code generated by Qt Designer but create another class that inherits
from the appropriate widget and use the initial class to fill it.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Calendar(object):
# def printDate(self, qDate):
# date =('{0}-{1}-{2}'.format(qDate.month(), qDate.day(), qDate.year()))
# #print(date)
# self.setupUi.textedit.setText(self.date.toStrings())
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(395, 310)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
self.calendarWidget.setGeometry(QtCore.QRect(0, 0, 392, 236))
self.calendarWidget.setObjectName("calendarWidget")
#self.calendarWidget.clicked.connect(lambda dateval:print(dateval))
# self.calendarWidget.clicked.connect(self.printDate)
MainWindow.setCentralWidget(self.centralwidget)
self.textedit = QtWidgets.QLineEdit(self.centralwidget)
self.textedit.setGeometry(QtCore.QRect(20,245,80,30))
self.textedit.setObjectName("textedit")
#self.printDate(self.label.setText(date))
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
#self.label.setText(_translate("Mainwindow","date"))
class Calendar(QtWidgets.QMainWindow, Ui_Calendar):
def __init__(self):
super().__init__()
self.setupUi(self)
self.calendarWidget.clicked.connect(self.printDate)
def printDate(self, qDate):
date =('{0}-{1}-{2}'.format(qDate.month(), qDate.day(), qDate.year()))
# self.setupUi.textedit.setText(self.date.toStrings())
self.textedit.setText(date)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
# MainWindow = QtWidgets.QMainWindow()
# ui = Ui_Calendar()
# ui.setupUi(MainWindow)
# MainWindow.show()
w = Calendar()
w.show()
sys.exit(app.exec_())
I am making a GUI in PyQt5 which uses QTreeWidget. I want a particular column to be integer only column. The user shouldn't be able to enter any non integer item in it. I saw some methods using QVariant but it doesn't seem to fullfill my requirement.
Here is what I found!
.But this seems to me like setting the data from the back-end not from the user side.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(478, 320)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.treeWidget = QtWidgets.QTreeWidget(self.centralwidget)
self.treeWidget.setAlternatingRowColors(True)
self.treeWidget.setObjectName("treeWidget")
item_0 = QtWidgets.QTreeWidgetItem(self.treeWidget)
item_1 = QtWidgets.QTreeWidgetItem(item_0)
item_1.setFlags(QtCore.Qt.ItemIsSelectable|QtCore.Qt.ItemIsEditable|QtCore.Qt.ItemIsDragEnabled|QtCore.Qt.ItemIsUserCheckable|QtCore.Qt.ItemIsEnabled)
self.verticalLayout.addWidget(self.treeWidget)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 478, 20))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.treeWidget.headerItem().setText(0, _translate("MainWindow", "Some other data"))
self.treeWidget.headerItem().setText(1, _translate("MainWindow", "Only Integers"))
__sortingEnabled = self.treeWidget.isSortingEnabled()
self.treeWidget.setSortingEnabled(False)
self.treeWidget.topLevelItem(0).setText(0, _translate("MainWindow", "Text"))
self.treeWidget.topLevelItem(0).child(0).setText(0, _translate("MainWindow", "Sub Text"))
self.treeWidget.setSortingEnabled(__sortingEnabled)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Here is code and the image. Please let me know what changes should i make in the code to achieve my objective. Thanks in advance.
You have to establish a delegate in the second column that has as editor a QSpinBox:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
# ...
class IntegerDelegate(QtWidgets.QStyledItemDelegate):
def createEditor(self, parent, option, index):
editor = QtWidgets.QSpinBox(parent)
editor.setFrame(False)
editor.setMinimum(-2147483648)
editor.setMaximum(2147483647)
editor.setSizePolicy(
QtWidgets.QSizePolicy.Ignored, editor.sizePolicy().verticalPolicy()
)
return editor
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
delegate = IntegerDelegate(self.treeWidget)
self.treeWidget.setItemDelegateForColumn(1, delegate)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Update:
class DoubleDelegate(QtWidgets.QStyledItemDelegate):
def createEditor(self, parent, option, index):
editor = QtWidgets.QDoubleSpinBox(parent)
editor.setFrame(False)
editor.setMinimum(-1.7976931348623157e+308)
editor.setMaximum(1.7976931348623157e+308)
editor.setSizePolicy(
QtWidgets.QSizePolicy.Ignored, editor.sizePolicy().verticalPolicy()
)
return editor
I need to make my program remove Access-Control-Allow-Origin on any website it goes to, how can I do this? Here is the code I have right now:
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.webView = QtWebEngineWidgets.QWebEngineView(self.centralwidget)
self.webView.setUrl(QtCore.QUrl("thewebsite"))
self.webView.loadFinished.connect(self.reform)
self.webView.setObjectName("webView")
self.gridLayout.addWidget(self.webView, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def reform(self, ok):
payload = open('payload.js', 'r').read()
if ok:
self.webView.page().runJavaScript('alert("Ready");')
self.webView.page().runJavaScript(payload)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
from PyQt5 import QtWebEngineWidgets
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.setFixedSize(500, 500)
MainWindow.show()
sys.exit(app.exec_())
How do I make it so it disables Access-Control-Allow-Origin?
You could try enabling the LocalContentCanAccessRemoteUrls WebAttribute:
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
settings = QtWebEngineWidgets.QWebEngineSettings.defaultSettings()
settings.setAttribute(QtWebEngineWidgets.QWebEngineSettings.LocalContentCanAccessRemoteUrls, True)
...
Im first trying to connect a sample cancel button that when clicked will show 'Hello'. Again, thats just a sample. I just like to know how can I connect my ui to the python code that id like to use for the cancel button. I hope someone can help me. Im new to python.
Heres the code:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSlot
from A3AMainWindow import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__()
self.setupUi(self)
self.connect(self.btn_inv_cancel.clicked.connect(self.on_click))
#pyqtSlot()
def on_click(self):
QMessageBox.Information(Self, "hello")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Try it:
from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox
from PyQt5.QtCore import pyqtSlot
from A3AMainWindow import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.on_click)
#pyqtSlot()
def on_click(self):
QMessageBox.information(self, "Announcement.","Hello")
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
A3AMainWindow.py
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(400, 300)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(134, 110, 121, 71))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 400, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())