Python PyQt5 对话框元素仅显示 exec_ 而没有显示

Posted

技术标签:

【中文标题】Python PyQt5 对话框元素仅显示 exec_ 而没有显示【英文标题】:Python PyQt5 dialog elements shows only with exec_ and no with show 【发布时间】:2018-01-01 14:29:25 【问题描述】:

我有一个主 gui(Ui_Mainwindow),其中有几个与按钮连接的功能。当按钮 (B_aerodrome_data(Ui_Mainwindow)) 单击时,我想要一个带有脉动进度条的对话框(dialog3)(progressBar(dialog3)) show()(as aerodrome_data 函数运行),然后在函数结束时隐藏() dialog3。问题是当我使用 dialog3.show()时,dialog3 出现时没有标签(label(dialog3))和 progressBar(dialog3),我可以'不要使用 exec_() 因为这将停止 aerodrome_data 函数。

我的代码在这里:

import sys
import os
import re
import time
from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport
from PyQt5.QtWidgets import QDialog,QWidget,QApplication, QInputDialog, QLineEdit, QFileDialog
from PyQt5.QtGui import QIcon
from ui import Ui_MainWindow
from dialog3 import Ui_dialog3



class Dialog3(QtWidgets.QDialog,Ui_dialog3):
    def __init__(self,parent=None):
        super(Dialog3, self).__init__(parent)
        self.setupUi(self)


class mainProgram(QtWidgets.QMainWindow, Ui_MainWindow):                   #main window
    def __init__(self, parent=None):
        super(mainProgram, self).__init__(parent)
        self.setupUi(self)

        self.dialog3 = Dialog3(self)


        self.set_path2.setVisible(False)
        self.set_path3.setVisible(False)
        self.set_path4.setVisible(False)
        self.set_path5.setVisible(False)

        self.tool2.setVisible(False)
        self.tool3.setVisible(False)
        self.tool4.setVisible(False)
        self.tool1x.setVisible(False)
        self.tool2x.setVisible(False)
        self.tool3x.setVisible(False)
        self.tool4x.setVisible(False)



        self.B_aerodrome_data.clicked.connect(self.aerodrome_data)


    def aerodrome_data(self):

        self.dialog3.show()   #here is the problem show()

        #do many stuff here that i dont't know how much time it takes

        self.dialog3.hide()

if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
nextGui = mainProgram()
nextGui.showMaximized()
sys.exit(app.exec_()) 

用户界面在这里:

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.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.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.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_())

dialog3 在这里:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_dialog3(object):
    def setupUi(self, dialog3):
        dialog3.setObjectName("dialog3")
        dialog3.resize(477, 149)
        dialog3.setMinimumSize(QtCore.QSize(477, 149))
        dialog3.setMaximumSize(QtCore.QSize(477, 149))
        self.label = QtWidgets.QLabel(dialog3)
        self.label.setGeometry(QtCore.QRect(150, 40, 221, 20))
        self.label.setObjectName("label")
        self.progressBar = QtWidgets.QProgressBar(dialog3)
        self.progressBar.setGeometry(QtCore.QRect(60, 80, 351, 23))
        self.progressBar.setMaximum(0)
        self.progressBar.setProperty("value", 0)
        self.progressBar.setAlignment(QtCore.Qt.AlignCenter)
        self.progressBar.setObjectName("progressBar")

        self.retranslateUi(dialog3)
        QtCore.QMetaObject.connectSlotsByName(dialog3)

    def retranslateUi(self, dialog3):
        _translate = QtCore.QCoreApplication.translate
        dialog3.setWindowTitle(_translate("dialog3", "Aerodrome data"))
        self.label.setText(_translate("dialog3", "This might take a moment , plz wait......"))



if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    dialog3 = QtWidgets.QDialog()
    ui = Ui_dialog3()
    ui.setupUi(dialog3)
    dialog3.show()
    sys.exit(app.exec_())

提前致谢。

已编辑 抱歉,我是新人

【问题讨论】:

改善代码的缩进,在python中非常重要 抱歉,刚刚编辑。 您认为有人会理解以下内容:我有一个主 gui,其中有几个与按钮连接的功能。当单击按钮时(B_aerodrome_data)我想要一个带有脉动进度条显示的对话框()(当 def() 运行时) 然后在函数结束时我隐藏() 对话框。问题是当我使用 show() 时,对话框出现时没有标签和进度条,我不能使用 exec_() 因为这将停止我想做的def(),def()是什么,你说什么标签和进度条? 再次编辑 【参考方案1】:

为了能够显示该对话框,您必须调用其 exec_() 函数。在不确切知道您在上面的显示和隐藏功能之间做什么的情况下,我建议您从正在打开的对话框中运行这些操作。如果操作很长并且对话框需要更新,甚至可以从单独的线程运行它们。

【讨论】:

以上是关于Python PyQt5 对话框元素仅显示 exec_ 而没有显示的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Python、PyQt5 和 Pyinstaller 修复未正确显示的按钮

Scrapy + pyqt5:信号仅在主线程错误中有效

Python GUI,pycharm+Pyqt5 配置及打包成exe

Python _PyQt5对话框

PyQt5 显示全屏对话框

pyqt5 继承python类可以传递信号函数吗