I have created a dialog with Qt Creator and then translated the .ui file to .py file with
pyuic5 dialog.ui -o dialog.py
The resulting code is the following
class Ui_dialog(object):
def setupUi(self, dialog):
dialog.setObjectName("dialog")
dialog.resize(976, 725)
self.verticalLayoutWidget = QtWidgets.QWidget(dialog)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(360, 210, 160, 80))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.retranslateUi(dialog)
QtCore.QMetaObject.connectSlotsByName(dialog)
def retranslateUi(self, dialog):
_translate = QtCore.QCoreApplication.translate
dialog.setWindowTitle(_translate("dialog", "Dialog"))
self.pushButton.setText(_translate("dialog", "PushButton"))
Now I'm trying to display the dialog from my main window with
dialog = QDialog()
dialog.ui = Ui_dialog()
dialog.ui.setupUi(self)
dialog.show()
dialog.exec_()
The dialog is shown, but it's empty so there's no button or any other widgets I tried!?
Ui_Dialog's setupUi method requires a widget to fill in, and in your case you should change the following:
dialog.ui.setupUi(self)
to:
dialog.ui.setupUi(dialog)
Related
I have a window created by pyqt and there is a button that clicks to show a dialog that takes input from the user using QinputDialog, normally when I press ok after input, QinputDialog is closed and the main window is closed. is also closed, how to click ok in Qinputdialog that the main window is not closed? Thank so much!
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import *
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(60, 30, 291, 61))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(130, 140, 75, 23))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.pushButton.clicked.connect(self.showinput)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "LABEL"))
self.pushButton.setText(_translate("Dialog", "CLICK"))
def showinput(self):
app2 = QApplication(sys.argv)
split_date, ok = QInputDialog.getText(None, "Input Dialog", "Input something?")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec())
Is it possible for a QDialog to send a value back to the application without also closing the dialog window? A common example would be the insert symbol dialog in Microsoft Word. (In fact, this is what I am trying to replicate.)
I have included some basic code for a main window with a label and a button. Pushing the button opens the dialog window. The dialog window contains a textLineEdit widget, an OK button, and a Cancel button. After pushing OK any text entered into the dialog's textLineEdit is set as the text in the main window's label. Would it be possible to return the entered text without closing the dialog? I know it would require some on_OK_clicked procedure in the Dialog class, but I cannot seem to return a value without also closing the dialog. Any ideas would be greatly appreciated.
import sys
from PyQt5 import QtCore, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(320, 86)
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.setObjectName("label")
self.verticalLayout.addWidget(self.label)
spacerItem = QtWidgets.QSpacerItem(20, 13, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
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.label.setText(_translate("MainWindow", "TextLabel"))
self.pushButton.setText(_translate("MainWindow", "Open Dialog"))
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 94)
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(20, 20, 361, 22))
self.lineEdit.setObjectName("lineEdit")
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(40, 50, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept) # type: ignore
self.buttonBox.rejected.connect(Dialog.reject) # type: ignore
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
class Dialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None):
QtWidgets.QDialog.__init__(self, parent)
self.setupUi(self)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.onClicked)
def onClicked(self):
dlg = Dialog()
if dlg.exec_():
self.label.setText(dlg.lineEdit.text())
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Thanks to ekhumoro for the help I needed. The solution was to use an Apply button in the QDialogButtonBox and to connect the click action to a slot in the main window. In this case, I found I had to use a lambda function as the slot. (This is the last piece I was missing.) dlg.buttonBox.button(QtWidgets.QDialogButtonBox.Apply).clicked.connect(lambda: self.label.setText(dlg.lineEdit.text()))
Here is the full working code.
import sys
from PyQt5 import QtCore, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(320, 86)
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.setObjectName("label")
self.verticalLayout.addWidget(self.label)
spacerItem = QtWidgets.QSpacerItem(20, 13, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
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.label.setText(_translate("MainWindow", "TextLabel"))
self.pushButton.setText(_translate("MainWindow", "Open Dialog"))
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 94)
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(20, 20, 361, 22))
self.lineEdit.setObjectName("lineEdit")
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(40, 50, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Close|QtWidgets.QDialogButtonBox.Apply)
self.buttonBox.button(QtWidgets.QDialogButtonBox.Apply).setText("Insert")
self.buttonBox.setObjectName("buttonBox")
self.retranslateUi(Dialog)
self.buttonBox.rejected.connect(Dialog.reject) # type: ignore
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
class Dialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None):
QtWidgets.QDialog.__init__(self, parent)
self.setupUi(self)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.onClicked)
def onClicked(self):
dlg = Dialog()
dlg.buttonBox.button(QtWidgets.QDialogButtonBox.Apply).clicked.connect(lambda: self.label.setText(dlg.lineEdit.text()))
dlg.exec()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
I want to add Qlabels on runtime by clicking the pushbutton.
For example, I want to add "Helloworld" text label each time the button is clicked.
I have written code but it replaces not add Qlabel in scrollArea, I want to add "Helloworld" in the scrollArea each time the pushbutton is pressed. Secondly, I have created the Qlabel label_99 on runtime, But I think this will not work. As
multiple Qlabels must be created on runtime, as I want to add Qlabel each time I click a pushbutton.
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
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.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea.setGeometry(QtCore.QRect(130, 80, 221, 301))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.scrollArea.sizePolicy().hasHeightForWidth())
self.scrollArea.setSizePolicy(sizePolicy)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 219, 299))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.verticalLayout = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents)
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.label_2 = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.label_2.setObjectName("label_2")
self.verticalLayout.addWidget(self.label_2)
self.label_3 = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.label_3.setObjectName("label_3")
self.verticalLayout.addWidget(self.label_3)
self.label_4 = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.label_4.setObjectName("label_4")
self.verticalLayout.addWidget(self.label_4)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(390, 110, 89, 25))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.button_pressed)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
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", "TextLabel"))
self.label_2.setText(_translate("MainWindow", "TextLabel"))
self.label_3.setText(_translate("MainWindow", "TextLabel"))
self.label_4.setText(_translate("MainWindow", "TextLabel"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
def button_pressed(self):
print("button pressed")
self.add_new_label()
def add_new_label(self):
self.label_99 = QtWidgets.QLabel()
label_99=QtWidgets.QLabel()
label_99.setText("HelloWorld")
self.scrollArea.setWidget(label_99)
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_())
setWidget sets the widget for the scroll are, and that is why the code you have doesn't work since it removes the previous widget from the scroll area, and sets the new label as the scroll area widget.
You need to create a new instance of QLabel, and add it to the layout of the scroll area widget, which is self.verticalLayout for the above class.
def add_new_label(self):
newLabel = QtWidgets.QLabel('Hello World')
self.verticalLayout.addWidget(newLabel)
The code to create new label and assign it class instance attribute as self.label_99 = QtWidgets.QLabel() is useless in your case. You just need to create a local variable to hold the label, and finally add it to the scroll area's widget's layout.
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_())
I want the click of 'OK' to run some code and then NOT close the dialog
Ui code is as below. I capture the button click using
self.accepted.connect(self.browse_folder)
However, what is happening is that as self.browse_folder is called, the main dialog closes. This behaviour seems to be Related to reject / accept connections in a ButtonBox
from PyQt5 import QtCore, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(600, 400)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(10, 350, 161, 41))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel |
QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.verticalLayoutWidget = QtWidgets.QWidget(Dialog)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 20, 561, 301))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.textEdit = QtWidgets.QTextEdit(self.verticalLayoutWidget)
self.textEdit.setObjectName("textEdit")
self.verticalLayout.addWidget(self.textEdit)
self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Read A File"))
Try self.close()
class ThermalWindow(QDialog):
"""
This "window" is a QWidget. If it has no parent, it
will appear as a free-floating window as we want.
"""
def __init__(self,parent,title):
super().__init__()
self.parent = parent
self.resize(400,400)
self.setWindowTitle('Thermal ' + title)
buttons = QDialogButtonBox.Ok | QDialogButtonBox.Cancel
buttonBox = QDialogButtonBox(buttons)
buttonBox.accepted.connect(self.ok_callback)
buttonBox.rejected.connect(self.cancel_callback)
layout = QFormLayout()
self.var = QLineEdit()
layout.addRow(title + " :", self.var)
layout.addWidget(buttonBox)
self.setLayout(layout)
def ok_callback(self):
print("OK")
self.close()
def cancel_callback(self):
print("Cancel")
self.close()