Python Shell restart after while loop with PyQt5 - python

actually I try to do a displayer of html page. I have some of list of html that I need to display, each page must stay visible while x secondes.
But, after the first page is display, the application crash and the python shell restart in consequence.
On my mind, I would create the window to display the page and close the application because after I will try to display png/jpg, so I need to close app to use Pygame for pictures and re-build the app to display html page after.
My list looking for it :
html page/html page/picture/html page/picture/picture
So I have build a sample code to test in while boucle the displayer :
from PyQt5 import QtWidgets, QtWebEngineWidgets, QtCore
import sys
continuer = True
while continuer:
print("Application created")
# Create application
app = QtWidgets.QApplication(sys.argv)
# Add window
win = QtWidgets.QWidget()
win.setWindowTitle('My first rendering')
# Add layout
layout = QtWidgets.QVBoxLayout()
win.setLayout(layout)
# Create QWebView
view = QtWebEngineWidgets.QWebEngineView()
view.setUrl(QtCore.QUrl('https://google.com'))
# Add QWebView to the layout
layout.addWidget(view)
# Show window, run app
win.show()
QtCore.QTimer.singleShot(7*1000, win.close)
QtCore.QTimer.singleShot(7*1000, app.quit)
print("View displayed")
# While loop
app.exec_()
print('Close Application')
print("End While Loop")
Result after executing
It's probably sys.argv in app var the mistake, but I'm new in Python so I don't know how to fix the problem.

The problem is that QApplication is not necessarily eliminated and therefore you would be creating more than one QApplication what Qt forbids, a better solution is to rethink it verifying that if it does not exist then create a new one:
import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
continuer = True
while continuer:
print("Application created")
# Create application
app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)
# Add window
win = QtWidgets.QWidget()
win.setWindowTitle("My first rendering")
# Create QWebEngineView
view = QtWebEngineWidgets.QWebEngineView()
view.setUrl(QtCore.QUrl("https://google.com"))
# Add layout
layout = QtWidgets.QVBoxLayout(win)
win.setLayout(layout)
# Add QWebView to the layout
layout.addWidget(view)
# Show window, run app
win.show()
QtCore.QTimer.singleShot(7 * 1000, win.close)
QtCore.QTimer.singleShot(7 * 1000, app.quit)
print("View displayed")
# While loop
app.exec_()
print("Close Application")
print("End While Loop")

Related

connecting toolbar icon to move stacked pages pyqt

I trying to connect my pyqt6 GUI app to move between stacked widget pages based on the icons you press in the toolbar. I saw that if i use the triggered method it suppose to work but for some reason when I run my python code, I always see the app on page 2 and not my default page 1 and the icons are not triggering movement to a different page. I checked the heirarchy of the elements that I built un the GUI in QT Designer and made sure there are two separate pages, you can see in the image below:
This is the code im running currently:
import sys
from PyQt6.QtWidgets import QApplication
from PyQt6.QtWidgets import QMainWindow
from app_try import Ui_MainWindow
class MainWindow:
def __init__(self):
self.main_win = QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.main_win)
def show_page(self, page):
print("showPage called with page", page)
self.ui.stackedWidget.setCurrentWidget(page)
self.ui.stackedWidget.setCurrentWidget(self.ui.page)
self.ui.action_send_info.triggered(lambda: self.show_page(self.ui.page))
print("Connected action_send_info to showPage")
self.ui.action_data_table.triggered(lambda: self.show_page(self.ui.page_2))
print("Connected action_data_table to showPage")
def show(self):
self.main_win.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
main_win = MainWindow()
main_win.show()
sys.exit(app.exec_())
app_try is the name of my python file which I import in order to get all the elemnts of the GUI. Am I missing something that i need to add in order for the icons to move between the page? thank you for any help!

How to jump from 1 qt window to other in pyqt5.?

