wxFilePickerCtrl (and FileDirCtrl) Opens Twice...

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
worn_out
In need of some credit
In need of some credit
Posts: 8
Joined: Sat Apr 23, 2022 5:31 pm

wxFilePickerCtrl (and FileDirCtrl) Opens Twice...

Post by worn_out »

OSX Monterey
wxWidgets, wxFormBujilder, wxPython

Problem: wxFilePickerCtrl opens twice (FileDirCtrl also does same thing)

From reading info on the widget(s), I understand it wants a Dir and File but, that's extra steps. How to have only one panel-popup where the file selection also includes it's path (like it does once the File is selected).

Perhaps I misunderstand and not using it correctly...

The code snippet below is one version of trying various changes but nothing yet gets around the two panel popups. (sure, not using show.modal stops it but, then the variables for file name are null)

Code: Select all

    def GetFile( self, event ):
        global hum, fileNameString
        print("get code")
        openFileDialog = wx.FileDialog(frame, "", "", "",
                                       "Python files (*.py)|*.py",
                                       wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
        openFileDialog.ShowModal()
        fileNameString = str(openFileDialog.GetPath())
        # openFileDialog.Destroy()
        print("FileName is: " + str(fileNameString))  # get filenamestring ok!
        hum = str(fileNameString)
        return hum, fileNameString

    def PublishTheFile( self, event ):
     #   global hum, fileNameString
        print("publish" + hum)
        self.m_textCtrl1.SetLabel("python3 -m PyInstaller -F -w --onefile " + hum)
        print(hum)
User avatar
doublemax
Moderator
Moderator
Posts: 17148
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxFilePickerCtrl (and FileDirCtrl) Opens Twice...

Post by doublemax »

https://discuss.wxpython.org/ may be a better place to ask wxPython related questions.

However: When exactly does the second FileDialog appear? IOW: Does the ShowModal() call already open both? If you select two different files each time, which one do you get from GetPath()

Are you sure that you don't call GetFile() twice?

You also mentioned wxFilePickerCtrl, but it doesn't appear in the code you posted. Was that a typo?
If you use wxFilePickerCtrl, you don't have to open a FileDialog yourself, the picker control would handle that.
Use the source, Luke!
worn_out
In need of some credit
In need of some credit
Posts: 8
Joined: Sat Apr 23, 2022 5:31 pm

Re: wxFilePickerCtrl (and FileDirCtrl) Opens Twice...

Post by worn_out »

Thank you for response...
doublemax wrote: Thu May 05, 2022 4:26 pm may be a better place to ask wxPython related questions
Well, it might be a better place but, I figured this is a wxWidget and not the actual Python programming. Perhaps I'm confused. I once posted a question (at the wxPython forum) about a wxWidget and response was to post the question at wxWidget forum...
doublemax wrote: Thu May 05, 2022 4:26 pm When exactly does the second FileDialog appear?
doublemax wrote: Thu May 05, 2022 4:26 pm If you select two different files each time, which one do you get from GetPath()
It appears immediately after okaying the first selection panel and, cannot select two items in the panels.
doublemax wrote: Thu May 05, 2022 4:26 pm Are you sure that you don't call GetFile() twice?
Well, here's the full code except for the imports and comments at the top of the file...

Code: Select all

# ------------------------------------- DECLARATIONS --------------------------

class MainFrame3( AppMaker3.MainFrame3 ):
#    def __init__( self, parent ):
#        AppMaker3.MainFrame3.__init__( self, parent )

    def GetFile( self, event ):
        global hum, fileNameString
        print("get code")
        openFileDialog = wx.FileDialog(frame, "", "", "",
                                       "Python files (*.py)|*.py",
                                       wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
        openFileDialog.ShowModal()
        fileNameString = str(openFileDialog.GetPath())
        # openFileDialog.Destroy()
        print("FileName is: " + str(fileNameString))  # get filenamestring ok!
        hum = str(fileNameString)
        return hum, fileNameString

    def PublishTheFile( self, event ):
     #   global hum, fileNameString
        print("publish" + hum)
        self.m_textCtrl1.SetLabel("python3 -m PyInstaller -F -w --onefile " + hum)
        print(hum)

    def Exit( self, event ):
        # TODO: Implement Exit
        print("Bye!")
        self.Destroy()
        os._exit(0)


# -------------------------------------- CLOSING ------------------------------
app = wx.App(False)           # mandatory in wx, creates an app, False
frame = MainFrame3(None)      # create an object
frame.Show(True)              # show the frame
app.MainLoop()                # start the applications
GIF video below... I hope it shows up (not sure about posting them at this site...)
Attachments
FilePicker.gif
User avatar
doublemax
Moderator
Moderator
Posts: 17148
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxFilePickerCtrl (and FileDirCtrl) Opens Twice...

Post by doublemax »

There are no documented cases (that i know of) where wxFileDialog::ShowModal() showed two dialogs. So this is either a strange bug in your code or in wxPython (but not in the C++ base of wxWidgets).
Well, here's the full code except for the imports and comments at the top of the file...
I know very little about Python, but i see no place where GetFile() or PublishTheFile() are called.

As you mentioned wxFilePicker before, i'll take a wild guess here: You have a wxFilePickerCtrl, and in the EVT_FILEPICKER_CHANGED event handler you call GetFile(). This would explain the two file dialogs. If that's the case, you don't need your own FileDialog. Just use wxFilePickerCtrl::GetPath() to retrieve the file path.
Use the source, Luke!
worn_out
In need of some credit
In need of some credit
Posts: 8
Joined: Sat Apr 23, 2022 5:31 pm

Re: wxFilePickerCtrl (and FileDirCtrl) Opens Twice...

Post by worn_out »

It's all there in the code... GetFile, Publish and Exit are the only def's

(the actual definition of the GUI stuff is in another file (and, nothing is duplicated or called from there).

(Click the GIF video to play it...)
User avatar
doublemax
Moderator
Moderator
Posts: 17148
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxFilePickerCtrl (and FileDirCtrl) Opens Twice...

Post by doublemax »

There must be something missing.
the actual definition of the GUI stuff is in another file
Please post this.

I saw the video but it doesn't help.
Use the source, Luke!
worn_out
In need of some credit
In need of some credit
Posts: 8
Joined: Sat Apr 23, 2022 5:31 pm

Re: wxFilePickerCtrl (and FileDirCtrl) Opens Twice...

Post by worn_out »

The video was only to show there are two panel popups...

Here's the code for the GUI file (generated by FormBuilder)

Code: Select all

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

###########################################################################
## Python code generated with wxFormBuilder (version 3.10.1-0-g8feb16b)
## http://www.wxformbuilder.org/
##
## PLEASE DO *NOT* EDIT THIS FILE!
###########################################################################

import wx
import wx.xrc

###########################################################################
## Class MainFrame3
###########################################################################

class MainFrame3 ( wx.Frame ):

	def __init__( self, parent ):
		wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"App-Maker3", pos = wx.DefaultPosition, size = wx.Size( 607,284 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

		self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )

		bSizer1 = wx.BoxSizer( wx.VERTICAL )

		self.m_panel1 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
		self.m_panel1.SetBackgroundColour( wx.Colour( 0, 0, 51 ) )

		bSizer2 = wx.BoxSizer( wx.VERTICAL )

		self.m_staticText1 = wx.StaticText( self.m_panel1, wx.ID_ANY, u"Code --> App", wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_CENTER_HORIZONTAL )
		self.m_staticText1.Wrap( -1 )

		self.m_staticText1.SetFont( wx.Font( 18, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, True, "Lucida Grande" ) )
		self.m_staticText1.SetForegroundColour( wx.Colour( 255, 255, 255 ) )

		bSizer2.Add( self.m_staticText1, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )

		self.m_staticline1 = wx.StaticLine( self.m_panel1, wx.ID_ANY, wx.DefaultPosition, wx.Size( -1,20 ), wx.LI_HORIZONTAL )
		self.m_staticline1.SetForegroundColour( wx.Colour( 230, 230, 230 ) )
		self.m_staticline1.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_3DLIGHT ) )

		bSizer2.Add( self.m_staticline1, 0, wx.EXPAND |wx.ALL, 5 )

		fgSizer1 = wx.FlexGridSizer( 1, 2, 0, 1 )
		fgSizer1.SetFlexibleDirection( wx.BOTH )
		fgSizer1.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_ALL )

		self.m_staticText2 = wx.StaticText( self.m_panel1, wx.ID_ANY, u"Dir and File", wx.DefaultPosition, wx.DefaultSize, 0 )
		self.m_staticText2.Wrap( -1 )

		self.m_staticText2.SetForegroundColour( wx.Colour( 255, 255, 255 ) )
		self.m_staticText2.SetMinSize( wx.Size( 80,-1 ) )

		fgSizer1.Add( self.m_staticText2, 0, wx.ALL, 5 )

		self.m_filePicker1 = wx.FilePickerCtrl( self.m_panel1, wx.ID_ANY, wx.EmptyString, u"Select a file", u"*.*", wx.DefaultPosition, wx.DefaultSize, wx.FLP_DEFAULT_STYLE )
		self.m_filePicker1.SetMinSize( wx.Size( 475,-1 ) )
		self.m_filePicker1.SetMaxSize( wx.Size( 475,-1 ) )

		fgSizer1.Add( self.m_filePicker1, 0, wx.ALL, 5 )


		bSizer2.Add( fgSizer1, 1, wx.EXPAND, 5 )

		bSizer3 = wx.BoxSizer( wx.VERTICAL )

		fgSizer2 = wx.FlexGridSizer( 1, 3, 0, 20 )
		fgSizer2.SetFlexibleDirection( wx.BOTH )
		fgSizer2.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )


		fgSizer2.Add( ( 350, 0), 1, wx.EXPAND, 5 )

		self.m_button1 = wx.Button( self.m_panel1, wx.ID_ANY, u"Publish", wx.DefaultPosition, wx.DefaultSize, 0 )
		fgSizer2.Add( self.m_button1, 0, wx.ALL, 5 )

		self.m_button2 = wx.Button( self.m_panel1, wx.ID_ANY, u"Quit", wx.DefaultPosition, wx.DefaultSize, 0 )
		fgSizer2.Add( self.m_button2, 0, wx.ALL, 5 )


		bSizer3.Add( fgSizer2, 1, wx.EXPAND, 5 )

		fgSizer3 = wx.FlexGridSizer( 1, 2, 0, 0 )
		fgSizer3.SetFlexibleDirection( wx.HORIZONTAL )
		fgSizer3.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_NONE )

		self.m_staticText3 = wx.StaticText( self.m_panel1, wx.ID_ANY, u"The String:", wx.DefaultPosition, wx.DefaultSize, 0 )
		self.m_staticText3.Wrap( -1 )

		self.m_staticText3.SetForegroundColour( wx.Colour( 255, 255, 255 ) )

		fgSizer3.Add( self.m_staticText3, 0, wx.ALL, 5 )

		self.m_textCtrl1 = wx.TextCtrl( self.m_panel1, wx.ID_ANY, u"<text>", wx.DefaultPosition, wx.Size( 500,60 ), 0 )
		self.m_textCtrl1.SetForegroundColour( wx.Colour( 33, 255, 6 ) )
		self.m_textCtrl1.SetBackgroundColour( wx.Colour( 0, 0, 0 ) )
		self.m_textCtrl1.SetMinSize( wx.Size( 500,60 ) )

		fgSizer3.Add( self.m_textCtrl1, 0, wx.ALL, 5 )


		bSizer3.Add( fgSizer3, 1, wx.EXPAND, 5 )


		bSizer2.Add( bSizer3, 1, wx.EXPAND, 5 )


		self.m_panel1.SetSizer( bSizer2 )
		self.m_panel1.Layout()
		bSizer2.Fit( self.m_panel1 )
		bSizer1.Add( self.m_panel1, 1, wx.EXPAND |wx.ALL, 5 )


		self.SetSizer( bSizer1 )
		self.Layout()

		self.Centre( wx.BOTH )

		# Connect Events
		self.m_filePicker1.Bind( wx.EVT_FILEPICKER_CHANGED, self.GetFile )
		self.m_button1.Bind( wx.EVT_BUTTON, self.PublishTheFile )
		self.m_button2.Bind( wx.EVT_BUTTON, self.Exit )

	def __del__( self ):
		pass


	# Virtual event handlers, override them in your derived class
	def GetFile( self, event ):
		event.Skip()

	def PublishTheFile( self, event ):
		event.Skip()

	def Exit( self, event ):
		event.Skip()

