I am trying to load the homescreen after the splashscreen - python

I am trying to load my homescreen after the splashscreen. Upon running the code, the splashscreen manages to load but it stops there. How do I fix this?
This is my SplashScreen class
class SplashScreen(QWidget):
def __init__(self):
super().__init__()
self.setFixedSize(700, 350)
self.setWindowFlag(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.counter = 0
self.n = 100
self.initUI()
self.timer = QTimer()
self.timer.timeout.connect(self.loading)
self.timer.start(30)
def initUI(self):
# layout to display splash scrren frame
layout = QVBoxLayout()
self.setLayout(layout)
# splash screen frame
self.frame = QFrame()
layout.addWidget(self.frame)
# splash screen title
self.title_label = QLabel(self.frame)
self.title_label.setObjectName('title_label')
self.title_label.resize(690, 120)
self.title_label.move(0, 5) # x, y
self.title_label.setText('Splash Screen')
self.title_label.setAlignment(Qt.AlignCenter)
# splash screen title description
self.description_label = QLabel(self.frame)
self.description_label.resize(690, 40)
self.description_label.move(0, self.title_label.height())
self.description_label.setObjectName('desc_label')
self.description_label.setText('<b>Splash Screen PyQt-5</b>')
self.description_label.setAlignment(Qt.AlignCenter)
# splash screen pogressbar
self.progressBar = QProgressBar(self.frame)
self.progressBar.resize(self.width() - 200 - 10, 50)
self.progressBar.move(100, 180) # self.description_label.y()+130
self.progressBar.setAlignment(Qt.AlignCenter)
self.progressBar.setFormat('%p%')
self.progressBar.setTextVisible(True)
self.progressBar.setRange(0, self.n)
self.progressBar.setValue(20)
# spash screen loading label
self.loading_label = QLabel(self.frame)
self.loading_label.resize(self.width() - 10, 50)
self.loading_label.move(0, self.progressBar.y() + 70)
self.loading_label.setObjectName('loading_label')
self.loading_label.setAlignment(Qt.AlignCenter)
self.loading_label.setText('Loading...')
This is the function that lets the splashscreen load and launch the WindowApp class but it does not work. What I mean by does not work I meant that it does not load the WindowApp. It just closes.
def loading(self):
# set progressbar value
self.progressBar.setValue(self.counter)
# stop progress if counter
# is greater than n and
# display main window app
if self.counter >= self.n:
self.timer.stop()
self.close()
MainWindow = QtWidgets.QMainWindow()
ui = WindowApp()
ui.setupUi(MainWindow)
MainWindow.show()
self.counter += 1
This is my main window
class WindowApp(QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1006, 654)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 10, 981, 641))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(10)
self.label.setFont(font)
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("../pygui/Homescreen.png"))
self.label.setScaledContents(True)
self.label.setWordWrap(False)
self.label.setObjectName("label")
self.infoBtn = QtWidgets.QPushButton(self.centralwidget)
self.infoBtn.setGeometry(QtCore.QRect(120, 550, 181, 41))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(15)
self.infoBtn.setFont(font)
self.infoBtn.setStyleSheet("border-radius: 10px;\n"
"border-color: 2px solid gray;\n"
"background-color: rgb(255, 255, 255);")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("icons/info_icon.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.infoBtn.setIcon(icon)
self.infoBtn.setObjectName("infoBtn")
self.startBtn = QtWidgets.QPushButton(self.centralwidget)
self.startBtn.setGeometry(QtCore.QRect(310, 270, 431, 111))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(28)
self.startBtn.setFont(font)
self.startBtn.setStyleSheet("background-color: #305E6C;\n"
"color: white;\n"
"border-radius: 10px;\n"
"border: 2px solid white")
self.startBtn.setObjectName("startBtn")
self.contactsBtn = QtWidgets.QPushButton(self.centralwidget)
self.contactsBtn.setGeometry(QtCore.QRect(720, 30, 101, 101))
self.contactsBtn.setStyleSheet("\n"
"background-color: #121212;\n"
"border-radius: 50%;\n"
"border: 2px solid white;")
self.contactsBtn.setText("")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("icons/typcn_contacts.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.contactsBtn.setIcon(icon1)
self.contactsBtn.setIconSize(QtCore.QSize(40, 40))
self.contactsBtn.setObjectName("contactsBtn")
self.dateBtn = QtWidgets.QPushButton(self.centralwidget)
self.dateBtn.setGeometry(QtCore.QRect(840, 30, 101, 101))
self.dateBtn.setStyleSheet("\n"
"background-color: #121212;\n"
"border-radius: 50%;\n"
"border: 2px solid white;")
self.dateBtn.setText("")
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("icons/Group.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.dateBtn.setIcon(icon2)
self.dateBtn.setIconSize(QtCore.QSize(40, 40))
self.dateBtn.setObjectName("dateBtn")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1006, 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)
self.infoBtn.clicked.connect(partial(self.show_popup))
self.dateBtn.clicked.connect(self.openDateWindow)
self.startBtn.clicked.connect(self.openDriveWindow)
self.contactsBtn.clicked.connect(self.openContactWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.infoBtn.setText(_translate("MainWindow", "About"))
self.startBtn.setText(_translate("MainWindow", "Driving Mode"))
def show_popup(self):
msg = QMessageBox()
msg.setWindowTitle('About Us')
msg.setText('Testing')
msg.setIcon(QMessageBox.Information)
x = msg.exec_()
def openDateWindow(self, MainWindow):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_DateWindow()
self.ui.setupUi(self.window, MainWindow)
self.window.show()
MainWindow.hide()
def openDriveWindow(self, MainWindow):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_DriveWindow()
self.ui.setupUi(self.window, MainWindow)
self.window.show()
MainWindow.hide()
def openContactWindow(self, MainWindow):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_ContactWindow()
self.ui.setupUi(self.window, MainWindow)
self.window.show()
MainWindow.hide()
I have tried changing this to WindowApp() just to check if the MainWindow has any problems but it manages to load without the splashscreen.
app = QApplication(sys.argv)
splash = SplashScreen()
splash.show()
sys.exit(app.exec_())

Check my answer from this previous post that I think will help you.
You can show the splash screen and when the splash screen closes then you can show your main window. It's similar to the login window from this previous question
But if you want to keep implementing your code the way you have it I think I know the problem.
You call a method which creates the QMainWindow and calls the .show() for that QMainWindow. Once the method is done the variables in that method are garbage collected and so is your QMainWindow object. The way to keep the reference and prevent those variables from getting garbage collected is by making it a class variable by adding self. in front of the variable.
I would maybe restructure a bit and do something like this:
if __name__ == "__main__":
app = QApplication(sys.argv)
splash = SplashScreen()
splash_closed = splash.exec_()
if splash_closed:
window = WindowApp()
window.show()
sys.exit(app.exec_())
Where the splashscreen is now a QDialog and when you're done loading you close the dialog with self.accept() instead of trying to show a new window:
class SplashScreen(QDialog):
def __init__(self):
super().__init__()
self.setFixedSize(700, 350)
self.setWindowFlag(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.counter = 0
self.n = 100
self.initUI()
self.timer = QTimer()
self.timer.timeout.connect(self.loading)
self.timer.start(30)
def initUI(self):
...
def loading(self):
# set progressbar value
self.progressBar.setValue(self.counter)
# stop progress if counter
# is greater than n and
# display main window app
if self.counter >= self.n:
self.timer.stop()
self.accept()
self.counter += 1
And you should also use the __init__ method on your main window and maybe move all the self.setupUI code in there, or just do something like this:
class WindowApp(QMainWindow):
def __init__(self):
QtWidgets.QDialog.__init__(self)
self.setupUi(self)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1006, 654)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")

Hope this helps
import sys
from PyQt5.QtWidgets import QMainWindow, QDialog, QApplication
from PyQt5.QtWidgets import QPushButton, QVBoxLayout, QFrame, QLabel, QProgressBar
from PyQt5.QtWidgets import QWidget
from PyQt5 import QtCore, QtGui
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt, QTimer
class SplashScreen(QWidget):
def __init__(self):
super().__init__()
self.setFixedSize(700, 350)
self.setWindowFlag(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.counter = 0
self.n = 100
self.initUI()
self.timer = QTimer()
self.timer.timeout.connect(self.loading)
self.timer.start(30)
def initUI(self):
# layout to display splash scrren frame
layout = QVBoxLayout()
self.setLayout(layout)
# splash screen frame
self.frame = QFrame()
layout.addWidget(self.frame)
# splash screen title
self.title_label = QLabel(self.frame)
self.title_label.setObjectName('title_label')
self.title_label.resize(690, 120)
self.title_label.move(0, 5) # x, y
self.title_label.setText('Splash Screen')
self.title_label.setAlignment(Qt.AlignCenter)
# splash screen title description
self.description_label = QLabel(self.frame)
self.description_label.resize(690, 40)
self.description_label.move(0, self.title_label.height())
self.description_label.setObjectName('desc_label')
self.description_label.setText('<b>Splash Screen PyQt-5</b>')
self.description_label.setAlignment(Qt.AlignCenter)
# splash screen pogressbar
self.progressBar = QProgressBar(self.frame)
self.progressBar.resize(self.width() - 200 - 10, 50)
self.progressBar.move(100, 180) # self.description_label.y()+130
self.progressBar.setAlignment(Qt.AlignCenter)
self.progressBar.setFormat('%p%')
self.progressBar.setTextVisible(True)
self.progressBar.setRange(0, self.n)
self.progressBar.setValue(20)
# spash screen loading label
self.loading_label = QLabel(self.frame)
self.loading_label.resize(self.width() - 10, 50)
self.loading_label.move(0, self.progressBar.y() + 70)
self.loading_label.setObjectName('loading_label')
self.loading_label.setAlignment(Qt.AlignCenter)
self.loading_label.setText('Loading...')
self.show()
def loading(self):
# set progressbar value
self.progressBar.setValue(self.counter)
# stop progress if counter
# is greater than n and
# display main window app
if self.counter >= self.n:
self.timer.stop()
'''
self.close()
MainWindow = QtWidgets.QMainWindow()
ui = WindowApp()
ui.setupUi(MainWindow)
MainWindow.show()
'''
self.ui = WindowApp()
self.ui.show()
self.close()
self.counter += 1
class WindowApp(QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1006, 654)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 10, 981, 641))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(10)
self.label.setFont(font)
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("../pygui/Homescreen.png"))
self.label.setScaledContents(True)
self.label.setWordWrap(False)
self.label.setObjectName("label")
self.infoBtn = QtWidgets.QPushButton(self.centralwidget)
self.infoBtn.setGeometry(QtCore.QRect(120, 550, 181, 41))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(15)
self.infoBtn.setFont(font)
self.infoBtn.setStyleSheet("border-radius: 10px;\n"
"border-color: 2px solid gray;\n"
"background-color: rgb(255, 255, 255);")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("icons/info_icon.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.infoBtn.setIcon(icon)
self.infoBtn.setObjectName("infoBtn")
self.startBtn = QtWidgets.QPushButton(self.centralwidget)
self.startBtn.setGeometry(QtCore.QRect(310, 270, 431, 111))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(28)
self.startBtn.setFont(font)
self.startBtn.setStyleSheet("background-color: #305E6C;\n"
"color: white;\n"
"border-radius: 10px;\n"
"border: 2px solid white")
self.startBtn.setObjectName("startBtn")
self.contactsBtn = QtWidgets.QPushButton(self.centralwidget)
self.contactsBtn.setGeometry(QtCore.QRect(720, 30, 101, 101))
self.contactsBtn.setStyleSheet("\n"
"background-color: #121212;\n"
"border-radius: 50%;\n"
"border: 2px solid white;")
self.contactsBtn.setText("")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("icons/typcn_contacts.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.contactsBtn.setIcon(icon1)
self.contactsBtn.setIconSize(QtCore.QSize(40, 40))
self.contactsBtn.setObjectName("contactsBtn")
self.dateBtn = QtWidgets.QPushButton(self.centralwidget)
self.dateBtn.setGeometry(QtCore.QRect(840, 30, 101, 101))
self.dateBtn.setStyleSheet("\n"
"background-color: #121212;\n"
"border-radius: 50%;\n"
"border: 2px solid white;")
self.dateBtn.setText("")
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("icons/Group.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.dateBtn.setIcon(icon2)
self.dateBtn.setIconSize(QtCore.QSize(40, 40))
self.dateBtn.setObjectName("dateBtn")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1006, 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)
self.infoBtn.clicked.connect(partial(self.show_popup))
self.dateBtn.clicked.connect(self.openDateWindow)
self.startBtn.clicked.connect(self.openDriveWindow)
self.contactsBtn.clicked.connect(self.openContactWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.infoBtn.setText(_translate("MainWindow", "About"))
self.startBtn.setText(_translate("MainWindow", "Driving Mode"))
def show_popup(self):
msg = QMessageBox()
msg.setWindowTitle('About Us')
msg.setText('Testing')
msg.setIcon(QMessageBox.Information)
x = msg.exec_()
def openDateWindow(self, MainWindow):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_DateWindow()
self.ui.setupUi(self.window, MainWindow)
self.window.show()
MainWindow.hide()
def openDriveWindow(self, MainWindow):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_DriveWindow()
self.ui.setupUi(self.window, MainWindow)
self.window.show()
MainWindow.hide()
def openContactWindow(self, MainWindow):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_ContactWindow()
self.ui.setupUi(self.window, MainWindow)
self.window.show()
MainWindow.hide()
app = QApplication(sys.argv)
splash = SplashScreen()
#splash.show()
sys.exit(app.exec_())

Related

How to get Icon size to completely fill PushButton/be rescalable in PyQt5 python

I have a class called TwoStateButton which inherits QPushButton so that the icon can change when the button is being clicked on and so the icon gets resized in proportion to the button when the whole window gets resized.
When I run this code, the button takes up the whole window and the icon is about 10% of the button. I'm trying to get it so that the icon image takes up the entire pushbutton and that they both get resized by the same amount when the window is resized.
What modifications to my code should I do to achieve this?
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class TwoStateButton(QtWidgets.QPushButton):
def __init__(self, on, hover, parent=None):
super(TwoStateButton, self).__init__(parent)
self.pad = 4 # padding between the icon and the button frame
self.minSize = 8 # minimum size of the icon
sizePolicy = QtWidgets.QSizePolicy(
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding
)
self.setSizePolicy(sizePolicy)
self.setIcon(QtGui.QIcon(on))
self.on = on
self.hover = hover
def mousePressEvent(self, event):
super(TwoStateButton, self).mousePressEvent(event)
self.setIcon(QtGui.QIcon(self.hover))
def mouseReleaseEvent(self, event):
super(TwoStateButton, self).mouseReleaseEvent(event)
self.setIcon(QtGui.QIcon("%s" % (self.on)))
def paintEvent(self, event):
qp = QtGui.QPainter(self)
opt = QtWidgets.QStyleOptionButton()
self.initStyleOption(opt)
r = opt.rect
h = r.height()
w = r.width()
iconSize = max(min(h, w) - 2 * self.pad, self.minSize)
opt.iconSize = QtCore.QSize(iconSize, iconSize)
self.style().drawControl(QtWidgets.QStyle.CE_PushButton, opt, qp,self)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1090, 681)
MainWindow.setStyleSheet("")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton_2 = TwoStateButton(
"images/Button5-3.png", "images/Button5-4.png", self.centralwidget
)
self.pushButton_2.setGeometry(QtCore.QRect(430, 310, 141, 81))
self.pushButton_2.setText("")
self.pushButton_2.setCheckable(False)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1090, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setStyleSheet(
"border-image: url(:/images/Button5-3.png)"
)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
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_())

how to detect a key release in pyqt

I want to create a PyQt5 window (Windows OS) which recognizes a button click with holding CTRL button. I successfully created a handler which recognizes CTRL key press but it couldn't find the pressing and releasing of a button which i need to call and dismiss button click event. I did a lot of search but the resources for PyQt5 seems pretty low. Any help is appreciated :)
import time
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)
self.show()
self.signals()
self.bleahOK=True
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(392, 255)
self.unlockButton = QtWidgets.QPushButton(Dialog)
self.unlockButton.setGeometry(QtCore.QRect(10, 180, 171, 51))
self.unlockButton.setObjectName("unlockButton")
self.lockButton = QtWidgets.QPushButton(Dialog)
self.lockButton.setGeometry(QtCore.QRect(220, 180, 151, 51))
self.lockButton.setObjectName("lockButton")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(30, 30, 331, 71))
font = QtGui.QFont()
font.setPointSize(12)
self.label.setFont(font)
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(30, 120, 261, 31))
font = QtGui.QFont()
font.setPointSize(18)
self.lineEdit.setFont(font)
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.unlockButton.setText(_translate("Dialog", "OK"))
self.lockButton.setText(_translate("Dialog", "Lock"))
self.label.setText(_translate("Dialog", ""))
self.lineEdit.setText(_translate("Dialog", ""))
def signals(self):
self.unlockButton.clicked.connect(self.unlock)
def unlock(self):
if 1: print('ff')
def keyPressEvent(self, event):
if type(event) == QtGui.QKeyEvent: #Unable to find when the key was released
print (event.key())
event.accept()
else:
event.ignore()
def lock(self):
print("Test")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
# ui.signals()
# Dialog.show()
sys.exit(app.exec_())
I recommend you not to modify the code generated by Qt Designer, instead create a class that inherits the appropriate widget and use that class as an interface as I recommend PyQt. Going to the problem, you have to use the keyReleaseEvent method to listen when a key is released:
import time
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(392, 255)
self.unlockButton = QtWidgets.QPushButton(Dialog)
self.unlockButton.setGeometry(QtCore.QRect(10, 180, 171, 51))
self.unlockButton.setObjectName("unlockButton")
self.lockButton = QtWidgets.QPushButton(Dialog)
self.lockButton.setGeometry(QtCore.QRect(220, 180, 151, 51))
self.lockButton.setObjectName("lockButton")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(30, 30, 331, 71))
font = QtGui.QFont()
font.setPointSize(12)
self.label.setFont(font)
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(30, 120, 261, 31))
font = QtGui.QFont()
font.setPointSize(18)
self.lineEdit.setFont(font)
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.unlockButton.setText(_translate("Dialog", "OK"))
self.lockButton.setText(_translate("Dialog", "Lock"))
self.label.setText(_translate("Dialog", ""))
self.lineEdit.setText(_translate("Dialog", ""))
class Dialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.is_key_ctrl_pressed = False
self.unlockButton.clicked.connect(self.unlock)
#QtCore.pyqtSlot()
def unlock(self):
if self.is_key_ctrl_pressed:
print("unlock")
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Control:
self.is_key_ctrl_pressed = True
super(Dialog, self).keyPressEvent(event)
def keyReleaseEvent(self, event):
if event.key() == QtCore.Qt.Key_Control:
self.is_key_ctrl_pressed = False
super(Dialog, self).keyReleaseEvent(event)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Dialog()
w.show()
sys.exit(app.exec_())

