I have use QtDesigner to turn a picture into a .qrc file, Then I use pyrcc5 to turn this .qrc file into a .py resource:
pyrcc5 source.qrc -o source.py
then I import this source.py file in my .py file. I removed _rc so it become like this
import source
now when I run the .py file itself the picture shows up. But when I run it from a MainPage the picture does not show.
This is my main page:
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi
class MainPage(QDialog):
def __init__(self):
super(MainPage,self).__init__()
loadUi('king.ui',self)
app= QApplication(sys.argv)
widget = MainPage()
widget.show()
sys.exit(app.exec_())
So any solution for this ?
Related
In my project I created GUI using Qt Designer. It has few input fields and button that read from those fields and create PDF file. It working fine, but I want to select path through window "Select path" (like on picture).
https://4d.developpez.com/documentation/4DDocV11/IMG/U0067001.PNG
How I can do that? I have been thinkig about those variants:
GUI have special button which open "Select path" window, and pass path to pdf creation function;
"Select path" open automaticaly when I press "Create PDF" button.
Main.py of my project:
import sys
import numpy as np
import window_8
import help_window_dialog
from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow, QMessageBox
from PyQt5.uic import loadUi
from tables2 import pdf_create
from Verification_1 import verification
from window_8 import Ui_MainWindow
from help_window_dialog import Ui_Help_w
class Window(QMainWindow, Ui_MainWindow, Ui_Help_w):
def __init__(self):
super(Window, self).__init__()
self.ui = window_8.Ui_MainWindow()
self.ui.setupUi(self)
self.ui.Calc_Button_level_N.clicked.connect(self.Level_N_cal)
self.ui.setNumbers_N.clicked.connect(self.Fill_N)
self.ui.Calc_Button_level_L.clicked.connect(self.Level_L_cal)
self.ui.setNumbers_L.clicked.connect(self.Fill_L)
self.ui.Calc_Button_level_H.clicked.connect(self.Level_H_cal)
self.ui.setNumbers_H.clicked.connect(self.Fill_H)
self.ui.Prin_out_res_N.clicked.connect(self.Out_Res_N)
self.ui.Help_Button_N.clicked.connect(self.open_window)
[some other functions and operations]
I'm trying to load a logo into a python scripts UI. I'm using Qt Designer and I created a label and set pixmap to the image. The image loads fine in the designer but when I import the ui file into the python script I get this error message
C:\Users\Mason\AppData\Local\Continuum\anaconda3\python.exe "C:/Users/Mason/PycharmProjects/Inspector/Tester/main.py"
Traceback (most recent call last):
File "C:/Users/Mason/PycharmProjects/Inspector/Tester/main.py", line 9, in <module>
UIClass, QtBaseClass = uic.loadUiType("ui5.ui")
File "C:\Users\Mason\AppData\Local\Continuum\anaconda3\lib\site-packages\PyQt5\uic\__init__.py", line 201, in loadUiType
exec(code_string.getvalue(), ui_globals)
File "<string>", line 30
import 3_rc
I still get that error even if I take the image out of the ui file and reload it. What am I doing wrong?
from PyQt5 import QtCore, uic, QtWidgets
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi
from PyQt5.QtGui import QIcon, QPixmap
import sys
UIClass, QtBaseClass = uic.loadUiType("ui5.ui")
class MyApp(UIClass, QtBaseClass):
def __init__(self):
UIClass.__init__(self)
QtBaseClass.__init__(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.on_pushbutton_click)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
Qt relies on a resource system for images and other assets. When you add an image in qt designer, it creates a resource file. You have to convert that resource file, just like with a ui file (uic is a shortcut to this process). The autogenerated ui file you get with uic is looking for that resource file, but can't find it.
You can convert your resource file to python with this, depending on your qt version:
pyrcc5 -o 3_rc.py your_resource_file_here.qrc
More info:
Related question: Importing resource file to PyQt code?
https://www.riverbankcomputing.com/static/Docs/PyQt4/resources.html
https://www.riverbankcomputing.com/static/Docs/PyQt5/resources.html
I'm begining with Python. I'm developing a simple desktop app to print a control check of motorcycles services. The problem is that the UI not showing the unchecked checkboxes in the table when I run the script
Screenshot with errors in red
Screenshot with cells configuration in Qt5 Designer
The code
import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi
class ServiceLabeler(QDialog):
def __init__(self):
super(ServiceLabeler, self).__init__()
loadUi('main.ui', self)
self.btnPrint.clicked.connect(self.printLabel)
#pyqtSlot()
def printLabel(self):
print('Printing ...')
app = QApplication(sys.argv)
widget = ServiceLabeler()
widget.show()
sys.exit(app.exec_())
If you want to see the main.ui code please follow next link to gist
https://gist.github.com/CristalT/0d2e5cc2c684c6dc2b87bd5ec1d7348e
For now i have two scripts one for file dialog and one of directory dialog. I want to have same option in one scripts in pyqt5. How can i do that?
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon
def hello():
app=QApplication(sys.argv)
w=QWidget()
w.resize(1100,800)
b=QPushButton(w)
b.move(10,30)
b.clicked.connect(on_click_me)
b.setText("Add_Files")
w.show()
sys.exit(app.exec_())
def on_click_me():
dialog=QFileDialog()
#dialog.setFileMode(QFileDialog.Directory);
#dialog.setFileMode(QFileDialog.AnyFile);
dialog.exec_()
#print(fileName)
hello()
Like this code will allow me to select either files or directory depending upon QFileDialog.Directory or QFileDialog.AnyFile method. Can't i can create something so i can open whatever i want directory or file at the selection time.
I'm trying to display a PNG image in a Widget using QLabel. I built the Widget using Qt Designer and the label shows the image properly. When I load the .ui into mi .py and execute it, everything shows up well, except for the label. I tried loading the QPixmap manually on the python code, but it didn't work either.
from PyQt5.QtWidgets import (QApplication, QLabel)
from PyQt5.QtGui import (QPixmap)
from PyQt5 import uic
import sys
import os
form = uic.loadUiType('gui/Files/Login.ui')
class LoginWindow(form[0], form[1]):
def __init__(self):
super().__init__()
self.setupUi(self)
self.logoLabel.setPixmap(QPixmap(os.path.abspath(os.path.join(os.path.dirname(__file__), 'logo.png'))))
self.logoLabel.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = LoginWindow()
win.show()
app.exec_()
Any idea what am I doing wrong? Any sugestions on how could I display the image other than with QLabel?