I'm developing a web browser in Python and PyQt5. The browser is working fine but new tab and javascript window.open() is not working. I want to know how to enable new tab and popup functions. javascript alert() function is working but new window and new tab is not working.
If you could help me then I'll be very thankful to you.
from PyQt5 import QtCore, QtGui, QtWidgets # +++ QtWidgets
from PyQt5 import QtWebEngineWidgets
from PyQt5.QtWebEngineWidgets import QWebEnginePage,QWebEngineSettings
class Ui_MainWindow(object):
def back(self): self.webView.back()
def forword(self): self.webView.forward()
def reload(self): self.webView.reload()
def stop(self): self.webView.stop()
def newtab(self): self.QWebEnginePage.OpenLinkInNewTab()
def createWindow(self, windowType):
if windowType == QWebEnginePage.WebBrowserTab:
self.webView = Ui_MainWindow()
self.webView.setAttribute(Qt.WA_DeleteOnClose, True)
self.webView.show()
return self.webView
return super(Ui_MainWindow, self).createWindow(windowType)
def home(self):
self.webView.load(QtCore.QUrl("http://pyxisad.com"))
QWebEnginePage.JavascriptCanOpenWindows:True
def search(self):
url = self.ln_addressbar.text()
self.webView.load(QtCore.QUrl(url))
QWebEnginePage.JavascriptCanOpenWindows:True
def setupUi(self, MainWindow):
MainWindow.setObjectName("PyxisAd")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tb_back = QtWidgets.QToolButton(self.centralwidget)
self.tb_back.setGeometry(QtCore.QRect(10, 20, 41, 31))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("icons/back.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.tb_back.setIcon(icon)
self.tb_back.setIconSize(QtCore.QSize(32, 32))
self.tb_back.setObjectName("tb_back")
############### Event #############################
self.tb_back.clicked.connect(self.back)
##################################################
self.tb_reload = QtWidgets.QToolButton(self.centralwidget)
self.tb_reload.setGeometry(QtCore.QRect(60, 20, 41, 31))
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("icons/reload.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.tb_reload.setIcon(icon1)
self.tb_reload.setIconSize(QtCore.QSize(32, 32))
self.tb_reload.setObjectName("tb_reload")
############### Event #############################3
self.tb_reload.clicked.connect(self.reload)
##################################################
self.tb_forword_2 = QtWidgets.QToolButton(self.centralwidget)
self.tb_forword_2.setGeometry(QtCore.QRect(110, 20, 41, 31))
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("icons/forword.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.tb_forword_2.setIcon(icon2)
self.tb_forword_2.setIconSize(QtCore.QSize(32, 32))
self.tb_forword_2.setObjectName("tb_forword_2")
############### Event #############################3
self.tb_forword_2.clicked.connect(self.forword)
##################################################
self.tb_home = QtWidgets.QToolButton(self.centralwidget)
self.tb_home.setGeometry(QtCore.QRect(160, 20, 41, 31))
icon3 = QtGui.QIcon()
icon3.addPixmap(QtGui.QPixmap("icons/home.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.tb_home.setIcon(icon3)
self.tb_home.setIconSize(QtCore.QSize(32, 32))
self.tb_home.setObjectName("tb_home")
############### Event #############################3
self.tb_home.clicked.connect(self.home)
##################################################
self.ln_addressbar = QtWidgets.QLineEdit(self.centralwidget)
self.ln_addressbar.setGeometry(QtCore.QRect(210, 20, 491, 31))
self.ln_addressbar.setObjectName("ln_addressbar")
self.ln_addressbar.setPlaceholderText("Enter URL..")
self.tb_search = QtWidgets.QToolButton(self.centralwidget)
self.tb_search.setGeometry(QtCore.QRect(710, 20, 41, 31))
icon4 = QtGui.QIcon()
icon4.addPixmap(QtGui.QPixmap("icons/search.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.tb_search.setIcon(icon4)
self.tb_search.setIconSize(QtCore.QSize(32, 32))
self.tb_search.setObjectName("tb_search")
############### Event #############################3
self.tb_search.clicked.connect(self.search)
#self.tb_search.returnPressed.connect(self.search)
##################################################
self.webView = QtWebEngineWidgets.QWebEngineView(self.centralwidget) # + QWebEngineView
self.webView.setGeometry(QtCore.QRect(0, 60, 1400, 700))
self.webView.setUrl(QtCore.QUrl("about:blank"))
self.webView.setObjectName("webView")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
############### Event #############################
#self.newtab.clicked.connect(createWindow())
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("PyxisAd", "PyxisAd"))
self.tb_back.setText(_translate("PyxisAd", "back"))
self.tb_reload.setText(_translate("PyxisAd", "reload"))
self.tb_forword_2.setText(_translate("PyxisAd", "forword"))
self.tb_home.setText(_translate("PyxisAd", "home"))
self.tb_search.setText(_translate("PyxisAd", "search"))
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_())
Related
I have this Qt Designer made main-window: main.py:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(364, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setMinimumSize(QtCore.QSize(341, 71))
font = QtGui.QFont()
font.setPointSize(14)
font.setBold(True)
font.setItalic(False)
font.setWeight(75)
self.label.setFont(font)
self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.models = QtWidgets.QGroupBox(self.centralwidget)
self.models.setObjectName("models")
self.gridLayout_2 = QtWidgets.QGridLayout(self.models)
self.gridLayout_2.setObjectName("gridLayout_2")
self.scrollArea = QtWidgets.QScrollArea(self.models)
self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 311, 196))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.scrollAreaWidgetContents.sizePolicy().hasHeightForWidth())
self.scrollAreaWidgetContents.setSizePolicy(sizePolicy)
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.gridLayout = QtWidgets.QGridLayout(self.scrollAreaWidgetContents)
self.gridLayout.setObjectName("gridLayout")
spacerItem = QtWidgets.QSpacerItem(296, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem, 0, 0, 1, 1)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.gridLayout_2.addWidget(self.scrollArea, 0, 0, 1, 1)
self.pushButton_parent_none = QtWidgets.QPushButton(self.models)
self.pushButton_parent_none.setObjectName("pushButton_parent_none")
self.gridLayout_2.addWidget(self.pushButton_parent_none, 1, 0, 1, 1)
self.pushButton_parent_main = QtWidgets.QPushButton(self.models)
self.pushButton_parent_main.setObjectName("pushButton_parent_main")
self.gridLayout_2.addWidget(self.pushButton_parent_main, 2, 0, 1, 1)
self.verticalLayout.addWidget(self.models)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 364, 29))
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.label.setText(_translate("MainWindow", "Main"))
self.models.setTitle(_translate("MainWindow", "GroupBox"))
self.pushButton_parent_none.setText(_translate("MainWindow", "Test-set-parent_None"))
self.pushButton_parent_main.setText(_translate("MainWindow", "Test-set-parent_Main"))
and this code : myapp.py :
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDesktopWidget
from main import Ui_MainWindow
class MyApp(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyApp, self).__init__(parent)
self.setupUi(self)
self.qtRectangle = self.frameGeometry()
self.centerPoint = QDesktopWidget().availableGeometry().center()
self.qtRectangle.moveCenter(self.centerPoint)
self.move(self.qtRectangle.topLeft())
self.pushButton_parent_none.clicked.connect(
lambda checked, x=self.models: self.set_parent_none(x))
self.pushButton_parent_main.clicked.connect(
lambda checked, x=self.models: self.set_parent_main(x))
self.cnt = 0
self.models.installEventFilter(self)
self.show
def eventFilter(self, source, event):
if source == self.models:
self.cnt += 1
# print('filteredddddddddddddd : ', self.cnt, event, type(event), source)
# print('filteredddddddddddddd', event.type(), event)
if event.type() == 19:
print(source.windowTitle())
print(source)
print('filteredddddddddddddd : ',
self.cnt, event.type(), event, type(event), source)
print('source.parent() :', source.parent())
self.set_parent_main(source)
print('source.parent() :', source.parent())
return True
else:
return False
else:
return False
def closeEvent(self, evnt):
print('clooossssing main !!')
evnt.accept()
def set_parent_main(self, sender):
print('main before !!!!!! :', sender.parent())
sender.setParent(self)
print('main after !!!!!! :', sender.parent())
sender.hide()
sender.show()
def set_parent_none(self, sender):
print('none before !!!!! :', sender.parent())
sender.setParent(None)
print('none after !!!!! :', sender.parent())
sender.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
myapp = MyApp()
myapp.show()
try:
sys.exit(app.exec_())
except SystemExit:
print('Closing Window...')
When I click the Test-set-parent_None button the QGroupBox Widget gets detached from MainWindow; and it gets re-attached if I click the Test-set-parent_Main button.
What is puzzling me it's why my eventFilter it's not working. If I close the QGroupBox with the X closing button (top right corner), I get the :
main before !!!!!! : None
main after !!!!!! : <__main__.MyApp object at 0x7ff7f35be550>
source.parent() : <__main__.MyApp object at 0x7ff7f35be550>
print that tells me that my QGroupBox is again the child of my MainWindow - but it doesn't show up. What am I missing in my 'eventFilter' implementation?
Returning True in the event-filter isn't always sufficient to prevent propagation of the event. For close-events, you must also explicitly ignore the event:
class MyApp(QMainWindow, Ui_MainWindow):
...
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.Close and source is self.models:
self.set_parent_main(source)
event.ignore()
return True
return super().eventFilter(source, event)
I created a back button in new window and next button in main window. After I clicked the next button to show a new window, it was successfully hides the current main window but when I clicked back button on the new window, the application automatically closes itself. Any idea?
The code in first mainWindow.py file:
from PyQt5 import QtCore, QtGui, QtWidgets
try:
from secondWindow import Ui_SecondWindow
except:
import secondWindow
class Ui_firstWindow(object):
def secondWindow(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_SecondWindow()
self.ui.setupUi(self.window)
self.window.show()
if self.window.show():
self.hide
def setupUi(self, firstWindow):
firstWindow.setObjectName("firstWindow")
firstWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(firstWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_next = QtWidgets.QPushButton(self.centralwidget)
self.btn_next.setGeometry(QtCore.QRect(160, 100, 521, 291))
self.btn_next.setObjectName("btn_next")
######To Open second window###########
self.btn_next.clicked.connect(self.secondWindow)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(360, 420, 151, 16))
self.label.setStyleSheet("font: 11pt \"MS Shell Dlg 2\";")
self.label.setObjectName("label")
firstWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(firstWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
firstWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(firstWindow)
self.statusbar.setObjectName("statusbar")
firstWindow.setStatusBar(self.statusbar)
self.retranslateUi(firstWindow)
QtCore.QMetaObject.connectSlotsByName(firstWindow)
def retranslateUi(self, firstWindow):
_translate = QtCore.QCoreApplication.translate
firstWindow.setWindowTitle(_translate("firstWindow", "MainWindow"))
self.btn_next.setText(_translate("firstWindow", "Next"))
self.label.setText(_translate("firstWindow", "First Window"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
firstWindow = QtWidgets.QMainWindow()
ui = Ui_firstWindow()
ui.setupUi(firstWindow)
firstWindow.show()
sys.exit(app.exec_())
The codes in second secondWindow.py file:
from PyQt5 import QtCore, QtGui, QtWidgets
try:
from mainWindow import Ui_firstWindow
except:
import mainWindow
class Ui_SecondWindow(object):
###function to go back the first window######
# #pyqtSlot()
def openBackFirstWindow(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_firstWindow()
self.ui.setupUi(self.window)
self.window.show()
if self.window.show():
self.hide
def setupUi(self, SecondWindow):
SecondWindow.setObjectName("SecondWindow")
SecondWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(SecondWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_back = QtWidgets.QPushButton(self.centralwidget)
self.btn_back.setGeometry(QtCore.QRect(220, 100, 401, 281))
self.btn_back.setObjectName("btn_back")
###To go back to first windo######
self.btn_back.clicked.connect(self.openBackFirstWindow)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(370, 440, 151, 16))
self.label.setStyleSheet("font: 11pt \"MS Shell Dlg 2\";")
self.label.setObjectName("label")
SecondWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(SecondWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
SecondWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(SecondWindow)
self.statusbar.setObjectName("statusbar")
SecondWindow.setStatusBar(self.statusbar)
self.retranslateUi(SecondWindow)
QtCore.QMetaObject.connectSlotsByName(SecondWindow)
def retranslateUi(self, SecondWindow):
_translate = QtCore.QCoreApplication.translate
SecondWindow.setWindowTitle(_translate("SecondWindow", "MainWindow"))
self.btn_back.setText(_translate("SecondWindow", "Back"))
self.label.setText(_translate("SecondWindow", "Second Window"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
SecondWindow = QtWidgets.QMainWindow()
ui = Ui_SecondWindow()
ui.setupUi(SecondWindow)
SecondWindow.show()
sys.exit(app.exec_())
When i clicked the back button in second window, the application stopped working and exits itself. Any idea?
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_firstWindow(object):
def setupUi(self, firstWindow):
firstWindow.setObjectName("firstWindow")
firstWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(firstWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_next = QtWidgets.QPushButton(self.centralwidget)
self.btn_next.setGeometry(QtCore.QRect(160, 100, 521, 291))
self.btn_next.setObjectName("btn_next")
# ######To Open second window###########
# self.btn_next.clicked.connect(self.secondWindow)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(360, 420, 151, 16))
self.label.setStyleSheet("font: 11pt \"MS Shell Dlg 2\";")
self.label.setObjectName("label")
firstWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(firstWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
firstWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(firstWindow)
self.statusbar.setObjectName("statusbar")
firstWindow.setStatusBar(self.statusbar)
self.retranslateUi(firstWindow)
QtCore.QMetaObject.connectSlotsByName(firstWindow)
def retranslateUi(self, firstWindow):
_translate = QtCore.QCoreApplication.translate
firstWindow.setWindowTitle(_translate("firstWindow", "MainWindow"))
self.btn_next.setText(_translate("firstWindow", "Next"))
self.label.setText(_translate("firstWindow", "First Window"))
class Ui_SecondWindow(object):
def setupUi(self, SecondWindow):
SecondWindow.setObjectName("SecondWindow")
SecondWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(SecondWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_back = QtWidgets.QPushButton(self.centralwidget)
self.btn_back.setGeometry(QtCore.QRect(220, 100, 401, 281))
self.btn_back.setObjectName("btn_back")
# ###To go back to first windo######
# self.btn_back.clicked.connect(self.openBackFirstWindow)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(370, 440, 151, 16))
self.label.setStyleSheet("font: 11pt \"MS Shell Dlg 2\";")
self.label.setObjectName("label")
SecondWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(SecondWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
SecondWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(SecondWindow)
self.statusbar.setObjectName("statusbar")
SecondWindow.setStatusBar(self.statusbar)
self.retranslateUi(SecondWindow)
QtCore.QMetaObject.connectSlotsByName(SecondWindow)
def retranslateUi(self, SecondWindow):
_translate = QtCore.QCoreApplication.translate
SecondWindow.setWindowTitle(_translate("SecondWindow", "MainWindow"))
self.btn_back.setText(_translate("SecondWindow", "Back"))
self.label.setText(_translate("SecondWindow", "Second Window"))
class SecondWindow(QtWidgets.QMainWindow, Ui_SecondWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
class MainWindow(QtWidgets.QMainWindow, Ui_firstWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
######To Open second window###########
self.btn_next.clicked.connect(self.secondWindow)
def secondWindow(self):
self.window = SecondWindow()
###To go back to first windo######
self.window.btn_back.clicked.connect(self.openBackFirstWindow)
self.window.show()
self.hide()
###function to go back the first window######
def openBackFirstWindow(self):
self.show()
self.window.hide()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
firstWindow = MainWindow()
firstWindow.show()
sys.exit(app.exec_())
im pretty new to working with pyqt5 in python, everything is kinda running how it is supposed to be. the only issue i have is that when im triggering to start the function stepperaction(self, index) with the "START" button, i can see the code running in the console, but the UI crashes while the code is running if i click anywhere on the ui with my mouse. i would like to be able to press the "STOP" button in the UI even while the function is running. How am i able to fix this?
# -*- coding: utf-8 -*-
# Created by: PyQt5 UI code generator 5.13.0
import time
import sys
# import RPi.GPIO as GPIO
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QVariant
stepcount = 0
class Ui_mainWindow(object):
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
mainWindow.resize(557, 320)
self.centralwidget = QtWidgets.QWidget(mainWindow)
self.centralwidget.setEnabled(True)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(360, 40, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(360, 70, 75, 23))
self.pushButton_4.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(270, 70, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(150, 40, 101, 22))
self.comboBox.setEditable(False)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("", QVariant(10*1600))
self.comboBox.addItem("", QVariant(20))
self.comboBox.addItem("", QVariant(30))
self.comboBox.addItem("", QVariant(40))
self.comboBox.addItem("", QVariant(50))
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 30, 121, 31))
font = QtGui.QFont()
font.setPointSize(12)
self.label.setFont(font)
self.label.setObjectName("label")
self.progressBar = QtWidgets.QProgressBar(self.centralwidget)
self.progressBar.setGeometry(QtCore.QRect(20, 250, 118, 23))
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName("progressBar")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(270, 40, 75, 23))
self.pushButton_3.setObjectName("pushButton_3")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(400, 252, 71, 21))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(480, 252, 51, 21))
self.label_3.setObjectName("label_3")
mainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(mainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 557, 21))
self.menubar.setObjectName("menubar")
self.menuSmart_Spray_Booth_Configuration = QtWidgets.QMenu(self.menubar)
self.menuSmart_Spray_Booth_Configuration.setObjectName("menuSmart_Spray_Booth_Configuration")
mainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(mainWindow)
self.statusbar.setObjectName("statusbar")
mainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuSmart_Spray_Booth_Configuration.menuAction())
self.retranslateUi(mainWindow)
QtCore.QMetaObject.connectSlotsByName(mainWindow)
self.comboBox.activated.connect(self.handleactivated)
self.pushButton.clicked.connect(lambda: self.stepperaction(index=self.comboBox.currentIndex()))
self.pushButton_2.clicked.connect(lambda: self.stop())
def handleactivated(self, index):
print(self.comboBox.itemText(index), "handle")
print(self.comboBox.itemData(index), "handle")
self.label_3.setText(self.comboBox.itemText(index))
def retranslateUi(self, mainWindow):
_translate = QtCore.QCoreApplication.translate
mainWindow.setWindowTitle(_translate("mainWindow", "Smart Spray Booth - Configuration"))
self.pushButton.setText(_translate("mainWindow", "START"))
self.pushButton_2.setText(_translate("mainWindow", "STOP"))
self.pushButton_3.setText(_translate("mainWindow", "Nullpunkt"))
self.comboBox.setItemText(0, _translate("mainWindow", "5cm"))
self.comboBox.setItemText(1, _translate("mainWindow", "10cm"))
self.comboBox.setItemText(2, _translate("mainWindow", "15cm"))
self.comboBox.setItemText(3, _translate("mainWindow", "20cm"))
self.comboBox.setItemText(4, _translate("mainWindow", "25cm"))
self.label.setText(_translate("mainWindow", "Bauteilgröße:"))
self.label_2.setText(_translate("mainWindow", "Bauteilgröße:"))
self.label_3.setText(_translate("mainWindow", "0 cm"))
self.menuSmart_Spray_Booth_Configuration.setTitle(_translate("mainWindow", "Smart Spray Booth - Configuration"))
if self.comboBox.currentTextChanged:
print(self.comboBox.currentData(), "from if")
def stop(self):
print("programm wird beendet")
sys.exit()
def stepperaction(self, index):
# GPIO.setmode(GPIO.BOARD) # read the pin as board instead of BCM pin
global stepcount
stepcount = self.comboBox.itemData(index)
print(stepcount, "this is given step")
# return stepcount
FastSpeed = 0.0001 # old = 0.001 Change this depends on your stepper motor
LowSpeed = 0.0001 # old = 0.001 Change this depends on your stepper motor
counter = 0
countery = 0
while True and counter < 3:
print("Move Up", stepcount, "steps")
for i in range(stepcount):
countery = countery + 1
print(countery)
time.sleep(LowSpeed)
print(countery)
time.sleep(LowSpeed)
time.sleep(1)
print("Move Down", stepcount, "steps")
for i in range(stepcount):
countery = countery + 1
print(countery)
time.sleep(FastSpeed)
print(countery)
time.sleep(FastSpeed)
time.sleep(1)
counter += 1
print("success")
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_())
I can't test on PyQt5, but on PySide you just need to pass the name of the function to connect, not execute it. It might also be better to avoid using lambdas in QT slots.
Try this modification:
....
self.pushButton.clicked.connect(self.stepperaction)
....
def stepperaction(self, index=None):
if index is None:
index = self.comboBox.currentIndex()
# GPIO.setmode(GPIO.BOARD) # read the pin as board instead of BCM pin
global stepcount
....
I have This Qt MainWindow Shown Below:
I want my Browse button to Open a Dialog box for selecting Specific Directory.
I Went through the various post on Stack Overflow, I tried implementing the solution in the post, but It's not working for me.
Here is my Code :
from PyQt5 import QtCore, QtGui, QtWidgets
import os
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.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
self.start_button = QtWidgets.QPushButton(self.centralwidget)
self.start_button.setAutoFillBackground(False)
self.start_button.setAutoDefault(False)
self.start_button.setDefault(False)
self.start_button.setFlat(False)
self.start_button.setObjectName("start_button")
self.start_button.clicked.connect(lambda: self.start_button_click())
self.gridLayout_2.addWidget(self.start_button, 0, 0, 1, 1)
self.Br_button = QtWidgets.QPushButton(self.centralwidget)
self.Br_button.setObjectName("Br_button")
self.Br_button.clicked.connect(lambda: self.browse_button())
self.gridLayout_2.addWidget(self.Br_button, 2, 0, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.gridLayout_2.addWidget(self.label, 2, 1, 1, 1, QtCore.Qt.AlignHCenter)
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(0)
self.tableWidget.setRowCount(0)
self.gridLayout_2.addWidget(self.tableWidget, 3, 0, 1, 2)
self.gridLayout.addLayout(self.gridLayout_2, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.start_button.setToolTip(_translate("MainWindow", "Start The Program"))
self.start_button.setText(_translate("MainWindow", "Start"))
self.Br_button.setToolTip(_translate("MainWindow", "Browse the File Location to Watch on"))
self.Br_button.setText(_translate("MainWindow", "Browse"))
def start_button_click(self):
self.label.setText("Hello")
def browse_button(self):
fileName = QtWidgets.QFileDialog.getExistingDirectory(QtWidgets.QFileDialog,None,"Open Directory",os.getcwd(), QtWidgets.QFileDialog.ShowDirsOnly)
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_())
When I Click on the Browse button it closes the Application abruptly after few seconds and I am Getting This Error Shown below:
Try it:
import sys
import os
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Form(QMainWindow):
def __init__(self,parent=None):
super().__init__(parent)
self.plainTextEdit = QPlainTextEdit()
self.plainTextEdit.setFont(QFont('Arial', 11))
openDirButton = QPushButton("Open Directory")
openDirButton.clicked.connect(self.browse_button)
layoutV = QVBoxLayout()
layoutV.addWidget(openDirButton)
layoutH = QHBoxLayout()
layoutH.addLayout(layoutV)
layoutH.addWidget(self.plainTextEdit)
centerWidget = QWidget()
centerWidget.setLayout(layoutH)
self.setCentralWidget(centerWidget)
def browse_button(self):
fileName = QFileDialog.getExistingDirectory(
#QtWidgets.QFileDialog, # ???
None,
"Open Directory",
os.getcwd(),
QFileDialog.ShowDirsOnly)
self.plainTextEdit.appendHtml("<br>Chose a folder: <b>{}</b>".format(fileName))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Form()
ex.resize(740,480)
ex.setWindowTitle("PyQt5-QFileDialog")
ex.show()
sys.exit(app.exec_())
I am having a pyhton script having some code as follows and and .ui file converted into the .py file. Now i want to execute the python script in widget code of gui widget?
Please help me how can i do that .
from PyQt5 import QtCore, QtGui, QtWidgets
import DC_gimbal as backend
import hudsup as hp
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(978, 600)
self.hudsup = hudsup()
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 291, 261))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.Heads_up = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.Heads_up.setContentsMargins(0, 0, 0, 0)
self.Heads_up.setObjectName("Heads_up")
self.widget = QtWidgets.QWidget(self.verticalLayoutWidget)
self.widget.setObjectName("widget")
self.HeadsWidget = QtWidgets.QWidget(self.widget)
self.HeadsWidget.setGeometry(QtCore.QRect(0, 0, 291, 261))
self.HeadsWidget.setObjectName("HeadsWidget")
self.Heads_up.addWidget(self.widget)
self.Heads_up.addWidget(self.huds_Up())
self.verticalLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(0, 260, 291, 291))
self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_2)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.widget_3 = QtWidgets.QWidget(self.verticalLayoutWidget_2)
self.widget_3.setObjectName("widget_3")
self.displayWidget = QtWidgets.QWidget(self.widget_3)
self.displayWidget.setGeometry(QtCore.QRect(0, 0, 291, 281))
self.displayWidget.setObjectName("displayWidget")
self.verticalLayout.addWidget(self.widget_3)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(500, 10, 93, 28))
self.pushButton.setObjectName("connect")
self.pushButton.clicked.connect(backend.Noctua_initialise())
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(620, 10, 93, 28))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(740, 10, 93, 28))
self.pushButton_3.setObjectName("pushButton_3")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(290, 330, 561, 211))
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 978, 26))
self.menubar.setObjectName("menubar")
self.menuconnect = QtWidgets.QMenu(self.menubar)
self.menuconnect.setObjectName("menuconnect")
self.menuDisconnect = QtWidgets.QMenu(self.menubar)
self.menuDisconnect.setObjectName("menuDisconnect")
self.menuMode = QtWidgets.QMenu(self.menubar)
self.menuMode.setObjectName("menuMode")
self.menuSensors = QtWidgets.QMenu(self.menubar)
self.menuSensors.setObjectName("menuSensors")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionLIDAR = QtWidgets.QAction(MainWindow)
self.actionLIDAR.setObjectName("actionLIDAR")
self.actionOpen_CV = QtWidgets.QAction(MainWindow)
self.actionOpen_CV.setObjectName("actionOpen_CV")
self.actionManual = QtWidgets.QAction(MainWindow)
self.actionManual.setObjectName("actionManual")
self.actionAuto = QtWidgets.QAction(MainWindow)
self.actionAuto.setObjectName("actionAuto")
self.actionSettings = QtWidgets.QAction(MainWindow)
self.actionSettings.setObjectName("actionSettings")
self.actionCalib = QtWidgets.QAction(MainWindow)
self.actionCalib.setObjectName("actionCalib")
self.actionDisconnect = QtWidgets.QAction(MainWindow)
self.actionDisconnect.setObjectName("actionDisconnect")
self.menuconnect.addAction(self.actionDisconnect)
self.menuDisconnect.addAction(self.actionSettings)
self.menuDisconnect.addAction(self.actionCalib)
self.menuMode.addAction(self.actionManual)
self.menuMode.addAction(self.actionAuto)
self.menuSensors.addAction(self.actionLIDAR)
self.menuSensors.addAction(self.actionOpen_CV)
self.menubar.addAction(self.menuconnect.menuAction())
self.menubar.addAction(self.menuDisconnect.menuAction())
self.menubar.addAction(self.menuMode.menuAction())
self.menubar.addAction(self.menuSensors.menuAction())
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(1)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
self.pushButton_3.setText(_translate("MainWindow", "PushButton"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
self.menuconnect.setTitle(_translate("MainWindow", "connect"))
self.menuDisconnect.setTitle(_translate("MainWindow", "configure"))
self.menuMode.setTitle(_translate("MainWindow", "Mode"))
self.menuSensors.setTitle(_translate("MainWindow", "Sensors"))
self.actionLIDAR.setText(_translate("MainWindow", "LIDAR"))
self.actionOpen_CV.setText(_translate("MainWindow", "Open CV"))
self.actionManual.setText(_translate("MainWindow", "Manual"))
self.actionAuto.setText(_translate("MainWindow", "Auto"))
self.actionSettings.setText(_translate("MainWindow", "Settings"))
self.actionCalib.setText(_translate("MainWindow", "Calib"))
self.actionDisconnect.setText(_translate("MainWindow", "Disconnect"))
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_())
This is my gui code and i want to add the below file in the headswidget and the file name is hudsup.py :
import sys
import math
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
def rec_line_cord(i,list,xc,origin_list,pitch,roll):
.
.
def rec_drawLine(self,i,list,painter,roll,metrics):
.
.
.
def drawMarkings(self, painter):
.
.
.
def drawNeedle(self, painter,roll):
.
.
.
def Compass(self,painter,yaw,r):
.
.
.
class Manager(QObject):
changedValue = pyqtSignal(tuple)
def __init__(self):
QObject.__init__(self)
filename = "Attitude_data_Manual_1232018_1158.txt"
res = self.read_content(filename)
self.results = zip(*res)
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_value)
self.timer.start(100)
#pyqtSlot()
def update_value(self):
try:
self.changedValue.emit(next(self.results))
except StopIteration:
self.timer.stop()
def read_content(self, filename):
.
.
.
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.v = None
#pyqtSlot(tuple)
def update_value(self, v):
self.v = v
self.update()
def paintEvent(self, event):
QWidget.paintEvent(self, event)
painter = QPainter(self)
r = self.rect()
.
.
if __name__ == '__main__':
app = QApplication(sys.argv)
manager = Manager()
w = Widget()
manager.changedValue.connect(w.update_value)
w.show()
sys.exit(app.exec_())
This is my example of python script.
here is hudsup.py output :
animated huds_up display
and my ui looks like this and i want my hudsup to displayed as mentioned :
Gui
You should not use the generated code directly, instead, create a new widget class and inherit from it:
class MainWindow(Ui_MainWindow, QMainWindow):
def __init__(self):
Ui_MainWindow.__init__(self)
QMainWindow.__init__(self)
self.setupUi(self)
To integrate your second widget you can just add it to Heads_up like you are doing now but don't need to call exec_:
self.hud_sup = HudSup()
self.Heads_up.addWidget(self.hud_sup)