【问题标题】:PyQt5 Designer. Getting a button event to functionPyQt5 设计师。使按钮事件起作用
【发布时间】:2018-04-05 09:16:01
【问题描述】:

我正在尝试使用 Qt Designer 让按钮事件处理程序工作。

我在 Python 3.6 中使用 Anaconda-Spyder

出现表单,但按钮btn_browse 不起作用。行编辑框有一个光标,你可以在里面输入。

从 ui 自动生成的 Python 文件如下。它被称为file_reader.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(640, 320)
        self.btn_browse = QtWidgets.QPushButton(Dialog)
        self.btn_browse.setGeometry(QtCore.QRect(220, 50, 113, 32))
        self.btn_browse.setObjectName("btn_browse")
        self.lineEdit = QtWidgets.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(170, 120, 241, 131))
        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.btn_browse.setText(_translate("Dialog", "MyButton"))

我使用的代码(几乎来自 QtDesigner Docs 站点)是

from PyQt5 import QtGui
from PyQt5.QtWidgets import QDialog, QApplication
from file_reader import Ui_Dialog
class Dialog(QDialog):
    def __init__(self):
        super(Dialog, self).__init__()

        # Set up the user interface from Designer.
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

        # Make some local modifications.
        self.ui.colorDepthCombo.addItem("2 colors (1 bit per pixel)")

        # Connect up the buttons.
        self.ui.btn_browse.clicked.connect(self.browse_folder)


    def browse_folder(self):
        #exit
        print("Hello")
        #self.textBrowser.clear() # In case there are any existing elements in the list
        directory = QtGui.QFileDialog.getExistingDirectory(self,"Pick a folder")
        # execute getExistingDirectory dialog and set the directory variable to be equal
        # to the user selected directory

        if directory: # if user didn't pick a directory don't continue
            for file_name in os.listdir(directory): # for all files, if any, in the directory
                self.listWidget.addItem(file_name)  # add file to the listWidget




import sys        
app = QApplication(sys.argv)
window = Dialog() #Also tried QDialog() here
ui = Ui_Dialog()
ui.setupUi(window)

window.show()
sys.exit(app.exec_())

我认为没有调用函数browse_folder。我认为问题可能是使用的 QDialog 类而不是 QMainForm。我

我正在努力。另外,我不确定 ui 转换器中的 x 开关是做什么的。

我在这里看了几个答案,看不出我做错了什么。

【问题讨论】:

    标签: python python-3.x pyqt5 qt-designer


    【解决方案1】:

    您的代码存在以下问题:

    • 您创建的不是 Dialog 对象,而是一个填充有Ui_DialogQDialog,它没有browse_folder 方法或连接。

    • QFileDialogQtWidgets 的一部分,它不是QtGui 的一部分,您可能正在使用PyQt4 的示例。

    • 我假设listWidget 来自Ui_Dialog,所以您必须通过ui 登录。


    from PyQt5.QtWidgets import QDialog, QApplication, QFileDialog
    from file_reader import Ui_Dialog
    import os
    
    class Dialog(QDialog):
        def __init__(self):
            super(Dialog, self).__init__()
    
            # Set up the user interface from Designer.
            self.ui = Ui_Dialog()
            self.ui.setupUi(self)
    
            # Make some local modifications.
            self.ui.colorDepthCombo.addItem("2 colors (1 bit per pixel)")
    
            # Connect up the buttons.
            self.ui.btn_browse.clicked.connect(self.browse_folder)
    
    
        def browse_folder(self):
            #exit
            print("Hello")
            #self.textBrowser.clear() # In case there are any existing elements in the list
            directory = QFileDialog.getExistingDirectory(self,"Pick a folder")
            # execute getExistingDirectory dialog and set the directory variable to be equal
            # to the user selected directory
    
            if directory: # if user didn't pick a directory don't continue
                for file_name in os.listdir(directory): # for all files, if any, in the directory
                    self.ui.listWidget.addItem(file_name)  # add file to the listWidget
    
    
    import sys        
    app = QApplication(sys.argv)
    window = Dialog()
    window.show()
    sys.exit(app.exec_())
    

    【讨论】:

    • 你的意思是 window = Dialog() 应该是 window=QDialog() 吗?这两种我都试过了。
    • @nerak99 你试过我建议的代码了吗?
    • 嘿对不起eyllanesc。我不是这里的常客。在我注释掉'self.ui.colorDepthCombo.addItem(“2种颜色(每像素1位)”)'之后,我得到了某个地方。谢谢。是父类的 colorDepthCombo.additem() 方法吗?如果是这样,我应该通过 super() 或其他方式调用它
    • 感谢您的“Skandix”。如何在我的班级中调用 QDialog 方法?
    • @nerak99 QDialog不是方法,是类,调用它是什么意思?
    猜你喜欢
    • 1970-01-01
    • 2020-01-19
    • 2022-11-24
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 2014-09-26
    相关资源
    最近更新 更多