PyQt: How to refresh label on one window with push button on another one?

Hello dear Users of stackoverflow
Introduction:
I am kind of new to Python and want to build a touch GUI for my Raspberry Pi with PyQt5. Therefore, I use the QtDesigner to build up .ui files on Windows 7. After that, the files are translated to .py files using "pyuic5 -x file.ui -o file.py" in the LXTerminal of the Pi.
My GUI:
I need to build up one output window (MainWindow) with a label and a push button, which opens up another window (I chose Dialog) for input. The input window has a spin box to set the value and a horizontal slider for bigger value steps. At the bottom of the window is a push button, which sets the spin box value as global variable and closes the Input window again.
The problem:
I want the push button of the input window that closes this window to also refresh the output label of the MainWindow, so that it shows the new value.
Pictures:
I am not allowed to embed Pictures of my GUI yet, so please see the following links.
MainWindow
InputWindow
InputWindow with Connections between slider and spin box
My Code:
The following code is a simple example and has everything working except the refresh of the Label. Please help me to get this work, even if it might be very simple for advanced and professional developers. I spent days on trying and googling for this and got lots of more complicated things working.
Best wishes,
RaspiManu
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
value = 0
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(890, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(180, 100, 500, 250))
font = QtGui.QFont()
font.setPointSize(20)
self.label.setFont(font)
self.label.setStyleSheet("background-color: rgb(255, 255, 255);")
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(180, 370, 500, 100))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
############
self.pushButton.clicked.connect(self.OpenInput)
############
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 retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Value"))
self.pushButton.setText(_translate("MainWindow", "Go to input window"))
##############################
# Show second window for input
def OpenInput(self, MainWindow):
Dialog.show()
##############################
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(889, 598)
self.spinBox = QtWidgets.QSpinBox(Dialog)
self.spinBox.setGeometry(QtCore.QRect(210, 170, 471, 141))
font = QtGui.QFont()
font.setPointSize(33)
self.spinBox.setFont(font)
self.spinBox.setAlignment(QtCore.Qt.AlignCenter)
self.spinBox.setObjectName("spinBox")
self.horizontalSlider = QtWidgets.QSlider(Dialog)
self.horizontalSlider.setGeometry(QtCore.QRect(209, 360, 471, 61))
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(310, 460, 271, 71))
font = QtGui.QFont()
font.setPointSize(13)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
############
self.pushButton.clicked.connect(self.CloseAndRefresh)
############
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(210, 40, 471, 91))
font = QtGui.QFont()
font.setPointSize(24)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.retranslateUi(Dialog)
self.horizontalSlider.valueChanged['int'].connect(self.spinBox.setValue)
self.spinBox.valueChanged['int'].connect(self.horizontalSlider.setValue)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton.setText(_translate("Dialog", "Back to first Window"))
self.label.setText(_translate("Dialog", "Value"))
#######################################################
# Close second window and refresh label on first window
def CloseAndRefresh(self):
global value
value = self.spinBox.value()
print(value) #checking input
##################################################
# The refresh of the outputting label on the #
# MainWindow should be started at this position. #
##################################################
Dialog.close()
#######################################################
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
Dialog = QtWidgets.QDialog()
dia = Ui_Dialog()
dia.setupUi(Dialog)
sys.exit(app.exec_())
Try it:
from PyQt5 import QtCore, QtGui, QtWidgets
value = 0
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(890, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(180, 100, 500, 250))
font = QtGui.QFont()
font.setPointSize(20)
self.label.setFont(font)
self.label.setStyleSheet("background-color: rgb(255, 255, 255);")
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(180, 370, 500, 100))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
############
self.pushButton.clicked.connect(self.OpenInput)
############
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 retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Value"))
self.pushButton.setText(_translate("MainWindow", "Go to input window"))
##############################
# Show second window for input
def OpenInput(self, MainWindow):
Dialog.show()
##############################
# +++
def labelText(self, MainWindow, value):
self.label.setText(str(value))
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(889, 598)
self.spinBox = QtWidgets.QSpinBox(Dialog)
self.spinBox.setGeometry(QtCore.QRect(210, 170, 471, 141))
font = QtGui.QFont()
font.setPointSize(33)
self.spinBox.setFont(font)
self.spinBox.setAlignment(QtCore.Qt.AlignCenter)
self.spinBox.setObjectName("spinBox")
self.horizontalSlider = QtWidgets.QSlider(Dialog)
self.horizontalSlider.setGeometry(QtCore.QRect(209, 360, 471, 61))
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(310, 460, 271, 71))
font = QtGui.QFont()
font.setPointSize(13)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
############
self.pushButton.clicked.connect(self.CloseAndRefresh)
############
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(210, 40, 471, 91))
font = QtGui.QFont()
font.setPointSize(24)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.retranslateUi(Dialog)
self.horizontalSlider.valueChanged['int'].connect(self.spinBox.setValue)
self.spinBox.valueChanged['int'].connect(self.horizontalSlider.setValue)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton.setText(_translate("Dialog", "Back to first Window"))
self.label.setText(_translate("Dialog", "Value"))
#######################################################
# Close second window and refresh label on first window
def CloseAndRefresh(self):
global value
value = self.spinBox.value()
print(value) #checking input
# +++
ui.labelText(MainWindow, value)
##################################################
# The refresh of the outputting label on the #
# MainWindow should be started at this position. #
##################################################
Dialog.close()
#######################################################
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
Dialog = QtWidgets.QDialog()
dia = Ui_Dialog()
dia.setupUi(Dialog)
sys.exit(app.exec_())

