1

I have connected a QPushButton to a method that call file dialog. The simplified code look like this:

def init_buttons(self):
    self.browse_button = QPushButton('&Browse')
    self.browse_button.clicked.connect(self.browse_file)

def browse_file(self):
    file_name = QFileDialog.getExistingDirectory()
    # Just for checking
    print(file_name)

Sometimes QFileDialog won't showing up. The process is indeed running, since the main class/widget doesn't response to my clicking. Sometimes it's showing up.

If QFileDialog doesn't show up, with pycharm, I have to stop and kill process to end the program. If I run the program directly from terminal, I have to manually end the running process to end the program. I can't figure out what causing this, since terminal not showing any exception or warning.

So, what is this?

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
Mas Bagol
  • 4,377
  • 10
  • 44
  • 72
  • I have tested your code on Arch Linux, and I cannot reproduce the problem. The directory path is printed as expected, and the process terminates normally. So either you are doing something else that you are not telling us about, or there is something specific about your setup that is causing the problem. Are you able to test your code on a different machine, or perhaps in a VM? – ekhumoro May 17 '15 at 17:06
  • PS: on my machine, I have an external drive which automatically goes into sleep mode to save power when it's not been in use for a while. If I try to open a file-dialog while it's sleeping, it takes 5-10 seconds to wake up, and blocks everything while it does so. Maybe there's a similar issue on your machine? – ekhumoro May 17 '15 at 17:15
  • Usually, when testing my code, I have chromium, pycharm and konsole running. I don't know if those processes disturb my code. But, it seems everyone can run my code just fine. Maybe there's something wrong with my system. Or maybe, because I use KDE plasma 5 which is based on qt5? – Mas Bagol May 18 '15 at 08:00
  • Probably because of this bug: https://bugs.kde.org/show_bug.cgi?id=350758 I'm working on it https://git.reviewboard.kde.org/r/125208/ – csslayer Sep 13 '15 at 00:20

2 Answers2

1

The parameters for the getExistingDirectory were wrong. Please try this. Also, I have added further information in my pull request.

import os

def browse_file(self):
    self.save_dir = QFileDialog.getExistingDirectory(self, 
                      "Open Save Directory", os.path.expanduser('~'))
    print(self.save_dir)
ham-sandwich
  • 3,975
  • 10
  • 34
  • 46
  • As said in comments on my question. I guess the problem is in my system. But I can't confirm it. And note, the code you've provided on github gives `Segmentation Fault, core dumped` on my system. [This answer](http://stackoverflow.com/questions/13654449/error-segmentation-fault-core-dumped/13654489#13654489) about `segmentation fault` make me believe there's something wrong outside my code. Anyway, thanks for your help – Mas Bagol May 18 '15 at 09:36
  • Sorry I couldn't have been of more help. I just tested on another OS there and no problems. Maybe use the Pyqt mailing list to see if anyone knows why. – ham-sandwich May 18 '15 at 10:05
0
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import (QMainWindow, QTextEdit,
    QAction,QMessageBox, QFileDialog, QApplication,QPushButton,QInputDialog,QLineEdit)
from PyQt5.QtGui import QIcon


class Example(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()


    def initUI(self):
        self.fileName=""
        self.text=""
        btn1 = QPushButton("Encrypt", self)
        btn1.clicked.connect(self.onBtn1)
        self.show()

    def onBtn1(self):

        self.fileName, _  = QFileDialog.getOpenFileName(self, 'Open file', '/Users/Jarvis/Desktop/')

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
ahumblenerd
  • 205
  • 3
  • 12
  • `self.fileName, _` what is this (the underscore) mean? Could you explain? – Mas Bagol May 17 '15 at 08:39
  • Err, not working. I tried adding `_` like you did there. The `not showing` is still happen. Even worse, `Segmentation fault (core dumped)` happens – Mas Bagol May 17 '15 at 08:46
  • What platform are you using ? The code ran fine mac osx yosemite. I didnt test it in linux . – ahumblenerd May 17 '15 at 09:03
  • You may remove the `_`. I ripped some of my previous code to give you that example. Please chek the platform that you are using . The code worked fine in Mac OSX yosemite and gave me the chosen file name. – ahumblenerd May 17 '15 at 09:16
  • If you are a linux user please make sure that you have installed all the necessary packages . – ahumblenerd May 17 '15 at 09:16
  • This is all completely irrelevant, because `getExistingDirectory` will never return a tuple - it will only ever return a directory path, no matter what version of PyQt or platform you are using. – ekhumoro May 17 '15 at 16:51