PyQt文件浏览-设置默认选项? [英] PyQt file browsing - setting a default option?

查看:104
本文介绍了PyQt文件浏览-设置默认选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在我正在设计的GUI中实现文件浏览小部件.我正在使用QFileDialog模块,该模块效果很好-我可以使用以下代码行浏览和保存文件:

I've been trying to implement a file browsing widget in the GUI I'm designing. I am using the QFileDialog module, which works great - I can browse and save a file with the following line of code:

filenames = QFileDialog.getOpenFileName()

我的窗口小部件设置有QLineEdit和QPushButton,我要显示所选文件的名称,QPushButton我要启动上面的代码行.但是,我想知道是否可以设置默认"选项.如果未单击浏览按钮,我希望文件如下:

My widget is set up with a QLineEdit, which I would like to display the name of the file selected, and a QPushButton, which I would like to initiate the above line of code. However, I'd like to know if there's a way I can set a "default" option. If the browse push button is not clicked, I would like the file to be the following:

filenames = str(glob.glob('*.npy')[0])

这将另存为相关文件名,并显示在我的LineEdit中.我的问题来自尝试在LineEdit中显示不同的文件名,具体取决于是否单击了浏览按钮.如果已单击它,我希望LineEdit显示用户选择的文件而不是默认选项.这是我的retranslate函数中适用的代码行:

Which would be saved as the filename in question and show up in my LineEdit. My problem is coming from trying to display a different file name in the LineEdit, depending on whether or not the browse push button has been clicked. If it has been clicked, I would like the LineEdit to show the user-selected file instead of the default option. Here are the applicable lines of code in my retranslate function:

def retranslateUi(self, ROIGUI):
    self.lineEdit.setText(_translate("ROIGUI", self.fileSelect(False), None))
    self.Browse.setText(_translate("ROIGUI", "Browse...", None))
    self.Browse.clicked.connect(self.fileSelect(True))

链接到以下功能.如您所见,这当前无法正常工作,因为在LineEdit中,tripped始终为False.很傻

Which link to the following function. As you can see, this is currently not working correctly because in the LineEdit, tripped is always False. Very silly.

def fileSelect(self,tripped):
    filenames = str(glob.glob('*.npy')[0])
    if tripped==True:
        filenames = QFileDialog.getOpenFileName()
        self.lineEdit.setText(_translate("ROIGUI", filenames, None))
    return filenames

我一直在尝试使它起作用的其他方法,但是我尝试的所有操作(a)从未在文件浏览后更新LineEdit,或(b)立即运行文件浏览而无需使用默认选项.有什么想法吗?我敢肯定,我只是没有办法做到这一点.

I've been trying different ways of getting this to work, but everything I tried either (a) never updates my LineEdit after file browsing, or (b) runs file browsing immediately without ever using the default option. Thoughts? I'm sure there's a way of doing this that I'm just not seeing.

先谢谢您.

已编辑以添加

Edited To Add

我想我已经解决了大部分问题-现在我的浏览"按钮已通过buttonGroup连接到一个整数,因此我的fileSelect看起来像这样:

I think I have fixed most of my problem - my Browse button is now connected via buttonGroup to an integer, so my fileSelect looks like this:

def fileSelect(self):
    signal = self.buttonGroup2.checkedId()
    if signal==-1:
        filenames = str(glob.glob('*.npy')[0])
    elif signal==1:
        filenames = QFileDialog.getOpenFileName()
        if (filenames.isNull()):
            filenames = str(glob.glob('*.npy')[0])
    return filenames

我的重新翻译"浏览按钮和lineEdit如下所示:

And my "retranslate" browse button and lineEdit look like this:

self.lineEdit.setText(_translate("ROIGUI", str(self.fileSelect()), None))

self.Browse.clicked.connect(self.fileSelect)

我唯一的问题是要更新lineEdit的文本;尽管使用浏览器选择后,使用中的文件本身会更新,但文本本身不会更新.帮助吗?

My only problem is getting the text of my lineEdit to update; although the file in use itself updates after it's selected with Browse, the text itself doesn't update. Help?

推荐答案

如果从QFileDialog文件名变量中选择了取消"按钮,则它为空QString,因此,您可以:

If the cancel button is selected from the QFileDialog filenames variable will be a null QString so, you could:

filenames = QFileDialog.getOpenFileName()
if (filenames.isNull()):
    self.lineEdit.setText(_translate("ROIGUI", filenames, None))
else:
    # The alternative code. Set the default value here to the QLineEdit.

参考: QFileDialog.getOpenFileName()

这篇关于PyQt文件浏览-设置默认选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