PyQt的:在对话框中连接按钮 [英] PyQt: Connecting a button in a dialog
问题描述
我在写我的第一个PyQt的计划,但我有一个按钮有问题。我看了一些其他的问答集A的,但我没能解决这个问题。
I'm writing my first PyQt program, but I have a problem with a pushbutton. I read some other Q&A's but I wasn't able to solve it.
基本上我有一个菜单栏主窗口。通过点击菜单项actionSelect,打开一个名为SelectFiles新的对话框。它包含一个名为ChooseDirButton按钮应该打开选择目录窗口小部件,改变与选定的目录中的ShowPathlinedit文本。
Basically I have a main window with a menu bar. By clicking on the menu item "actionSelect", a new dialog called SelectFiles is opened. It contains a pushbutton called "ChooseDirButton" that should open the select directory widget and change the "ShowPath" linedit text with the selected directory.
我的code是这样的:
My code looks like this:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import TeraGui
class MainWindow(QMainWindow, TeraGui.Ui_MainWindow):
path = ""
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.actionSelect.triggered.connect(self.Select)
def Select(self):
dialog = QDialog()
dialog.ui = TeraGui.Ui_SelectFiles()
dialog.ui.setupUi(dialog)
dialog.setAttribute(Qt.WA_DeleteOnClose)
dialog.exec_()
def ChooseDirectory():
global path
path = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
self.ShowPath.setText(path)
app = QApplication(sys.argv)
form = MainWindow()
form.show()
app.exec_()
我不能让点击按钮ChooseDirButton的时候要执行的ChooseDirectory方法。我试图将它们连接起来,但我不明白正确的语法。而且可能有一些错误的方法ChooseDirectory也。
我创建了GUI使用Qt Designer,并与进口TeraGui命令将其导入。
I'm not able to let the ChooseDirectory method be executed when the pushbutton "ChooseDirButton" is clicked. I tried to connect them, but I do not understand the right syntax. Moreover there could be something wrong in the ChooseDirectory method also. I created the GUI with Qt Designer and import it with the "import TeraGui" command.
推荐答案
看起来你需要创建一个子类的对话,就像你的主窗口。
It looks like you need to create a subclass for your dialog, just as you did for the main window.
我无法实际测试,如果没有你的UI模块,但这样的事情应该工作:
I can't actually test it without your ui modules, but something like this should work:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import TeraGui
class MainWindow(QMainWindow, TeraGui.Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.actionSelect.triggered.connect(self.Select)
def Select(self):
dialog = Dialog(self)
dialog.exec_()
self.ShowPath.setText(dialog.path)
class Dialog(QDialog, TeraGui.Ui_SelectFiles):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setAttribute(Qt.WA_DeleteOnClose)
self.setupUi(self)
self.ChooseDirButton.clicked.connect(self.ChooseDirectory)
self.path = ''
def ChooseDirectory(self):
self.path = str(QFileDialog.getExistingDirectory(
self, "Select Directory"))
app = QApplication(sys.argv)
form = MainWindow()
form.show()
app.exec_()
这篇关于PyQt的:在对话框中连接按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!