PyQt5 issue with QApplication module - python

I have an issue where like many others i can't run some pyqt5 code because i get the error that vscode can't find the module QApplication when i run the command from pyqt5.QtWidgets import QApplication.
Here is my code:
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
ui = uic.loadUi("interface.ui")
ui.show()
def recherche():
if ui.NomProduit.text()!="":
produit = str(ui.NomProduit.text())
else:
ui.NomProduit.setText("Veuillez rentrez le nom d'un produit")
ui.Recherche.clicked.connect(recherche)

I edited from PyQt5 import Uic to from PyQt5.uic import loadUi and I used Opject Oriented Programing (OOP) . And I imported all methods From PyQt5.QtWidgets import *
Try this :
import sys
from PyQt5.uic import loadUi
from PyQt5.QtWidgets import *
app = QApplication(sys.argv)
class Main(QMainWindow):
def __init__(self):
super().__init__()
loadUi("interface.ui", self)
self.show()
self.Buttons()
def Buttons(self):
self.Recherche.clicked.connect(self.recherche)
def recherche(self):
if self.NomProduit.text()!="":
produit = str(self.NomProduit.text())
else:
self.NomProduit.setText("Veuillez rentrez le nom d'un produit")
ui = Main()
app.exec_()

Related

How to use QTest.keySequence?

I'm trying to test if a shortcut is working using PyQt5 and QTest. Here is my code:
Main.py
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QApplication, QPushButton
class Window(QWidget):
def __init__(self):
super().__init__()
self.more_btn = QPushButton("More")
self.more_btn.clicked.connect(self.on_clicked)
self.more_btn.setShortcut(QKeySequence.Undo)
vbox = QVBoxLayout()
vbox.addWidget(self.more_btn)
self.setLayout(vbox)
def on_clicked(self):
print("Item clicked")
test_main.py
import sys
import unittest
from PyQt5.QtGui import QKeySequence
from PyQt5.QtTest import QTest
from PyQt5.QtWidgets import QApplication
from Main import Window
class MainTestCase(unittest.TestCase):
def test_main(self):
app = QApplication(sys.argv)
form = Window()
QTest.keySequence(form, QKeySequence.Undo)
When I run test_main, item_clicked should be printed, but it doesn't. I tried to debug the program and set the breatpoint at the print("Item clicked") line, it didn't stop. It seems that QTest.keySequence didn't work. Why? How should I make it work?

To turn back from 2nd window to 1st window (different files)

An application is being created with a large number of different windows.
The bottom line is that I am trying to get from the main window (by clicking button) to the client window. There is "Back" button in the clients window, which should return the user to the main window.
Both codes are in different files. The problem occurs at the stage of pressing the "Customers" button (Process finished with exit code -1073740791 (0xC0000409)).
Moreover, if everything is in one file, then everything works fine. But only here the program is not planned to be small, so I don't want to put a huge code in one file.
Welcome_Screen.py
import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import QDialog, QApplication
import Customers
class WelcomeScreen(QDialog):
def __init__(self):
super(WelcomeScreen, self).__init__()
loadUi('screens/welcomescreen.ui', self)
self.CustomerData.clicked.connect(self.go_to_CustomerData)
def go_to_CustomerData(self):
client = Customers.CustomerScreen()
widget.addWidget(client)
widget.setCurrentIndex(widget.currentIndex() + 1)
# main
app = QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon('Icons/MainIcon.png'))
app.setApplicationDisplayName('Bonus Program')
welcome = WelcomeScreen()
widget = QtWidgets.QStackedWidget()
widget.addWidget(welcome)
widget.setFixedWidth(600)
widget.setFixedHeight(475)
widget.show()
try:
sys.exit(app.exec_())
except:
print("Exiting")
Customers.py
import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import QDialog, QApplication, QWidget, QMessageBox, QTableWidget, QTableWidgetItem, QTableView
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel, QSqlQuery
from PyQt5.QtGui import QIcon
import Welcome_Screen as ws
import sqlite3
db_path = 'Bonus_db.sqlite'
class CustomerScreen(QDialog):
def __init__(self):
super(CustomerScreen, self).__init__()
loadUi("screens/customerscreen.ui", self)
self.back_btn.setIcon(QIcon('Icons/Back.png'))
self.back_btn.setIconSize(QtCore.QSize(45, 60))
self.back_btn.clicked.connect(self.go_Back_to_WelcomeScreen)
# self.SoldTos.clicked.connect(self.gotoSoldTos)
def go_Back_to_WelcomeScreen(self):
welcome = ws.WelcomeScreen()
ws.widget.addWidget(welcome)
ws.widget.setCurrentIndex(ws.widget.currentIndex() + 1)
Customer_app = QApplication(sys.argv)
cust_window = CustomerScreen()
cust_window.show()
sys.exit(Customer_app.exec_())