User avatar
doublemax
Moderator
Moderator
Posts: 17148
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxFilePickerCtrl (and FileDirCtrl) Opens Twice...

Post by doublemax »

Code: Select all

self.m_filePicker1.Bind( wx.EVT_FILEPICKER_CHANGED, self.GetFile )
That's all i needed to see and it confirms my wild guess.

Remove all FileDialog related code from GetPath()

As i've never written (only read) Python code, i'm not sure if this is correct:
Change

Code: Select all

fileNameString = str(openFileDialog.GetPath())
to:

Code: Select all

fileNameString = str(m_filePicker1.GetPath())
But you should get the idea.
Use the source, Luke!
worn_out
In need of some credit
In need of some credit
Posts: 8
Joined: Sat Apr 23, 2022 5:31 pm

Re: wxFilePickerCtrl (and FileDirCtrl) Opens Twice...

Post by worn_out »

Thank you for your effort/help.

Unfortunately, no solution, yet. But, I'll keep working on it and will perhaps mosey on over to wxPython forum.

I've seen sever similar posts on the problem at various sites but, never a solution. (I hate Python! and prefer Java/C++ but, present need is for it to be done in Python)...

Thanks again...
User avatar
doublemax
Moderator
Moderator
Posts: 17148
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxFilePickerCtrl (and FileDirCtrl) Opens Twice...

