我正在尝试使用Qt Designer来处理按钮事件处理程序。
我正在使用Anaconda-Spyder和Python 3.6
表单出现但按钮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开关是做什么的。
我在这里看了几个答案,看不出我做错了什么。
答案 0 :(得分:1)
您的代码存在以下问题:
您没有创建Dialog对象,但QDialog
填充了Ui_Dialog
没有browse_folder方法或连接。
QFileDialog
是QtWidgets
的一部分,它不是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_())