I am working on pyqt5 project where I have login window and registration window. I have a button on login window which when clicked should open the registration window. I have designed the ui in qt desginer and have converted the .ui files to .py files using pyuic.
So I have two files login_ui.py and register_window_ui.py. To use them, I have also created two separate files i.e. login.py and register.py which contains all the functional code of the ui files.
Below is the files code:
login.py
import sys
import os
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from ui.login_ui import Ui_login_main_window
from ui.register_window_ui import Ui_register_window
curr_path = os.path.dirname(os.path.abspath(__file__))
class Login(QMainWindow, Ui_login_main_window):
def __init__(self):
QMainWindow.__init__(self)
self.login_ui = Ui_login_main_window()
self.login_ui.setupUi(self)
self.login_ui.register_settings_btn.clicked.connect(self.show_registration_window)
def show_registration_window(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_register_window()
self.ui.setupUi(self.window)
self.window.show()
app = QApplication(sys.argv)
main_window = Login()
main_window.show()
sys.exit(app.exec_())
register.py
import sys
import os
from PyQt5.QtWidgets import QApplication, QMainWindow
from ui.register_window_ui import Ui_register_window
curr_path = os.path.dirname(os.path.abspath(__file__))
class Register(QMainWindow, Ui_register_window):
def __init__(self):
QMainWindow.__init__(self)
self.register_win_ui = Ui_register_window()
self.register_win_ui.setupUi(self)
self.register_win_ui.register_trial_btn.clicked.connect(self.print_data)
def print_data(self):
print("Clicked")
app = QApplication(sys.argv)
main_window = Register()
main_window.show()
sys.exit(app.exec_())
As you can see that in login.py I have created a button click event for register_settings_btn which when clicked will show Ui_register_window() which is register_window. So when I click it, it shows me the window but the window is not functional i.e. there is no button click event happening. For ex in register.py I have created a button click event which is not working.
Can anyone please explain me why is it not working. Please help. Thanks
I have a button on login window which when clicked should open the registration window
instead of creating 2 QApplications create only 1 and show or exec 1st the login, validate the input and if everything is ok, then show the Register
i.e. instead do something like:
app = QApplication(sys.argv)
login_window = Login()
result = login_window.exec()
if result == ???:
register_window = Register()
register_window.show()
else:
showMsg('Login failed!')
sys.exit(app.exec_())
update! create another file i.e. myProject.py and add this!
#!/usr/local/bin/python
# coding: utf-8
import os, sys
add your imports here!
if __name__ == "__main__":
app = QApplication(sys.argv)
#create login view nd register view
login_window = Login()
register_window = Register()
#execute the view login and read the doc, this will block the code until the user quit the view!
result = login_window.exec()
#define a condition to proof the validity of login process
if result == ???:
#show the register
register_window.show()
else:
#or show the error msg
showMsg('Login failed!')
sys.exit(app.exec_())

PyQt5 don't update TextEdit and Label Text [duplicate]

I'm newbie.
I want to click a pushButton to open a new window and take text from main window lineEdit and copy to new pop-up window lineEdit.
So far I an create new window but can't access lineEdit. No errors, app is not responding.
This is what I have:
from PyQt5.QtWidgets import QApplication
from PyQt5 import uic
app = QApplication([]) #Main Window
ui = uic.loadUi(r"D:\UI_test\gui\main_gui_TT.ui")
appedit = QApplication([]) #Pop-up
uiedit = uic.loadUi(r"D:\UI_test\gui\input_TT.ui")
def edit1():
uiedit.show()
appedit.exec_()
uiedit.lineEdit_CC.setText('text') <-this line is a problem
ui.pushButton_1edit.pressed.connect(edit1)
ui.show()
app.exec_()
Please help what is wrong here?
You should only have a single QApplication even if you have many windows, considering the above the solution is:
from PyQt5.QtWidgets import QApplication
from PyQt5 import uic
app = QApplication([]) # Main Window
ui = uic.loadUi(r"D:\UI_test\gui\main_gui_TT.ui")
uiedit = uic.loadUi(r"D:\UI_test\gui\input_TT.ui")
def edit1():
uiedit.show()
uiedit.lineEdit_CC.setText("text")
ui.pushButton_1edit.pressed.connect(edit1)
ui.show()
app.exec_()

Use pyqt4 to create GUI that runs python script

I have written a python script that manipulates data and then saves it into an excel file with the following function:
def saveData():
table = pd.DataFrame(data)
writer = pd.ExcelWriter('manipulatedData.xlsx')
table.to_excel(writer, 'sheet 1')
writer.save()
I also have the following super basic/minimalistic gui window:
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.setGeometry(500,500,500,500)
window.setWindowTitle("PyQt Test!")
window.setWindowIcon(QtGui.QIcon('pythonLogoSmall.png'))
window.show()
sys.exit(app.exec_())
What I really would like to know is this:
1) How can I let the user of my gui select the location + name for the manipulatedData file?
2) How can I add a "run" button, that starts my python script and manipulates the data, before it saves the manipulated data to the desired location.
PyQt4 has QLineEdit() and QPushButton()
from PyQt4 import QtGui
import sys
# --- functions ---
def my_function(event=None):
print 'Button clicked: event:', event
print linetext.text()
# run your code
# --- main ---
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
# add "layout manager"
vbox = QtGui.QVBoxLayout()
window.setLayout(vbox)
# add place for text
linetext = QtGui.QLineEdit(window)
vbox.addWidget(linetext)
# add button
button = QtGui.QPushButton("Run", window)
vbox.addWidget(button)
# add function to button
button.clicked.connect(my_function)
window.show()
sys.exit(app.exec_())