Post by doublemax »

worn_out wrote: Thu May 05, 2022 10:05 pm Unfortunately, no solution, yet. But, I'll keep working on it and will perhaps mosey on over to wxPython forum
What did you try? The error is obvious and the solution should be too.

It's a built-in function of wxFilePickerCtrl to open a wxFileDialog and update its internal variable with the path of the file. So you don't have to open a wxFileDialog in your code, just get the path from wxFilePickerCtrl.
Use the source, Luke!
worn_out
In need of some credit
In need of some credit
Posts: 8
Joined: Sat Apr 23, 2022 5:31 pm

Re: wxFilePickerCtrl (and FileDirCtrl) Opens Twice...

Post by worn_out »

Yes, error and solution are 'somewhat' obvious but...

Your observations are consistent with mine as well as the solutions I explored. There's not a lot going on with this, it's simple enough and, most of the time, error leads me to think that 'whatever' the hidden variable/holder is for the file/path, it is not available or known (to me).
Logic suggests it's "m_filePicker1" but, it errors. And, I tried wxFilePickerCtrl, as well.

FYI - I can cleanly do this with Python's Tkinter's file chooser but, not with wx.

Without calling ShowModal, no variables get loaded. And, without declaring openFileDialog, ShowModal errors... (duh).

I know FormBuilder still has bugs and I can hand-code the GUI stuff but, I'm pushing myself to avoid that.

I'm going to think about this (and do some more homework)...

Thanks again for your help and quick response...

Below is a typical error msg for one variation of tweaking... Shows a Not defined because Show.Modal was commented out...

Reminds me of Catch 22
Attachments
Screen Shot 2022-05-05 at 4.08.41 PM.png
Screen Shot 2022-05-05 at 4.08.41 PM.png (57.15 KiB) Viewed 176 times
User avatar
doublemax
Moderator
Moderator
Posts: 17148
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxFilePickerCtrl (and FileDirCtrl) Opens Twice...

Post by doublemax »

Try "self.m_filePicker1"
Use the source, Luke!
worn_out
In need of some credit
In need of some credit
Posts: 8
Joined: Sat Apr 23, 2022 5:31 pm

SOLVED Re: wxFilePickerCtrl (and FileDirCtrl) Opens Twice...

Post by worn_out »

Using 'self' was included in several attempts.

However, the trick was to eliminate show.modal, and openfiledialog And, to use 'self' at the same time.. I'm certain I tried it several times but at age 73 I have no excuse beyond seeing what I want to see. I'm embarrassed... :oops:

THANK YOU!
Post Reply