Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyQt5设计师。获取要运行的按钮事件_Python_Python 3.x_Pyqt5_Qt Designer - Fatal编程技术网

Python PyQt5设计师。获取要运行的按钮事件

Python PyQt5设计师。获取要运行的按钮事件,python,python-3.x,pyqt5,qt-designer,Python,Python 3.x,Pyqt5,Qt Designer,我正在尝试使用Qt设计器使按钮事件处理程序工作 我在Python 3.6中使用Anaconda Spyder 表单出现,但按钮btn\u browse不起作用。“行”编辑框中有一个光标,您可以在其中键入 下面是从ui自动生成的Python文件。它被称为file\u reader.py from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Dialog(object): def setupUi(self, Dialog):

我正在尝试使用Qt设计器使按钮事件处理程序工作

我在Python 3.6中使用Anaconda Spyder

表单出现,但按钮
btn\u browse
不起作用。“行”编辑框中有一个光标,您可以在其中键入

下面是从ui自动生成的Python文件。它被称为
file\u 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文档站点)是

我认为没有调用函数
browse\u folder
。我认为问题可能是正在使用的QDialog类,而不是QMainForm。我

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


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

您的代码有以下问题:

  • 您不是在创建对话框对象,而是一个
    QDialog
    对话框,其中填充了
    Ui\u对话框
    ,该对话框没有浏览文件夹方法或连接

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

  • 我假设
    listWidget
    来自
    Ui\u对话框
    ,因此您必须通过
    Ui
    登录



您的意思是window=Dialog()应该是window=QDialog()?这两个我都试过了。@nerak99你试过我建议的代码了吗?嘿,对不起,艾伦斯。我不是这里的常客。在我注释掉“self.ui.colorDepthCombo.addItem”(“2种颜色(每像素1位)”)之后,我找到了答案。谢谢是父类中的colorDepthCombo.additem()方法吗?如果是这样的话,我应该通过super()或其他类似的名字来称呼它“Skandix”。如何从类中调用QDialog方法?@nerak99 QDialog不是一个方法,它是一个类,调用它是什么意思?
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_())
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_())