0

File: untitled.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>43</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <layout class="QGridLayout" name="gridLayout">
   <item row="0" column="0">
    <widget class="QPushButton" name="pushButton">
     <property name="text">
      <string>Open choose folder window</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

File: untitled.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 43)
        self.gridLayout = QtWidgets.QGridLayout(Dialog)
        self.gridLayout.setObjectName("gridLayout")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.pushButton.setText(_translate("Dialog", "Open choose folder window"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

File: main.py

from PyQt5 import QtCore, QtGui, QtWidgets
import untitled
import sys
from sys import platform
import getpass
import os

class Main_program:

    def __init__(self):
        self.app = QtWidgets.QApplication(sys.argv)
        self.Dialog = QtWidgets.QDialog()
        self.ui = untitled.Ui_Dialog()
        self.ui.setupUi(self.Dialog)
        self.Dialog.show()

        self.ui.pushButton.clicked.connect(lambda state:self.open_choose_folder_window(state))

        sys.exit(self.app.exec_())

    def open_choose_folder_window(self,state):
        if platform == "linux" or platform == "linux2":
            # linux
            sys_username = getpass.getuser()
            try:
                dialog_dir = os.path.abspath("/home/"+sys_username+"/")
            except:
                dialog_dir = QtCore.QDir.currentPath()
        elif platform == "darwin":
            dialog_dir = QtCore.QDir.currentPath()
        elif platform == "win32":
            sys_username = getpass.getuser()
            try:
                dialog_dir = os.path.abspath("C:/Users/"+sys_username+"/")
            except:
                dialog_dir = QtCore.QDir.currentPath()
        else:
            dialog_dir = QtCore.QDir.currentPath()
        folder_path = QtWidgets.QFileDialog.getExistingDirectory(self.Dialog, 'Choose a folder',dialog_dir)
        if os.path.exists(folder_path)==True:
            print(folder_path)


if __name__ == "__main__":
    program = Main_program()

Running: python3.6 main.py and the clicking in the Choose folder button there an error (or warning) under linux (ubuntu 18.x)

Gtk-Message: 22:48:40.907: GtkDialog mapped without a transient parent. This is discouraged.

How to avoid this error/warning?

Thanks in advance, Chris Pappas

Chris P
  • 2,059
  • 4
  • 34
  • 68
  • That's a warning and AFAIK you can't get rid of that, unless you use the [`DontUseNativeDialog`](https://doc.qt.io/qt-5/qfiledialog.html#Option-enum) option in the arguments. – musicamante Mar 14 '22 at 21:06
  • PS: Use [QStandardPaths](https://doc.qt.io/qt-5/qstandardpaths.html). – musicamante Mar 15 '22 at 13:06

0 Answers0