Python PyQt5 threading don't work

i want to create a progress bar showing(as busy) until the code of a button ends .My target is to show it when button(connected to function) pressed ,stay visible as code runs , and hide it when the function connected to the button ends.This task is done partially but my progress bar setVisible(True) waits until code ends and they show.
My code is here:
from ui import Ui_MainWindow
from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport
from PyQt5.QtWidgets import QDialog,QWidget,QApplication, QInputDialog, QLineEdit, QFileDialog
from PyQt5.QtGui import QIcon
class ProgressThread(QtCore.QThread):
job_done = QtCore.pyqtSignal('QString')
def __init__(self, parent=None):
super(ProgressThread, self).__init__(parent)
self.gui_bar = None
def do_work(self):
self.job_done.emit(self.gui_bar)
def run(self):
self.do_work()
class mainProgram(QtWidgets.QMainWindow, Ui_MainWindow): #main window
def __init__(self, parent=None):
super(mainProgram, self).__init__(parent)
self.setupUi(self)
self.progress_thread = ProgressThread()
self.progress_thread.job_done.connect(self.on_job_start)
self.create_ui()
self.B_aerodrome_data.clicked.connect(self.start_thread)
self.B_aerodrome_data.clicked.connect(self.aerodrome_data)
def aerodrome_data(self): #def i want with start show progress bar
#here show progressBar(self.progress.setVisible(True) and update gui)
#do many staffs of code here that takes time
#here hide progress bar and update gui
def start_thread(self):
self.progress_thread.gui_bar = self.progress.setVisible(False)
self.progress_thread.start()
def on_job_done(self):
print("Generated string : ")
self.progress.setVisible(False)
def on_job_start(self):
print("aaaaaaaaaa")
self.progress.setRange(0,0)
self.progress.setVisible(True)
def create_ui(self):
self.progress = QtWidgets.QProgressBar(self)
self.progress.setVisible(False)
self.progress.setGeometry(200, 205, 250, 20)
self.progress.setRange(0, 0)
self.progress.setObjectName("progress")
#self.button.clicked.connect(self.start_thread)
layout = QtWidgets.QVBoxLayout(self)
#layout.addWidget(self.button)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
nextGui = mainProgram()
nextGui.showMaximized()
sys.exit(app.exec_())
The Ui_Mainwindow is here:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1371, 924)
MainWindow.setAcceptDrops(True)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("images/356.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
MainWindow.setAutoFillBackground(True)
MainWindow.setDocumentMode(True)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.L_adinput = QtWidgets.QLabel(self.centralWidget)
self.L_adinput.setGeometry(QtCore.QRect(30, 70, 141, 31))
self.L_adinput.setObjectName("L_adinput")
# self.progress = QtWidgets.QProgressBar(self)
# self.progress.setGeometry(200, 205, 250, 20)
# self.progress.setProperty("value", 0)
# self.progress.setObjectName("progress")
self.L_progress = QtWidgets.QLabel(self.centralWidget)
self.L_progress.setGeometry(QtCore.QRect(150, 200, 141, 31))
self.L_progress.setObjectName("L_progress")
self.B_aerodrome_data = QtWidgets.QPushButton(self.centralWidget)
self.B_aerodrome_data.setGeometry(QtCore.QRect(670, 100, 271, 41))
self.B_aerodrome_data.setObjectName("B_aerodrome_data")
self.T_aerodrome_ouput = QtWidgets.QTextEdit(self.centralWidget)
self.T_aerodrome_ouput.setGeometry(QtCore.QRect(30, 230, 561, 371))
self.T_aerodrome_ouput.setReadOnly(True)
self.T_aerodrome_ouput.setObjectName("T_aerodrome_ouput")
self.B_alternates = QtWidgets.QPushButton(self.centralWidget)
self.B_alternates.setGeometry(QtCore.QRect(670, 160, 271, 41))
self.B_alternates.setObjectName("B_alternates")
self.B_weather = QtWidgets.QPushButton(self.centralWidget)
self.B_weather.setGeometry(QtCore.QRect(670, 220, 271, 41))
self.B_weather.setObjectName("B_weather")
self.B_fuel = QtWidgets.QPushButton(self.centralWidget)
self.B_fuel.setEnabled(True)
self.B_fuel.setGeometry(QtCore.QRect(1050, 100, 271, 41))
self.B_fuel.setObjectName("B_fuel")
self.set_path1 = QtWidgets.QPushButton(self.centralWidget)
self.set_path1.setEnabled(True)
self.set_path1.setGeometry(QtCore.QRect(1100, 160, 101, 31))
self.set_path1.setObjectName("set_path1")
self.set_path2 = QtWidgets.QPushButton(self.centralWidget)
self.set_path2.setEnabled(True)
self.set_path2.setGeometry(QtCore.QRect(1100, 210, 101, 31))
self.set_path2.setObjectName("set_path2")
self.set_path3 = QtWidgets.QPushButton(self.centralWidget)
self.set_path3.setEnabled(True)
self.set_path3.setGeometry(QtCore.QRect(1100, 260, 101, 31))
self.set_path3.setObjectName("set_path3")
self.set_path4 = QtWidgets.QPushButton(self.centralWidget)
self.set_path4.setEnabled(True)
self.set_path4.setGeometry(QtCore.QRect(1100, 310, 101, 31))
self.set_path4.setObjectName("set_path4")
self.set_path5 = QtWidgets.QPushButton(self.centralWidget)
self.set_path5.setEnabled(True)
self.set_path5.setGeometry(QtCore.QRect(1100, 360, 101, 31))
self.set_path5.setObjectName("set_path5")
self.tool1 = QtWidgets.QToolButton(self.centralWidget)
self.tool1.setGeometry(QtCore.QRect(1240, 170, 25, 19))
self.tool1.setObjectName("tool1")
self.tool2 = QtWidgets.QToolButton(self.centralWidget)
self.tool2.setGeometry(QtCore.QRect(1240, 220, 25, 20))
self.tool2.setObjectName("tool2")
self.tool3 = QtWidgets.QToolButton(self.centralWidget)
self.tool3.setGeometry(QtCore.QRect(1240, 270, 25, 19))
self.tool3.setObjectName("tool3")
self.tool4 = QtWidgets.QToolButton(self.centralWidget)
self.tool4.setGeometry(QtCore.QRect(1240, 320, 25, 19))
self.tool4.setObjectName("tool4")
self.tool1x = QtWidgets.QToolButton(self.centralWidget)
self.tool1x.setGeometry(QtCore.QRect(1280, 220, 25, 19))
self.tool1x.setObjectName("tool1x")
self.tool2x = QtWidgets.QToolButton(self.centralWidget)
self.tool2x.setGeometry(QtCore.QRect(1280, 270, 25, 19))
self.tool2x.setObjectName("tool2x")
self.tool3x = QtWidgets.QToolButton(self.centralWidget)
self.tool3x.setGeometry(QtCore.QRect(1280, 320, 25, 19))
self.tool3x.setObjectName("tool3x")
self.tool4x = QtWidgets.QToolButton(self.centralWidget)
self.tool4x.setGeometry(QtCore.QRect(1280, 370, 25, 19))
self.tool4x.setObjectName("tool4x")
self.line = QtWidgets.QFrame(self.centralWidget)
self.line.setGeometry(QtCore.QRect(990, -50, 20, 851))
self.line.setFrameShape(QtWidgets.QFrame.VLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
self.T_aerodromes_input = QtWidgets.QTextEdit(self.centralWidget)
self.T_aerodromes_input.setGeometry(QtCore.QRect(30, 100, 331, 71))
self.T_aerodromes_input.setStyleSheet("font: 10pt \"MS Shell Dlg 2\";")
self.T_aerodromes_input.setObjectName("T_aerodromes_input")
self.label = QtWidgets.QLabel(self.centralWidget)
self.label.setGeometry(QtCore.QRect(30, 200, 161, 31))
self.label.setObjectName("label")
self.B_print = QtWidgets.QPushButton(self.centralWidget)
self.B_print.setGeometry(QtCore.QRect(530, 230, 41, 31))
self.B_print.setText("")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("images/printer.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.B_print.setIcon(icon1)
self.B_print.setIconSize(QtCore.QSize(41, 31))
self.B_print.setObjectName("B_print")
self.B_clear = QtWidgets.QPushButton(self.centralWidget)
self.B_clear.setGeometry(QtCore.QRect(490, 230, 41, 31))
self.B_clear.setText("")
self.label_2 = QtWidgets.QLabel(self.centralWidget)
self.label_2.setGeometry(QtCore.QRect(670, 30, 271, 41))
self.label_2.setStyleSheet("font: 75 11pt \"MS Shell Dlg 2\";")
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralWidget)
self.label_3.setGeometry(QtCore.QRect(1050, 30, 271, 41))
self.label_3.setStyleSheet("font: 75 11pt \"MS Shell Dlg 2\";")
self.label_3.setAlignment(QtCore.Qt.AlignCenter)
self.label_3.setObjectName("label_3")
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("images/clear.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.B_clear.setIcon(icon2)
self.B_clear.setIconSize(QtCore.QSize(41, 31))
self.B_clear.setObjectName("B_clear")
self.L_adinput.raise_()
self.L_progress.raise_()
self.B_aerodrome_data.raise_()
self.T_aerodrome_ouput.raise_()
self.B_alternates.raise_()
self.B_weather.raise_()
self.B_fuel.raise_()
self.set_path2.raise_()
self.set_path3.raise_()
self.set_path4.raise_()
self.set_path5.raise_()
self.tool1.raise_()
self.set_path1.raise_()
self.tool2.raise_()
self.tool3.raise_()
self.tool4.raise_()
self.tool1x.raise_()
self.tool2x.raise_()
self.tool3x.raise_()
self.tool4x.raise_()
self.line.raise_()
self.T_aerodromes_input.raise_()
self.label.raise_()
self.B_print.raise_()
self.B_clear.raise_()
self.label_2.raise_()
self.label_3.raise_()
MainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(MainWindow)
self.tool1.clicked.connect(self.tool2.show)
self.tool1.clicked.connect(self.set_path2.show)
self.tool2.clicked.connect(self.tool3.show)
self.tool3.clicked.connect(self.tool4.show)
self.tool3.clicked.connect(self.set_path4.show)
self.tool4.clicked.connect(self.set_path5.show)
self.tool2.clicked.connect(self.set_path3.show)
self.tool4x.clicked.connect(self.set_path5.hide)
self.tool3x.clicked.connect(self.tool4.hide)
self.tool3x.clicked.connect(self.set_path4.hide)
self.tool2x.clicked.connect(self.tool3.hide)
self.tool2x.clicked.connect(self.set_path3.hide)
self.tool1x.clicked.connect(self.tool2.hide)
self.tool1x.clicked.connect(self.set_path2.hide)
self.tool4x.clicked.connect(self.tool4x.hide)
self.tool3x.clicked.connect(self.tool3x.hide)
self.tool2x.clicked.connect(self.tool2x.hide)
self.tool1x.clicked.connect(self.tool1x.hide)
self.tool1.clicked.connect(self.tool1x.show)
self.tool2.clicked.connect(self.tool2x.show)
self.tool3.clicked.connect(self.tool3x.show)
self.tool4.clicked.connect(self.tool4x.show)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Planner"))
self.L_adinput.setText(_translate("MainWindow", "ICAO LOCATORS"))
self.L_progress.setText(_translate("MainWindow", "Progress"))
self.B_aerodrome_data.setText(_translate("MainWindow", "AERODROME DATA"))
# self.T_aerodrome_ouput.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
# "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
# "p, li { white-space: pre-wrap; }\n"
# "</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
# "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>"))
self.B_alternates.setText(_translate("MainWindow", "ALTERNATES XLSX"))
self.B_weather.setText(_translate("MainWindow", "WEATHER BRIEFING"))
self.B_fuel.setText(_translate("MainWindow", "FUEL XLSX"))
self.set_path1.setText(_translate("MainWindow", "SET FORM"))
self.set_path2.setText(_translate("MainWindow", "SET FORM"))
self.set_path3.setText(_translate("MainWindow", "SET FORM"))
self.set_path4.setText(_translate("MainWindow", "SET FORM"))
self.set_path5.setText(_translate("MainWindow", "SET FORM"))
self.tool1.setText(_translate("MainWindow", "..."))
self.tool2.setText(_translate("MainWindow", "..."))
self.tool3.setText(_translate("MainWindow", "..."))
self.tool4.setText(_translate("MainWindow", "..."))
self.tool1x.setText(_translate("MainWindow", "X"))
self.tool2x.setText(_translate("MainWindow", "X"))
self.tool3x.setText(_translate("MainWindow", "X"))
self.tool4x.setText(_translate("MainWindow", "X"))
self.T_aerodromes_input.setPlaceholderText(_translate("MainWindow", "Set 4 character ICAO Locator here , divided by spaces..."))
self.label.setText(_translate("MainWindow", "OUTPUT"))
self.label_2.setText(_translate("MainWindow", "GENERAL"))
self.label_3.setText(_translate("MainWindow", "FUEL"))
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_())
In your case I see that you have created functions unnecessarily, I have eliminated some. According to what you tell me, it is not necessary for the signal to carry information, so I will create a signal without parameters. the slot connected to the button must make visible the QProgressBar and start the thread, and the signal job_done must connect with the method that hides the QProgressBar, in the following example use QThread::sleep() to emulate some processing.
class ProgressThread(QtCore.QThread):
job_done = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(ProgressThread, self).__init__(parent)
def do_work(self):
QtCore.QThread.sleep(2) # emulate processing
self.job_done.emit()
def run(self):
self.do_work()
class mainProgram(QtWidgets.QMainWindow, Ui_MainWindow): # main window
def __init__(self, parent=None):
super(mainProgram, self).__init__(parent)
self.setupUi(self)
self.progress_thread = ProgressThread()
self.progress_thread.job_done.connect(self.on_job_done)
self.create_ui()
self.B_aerodrome_data.clicked.connect(self.aerodrome_data)
def aerodrome_data(self):
self.start_thread()
def start_thread(self):
self.progress.setVisible(True)
self.progress_thread.start()
def on_job_done(self):
print("Generated string : ")
self.progress.setVisible(False)
def create_ui(self):
[...]
try this code, you will find your solution from here.
when you will press the button thread will start and emit int data to the progress bar from 0-100
from PyQt5.QtWidgets import QMainWindow,QWidget,QApplication,QVBoxLayout,QPushButton,QProgressBar
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt,QThread,pyqtSignal
import time
import sys
class THREAD(QThread): # QThread is use for parallal execution
EMIT=pyqtSignal(int)
def run(self):
count=0
while count<=100:
count+=1
self.EMIT.emit(count)
time.sleep(0.1)
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.ICON="icon.jpg"
self.TITLE="ROBO Science LAB"
self.TOP = 100
self.LEFT = 100
self.WIDTH = 700
self.HEIGHT = 500
self.InitWindow()
self.setCentralWidget(ComponentManagement())
self.show()
def InitWindow(self):
self.setWindowIcon(QIcon(self.ICON))
self.setWindowTitle(self.TITLE)
self.setGeometry(self.TOP,self.LEFT,self.WIDTH,self.HEIGHT)
class ComponentManagement(QWidget):
def __init__(self):
super().__init__()
self.UIcomponent()
def UIcomponent(self):
vbox = QVBoxLayout()
self.progress1 = QProgressBar()
BUTTON = QPushButton("Click to start progress")
BUTTON.setStyleSheet("background:#E67E22")
BUTTON.clicked.connect(self.StartProgressBar)
vbox.addWidget(self.progress1)
vbox.addWidget(BUTTON)
self.setLayout(vbox)
def SetProgressValue(self, val):
self.progress1.setValue(val)
def StartProgressBar(self):
self.ThredObject = THREAD()
self.ThredObject.EMIT.connect(self.SetProgressValue)
self.ThredObject.start()
if __name__=="__main__":
App=QApplication(sys.argv)
window=Window()
sys.exit(App.exec())

pyqt5 QMessageBox not open

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(9576, 698)
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(9)
MainWindow.setFont(font)
MainWindow.setFocusPolicy(QtCore.Qt.StrongFocus)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 30, 221, 421))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.RButton = QtWidgets.QRadioButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.RButton.setFont(font)
self.RButton.setObjectName("RButton")
self.verticalLayout.addWidget(self.RButton)
self.RButton2 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.RButton2.setFont(font)
self.RButton2.setObjectName("RButton2")
self.verticalLayout.addWidget(self.RButton2)
self.RButton3 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.RButton3.setFont(font)
self.RButton3.setObjectName("RButton3")
self.verticalLayout.addWidget(self.RButton3)
self.Button = QtWidgets.QPushButton(self.centralwidget)
self.Button.setGeometry(QtCore.QRect(390, 150, 351, 221))
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(24)
self.Button.setFont(font)
self.Button.setFocusPolicy(QtCore.Qt.NoFocus)
self.Button.setAutoDefault(False)
self.Button.setObjectName("Button")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 957, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.Button.clicked.connect(self.btn_clicked)
self.RButton.clicked.connect(self.btn_clicked)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def btn_clicked(self):
QMessageBox.about(self,"창","안녕")
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.RButton.setText(_translate("MainWindow", "버튼1"))
self.RButton2.setText(_translate("MainWindow", "버튼2"))
self.RButton3.setText(_translate("MainWindow", "버튼3"))
self.Button.setText(_translate("MainWindow", "확인"))
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 button clicked program is turn off...
I need help
The problem is caused because QMessageBox.about() requires as a first parameter a widget but self, that is Ui_MainWindow, is not a widget. Qt Designer does not generate a widget but creates a class that fills a widget. It is also not recommended to modify the class generated by Qt Designer, it is appropriate to create a class that inherits from the appropriate widget and use the design class
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.Button.clicked.connect(self.btn_clicked)
self.RButton.clicked.connect(self.btn_clicked)
def btn_clicked(self):
QMessageBox.about(self,"창","안녕")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

Categories

Resources