QT_DEVICE_PIXEL_RATIO is deprecated

I get the following warning when I run the code below
Warning: QT_DEVICE_PIXEL_RATIO is deprecated. Instead use:
QT_AUTO_SCREEN_SCALE_FACTOR to enable platform plugin controlled per-screen factors.
QT_SCREEN_SCALE_FACTORS to set per-screen DPI.
QT_SCALE_FACTOR to set the application global scale factor.
code:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QDialog, QStackedWidget
from PyQt5.uic import loadUi
class WelcomeScreen(QDialog):
def _init_(self):
super(WelcomeScreen, self)._init_()
loadUi("screen1.ui", self)
# main
app = QApplication(sys.argv)
welcome = WelcomeScreen()
widget = QStackedWidget()
widget.addWidget(welcome)
widget.setFixedHeight(500)
widget.setFixedWidth(800)
widget.show()
# app.exec_()
try:
sys.exit(app.exec())
except:
print("exiting")

how to configure QLineEdit in pyautogui.typewrite

I use Qtdesigner and then convert it to py file..I want to take input text through the QLineEdit and put it to pyautogui.typewrite. facing problem to solve it.
I write the the code bleow
import pyautogui
import time
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QMovie
from PyQt5.QtCore import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUiType
from messageRHR import Ui_MainWindow
class MainThread(QThread):
def __init__(self,message):
super(MainThread,self).__init__()
# self.message = QLineEdit(self)
def run(self):
self.taskExecution()
def taskExecution(self):
pyautogui.typewrite(message)
time.sleep(1)
pyautogui.press('Enter')
startExecution = MainThread()
class Main(QMainWindow):
def __init__(self):
super(Main, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.startTask)
self.ui.pushButton_2.clicked.connect(self.close)
def startTask(self):
self.ui.movie = QtGui.QMovie("00545cb7179c504433d4c8f5e845f286.gif")
self.ui.label_2.setMovie(self.ui.movie)
self.ui.movie.start()
self.ui.movie = QtGui.QMovie("00545cb7179c504433d4c8f5e845f286.gif")
self.ui.label_3.setMovie(self.ui.movie)
self.ui.movie.start()
message = QLineEdit(self)
startExecution.start()
app = QApplication(sys.argv)
rsn = Main()
rsn.show()
exit(app.exec_())

PyQt with Flask dont show the MainWindow

i'm trayin to create a gui that run a flask application using this example https://github.com/onur2677/PyQt-Flask
in this exemple they use pyqt 4
i'm trying to use pyqt 5 so i made some changes
but the mainwindow open and close in the same time
this is my code for the gui
import sys
from PyQt5 import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import psycopg2
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from PyQt5 import *
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QEventLoop
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication
PORT = 5000
ROOT_URL = 'http://localhost:{}'.format(PORT)
class FlaskThread(QThread):
def __init__(self, application):
QThread.__init__(self)
self.application = application
def __del__(self):
self.wait()
def run(self):
self.application.run(port=PORT)
def createGuiFor(application):
qtapp = QApplication(sys.argv)
webapp = FlaskThread(application)
webapp.start()
qtapp.aboutToQuit.connect(webapp.terminate)
webview = QWebEngineView()
webview.load(QUrl(ROOT_URL))
webview.show()
webview.setWindowTitle("MyApp")
webview.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
return qtapp.exec_()
if __name__ == '__main__':
from MyWebApp import app
app = QApplication(sys.argv)
sys.exit(app.exec_())
sys.exit(createGuiFor(app))
and this is the flask application that will run in the gui
from flask import Flask ,render_template
app = Flask(__name__)
#app.route("/")
def hello():
return render_template("index.html")
if __name__ == "__main__":
app.run()
for any one ho want to use flask and pyqt there is an example
here https://github.com/smoqadam/PyFladesk
that work for me perfectly
EDIT:
Here is the same code with PySide6
https://github.com/eliaweiss/PySide6Fladesk.git

Categories

Resources