PySide how to get QWebInspector same window

I just started dwelling into the realm of Qt (coming from PyGTK) and I'm using PySide. So I found this great example on another answer here on stack exchange.
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.settings().setAttribute(
QWebSettings.WebAttribute.DeveloperExtrasEnabled, True)
# or globally:
# QWebSettings.globalSettings().setAttribute(
# QWebSettings.WebAttribute.DeveloperExtrasEnabled, True)
web.load(QUrl("http://www.google.com"))
web.show()
inspect = QWebInspector()
inspect.setPage(web.page())
inspect.show()
sys.exit(app.exec_())
My question is as follows, how do I make the inspector show up in the same window instead of a new one? I understand I need to add the QWebInspector to another widget inside the main window (a vbox for example), what I want to know is how to connect that event to the signal the context menu "Inspect" triggers. In PyGTK I would need to use .connect() but I can't find the right SIGNAL for this specific action.
Thanks for your time guys/gals
It shouldn't be necessary to do anything special for the context menu to work. Just add an inspector widget to your layout, and hide() it to start with. The default context menu action can then show() the inspector as needed.
A slightly trickier issue is how to hide the inspector again once it's shown, as there doesn't seem to be a corresponding context menu item for that.
The demo script below simply creates a keyboard shortcut to hide/show the inspector:
from PySide import QtGui, QtCore, QtWebKit
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.view = QtWebKit.QWebView(self)
self.view.settings().setAttribute(
QtWebKit.QWebSettings.WebAttribute.DeveloperExtrasEnabled, True)
self.inspector = QtWebKit.QWebInspector(self)
self.inspector.setPage(self.view.page())
self.inspector.hide()
self.splitter = QtGui.QSplitter(self)
self.splitter.addWidget(self.view)
self.splitter.addWidget(self.inspector)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.splitter)
QtGui.QShortcut(QtGui.QKeySequence('F7'), self,
self.handleShowInspector)
def handleShowInspector(self):
self.inspector.setShown(self.inspector.isHidden())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.view.load(QtCore.QUrl('http://www.google.com'))
window.show()
sys.exit(app.exec_())

Categories