943,982 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 3733
  • Python RSS
Nov 14th, 2006
0

popup menu does not appear if wx.FileDialog is activated beforehand

Expand Post »
Hello,

I am having a problem showing a popup menu in my wxpython script on fedora 5 (fc5); this problem does not occur on xp. I am using wxPython-2.6.3.2-1.fc5, unicode version.

I need to add an item to a popup menu after the user selects a file using wx.FileDialog, and then need to show the popup menu immediately after the file selection to inform the user of the files that have been selected thus far.

The popup menu will not show on fc5 if I first display the wx.FileDialog control - if I avoid wx.FileDialog the popup menu appears without a problem.

[php]
#!/usr/bin/python

import wx, time, os

thislimit = 5
thispopupmenu = None


def MakeThePopup(self):

global thispopupmenu

menu = wx.Menu()

ids = []

for ii in range(thislimit):
mnuid = wx.NewId()
ids.append(mnuid)
menu.Append(mnuid, "File " + str(ii+1))

menu.AppendSeparator()
add_popupmenu_id = 10102
ids.append(add_popupmenu_id)
menu.Append(add_popupmenu_id, "Add a File")


for daid in ids:
wx.EVT_MENU(self, daid, PopupClicked)

thispopupmenu = menu

### --- end of MakeThePopup function --- ###


def PopupClicked(event):

# get the text of the menu item that was clicked
mnulabel = event.GetEventObject().GetLabel(event.GetId())

# let's be certain that a menu click is being processed
daframe.SetTitle(mnulabel + ' - ' + str(time.time()))

# activate wx.FileDialog if appropriate popup menu item is clicked
if (mnulabel == "Add a File"):
global thislimit
thislimit = thislimit + 1
daframe.SetTitle(mnulabel)
dlg = wx.FileDialog(daframe, message="Select a File:",\
defaultDir=os.getcwd(), defaultFile="",\
wildcard="All files (*.*)|*.*",style=wx.OPEN)

### uncomment the following 2 lines of code and
### the popup menu will not appear on fc5

#if dlg.ShowModal() == wx.ID_OK:
# thisresult = "a file was selected"

### ######################

# make the poup menu
MakeThePopup(daframe.button)

# show the popup menu over the button
daframe.button.PopupMenu(thispopupmenu,wx.Point(5,5))
# the popup fails to show if a wx.FileDialog has
# been activated previously

return True


event.Skip()

### --- end of PopupClicked function --- ###


class DisFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)

# make a panel with a button
panel = wx.Panel(self)
self.button = wx.Button(panel, -1, "Show popup menu")
self.button.Bind(wx.EVT_BUTTON, self.ButtonClicked)
bs = wx.BoxSizer(wx.HORIZONTAL)
bs.Add(self.button,0,wx.ALL,30)
panel.SetSizer(bs)

MakeThePopup(self)


def ButtonClicked(self, event):

# make the popup
MakeThePopup(self)

# show the popup over the button
self.button.PopupMenu(thispopupmenu)

event.Skip()

### --- end of DisFrame class --- ###



if __name__ == '__main__':
app = wx.App()
daframe = DisFrame(None)
daframe.Show()
app.MainLoop()


[/php]

The popup menu appears if running the above script on fc5.

However, if I uncomment the 2 lines of code that show the wx.FileDialog, the popup fails to appear on fc5 and I need to click 'Stop Executing' after closing the application if running the script through SciTE.

How can I show the popup menu after activating a wx.FileDialog control ?

Any help will be appreciated. Thanks.

_
Last edited by dunderhead; Nov 14th, 2006 at 6:36 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dunderhead is offline Offline
20 posts
since Sep 2006
Nov 14th, 2006
0

Re: popup menu does not appear if wx.FileDialog is activated beforehand

You might have to destroy the dialog after it has been used:
Python Syntax (Toggle Plain Text)
  1. ### uncomment the following 2 lines of code and
  2. ### the popup menu will not appear on fc5
  3.  
  4. if dlg.ShowModal() == wx.ID_OK:
  5. # do something
  6. thisresult = "a file was selected"
  7.  
  8. dlg.Destroy()
  9.  
  10. ### ######################
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Nov 14th, 2006
0

Re: popup menu does not appear if wx.FileDialog is activated beforehand

Thanks for the tip bumsfeld.

dlg.Destroy() should have been there (silly oversight on my part).

Unfortunately the popup menu still fails to appear on fc5 if activating wx.FileDialog and then destroying the dialog.

The program seems to 'hang' (though it's still functional) and requires using SciTE's 'Stop Executing' command to completely kill the running process.

Very puzzling.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dunderhead is offline Offline
20 posts
since Sep 2006
Nov 14th, 2006
0

Re: popup menu does not appear if wx.FileDialog is activated beforehand

I don't have Fedora, so I can't see the effect, but check if Refresh() or Update() will help ...
python Syntax (Toggle Plain Text)
  1. ### uncomment the following 2 lines of code and
  2. ### the popup menu will not appear on fc5
  3.  
  4. if dlg.ShowModal() == wx.ID_OK:
  5. # do something
  6. thisresult = "a file was selected"
  7.  
  8. dlg.Destroy()
  9. daframe.Refresh() # next event
  10. daframe.Update() # immediate
  11.  
  12. ### ######################
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 14th, 2006
0

Re: popup menu does not appear if wx.FileDialog is activated beforehand

Followed your advice vegaseat, but still no luck - popup menu does not show on fc5 if I use a wx.FileDialog:

[php]
if dlg.ShowModal() == wx.ID_OK:
thisresult = "a file was selected"
dlg.Destroy()
daframe.Refresh()
daframe.Update()
[/php]

My feable attempts to debug the problem have revealed that the popup menu is apparently created without a problem, but that the problem occurs when the program tries to show it above the button:

[php]
# make the popup menu
MakeThePopup(daframe.button)
daframe.SetTitle('popup menu is created')

# show the popup menu over the button
daframe.button.PopupMenu(thispopupmenu,wx.Point(5,5))
daframe.SetTitle('showing popup menu')
[/php]
The above always produces a window title that reads 'popup menu is created' when a wx.FileDialog is used on fc5.

The title never reads 'showing popup menu' when using a file dialog box on fc5, indicating that the problem lies with the program's attempt to display the popup menu.

Are we any closer at finding a solution ?

_
Last edited by dunderhead; Nov 14th, 2006 at 11:58 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dunderhead is offline Offline
20 posts
since Sep 2006
Nov 15th, 2006
0

Re: popup menu does not appear if wx.FileDialog is activated beforehand

I have made a simpler version of this program in the hope that someone will be able to offer a solution.

This version simply displays a message box after clicking a button, and then within the same function attempts to display a popup menu.

The popup menu will only appear on fc5 if you avoid displaying the message box:

[php]
#!/usr/bin/python

import wx, random


def PopupClicked(event):

daframe.SetTitle(str(random.randint(0,100)))
event.Skip()



class DisFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)

# make a panel with a button
panel = wx.Panel(self)
self.button = wx.Button(panel, -1, "Show popup menu")
self.button.Bind(wx.EVT_BUTTON, self.ButtonClicked)
bs = wx.BoxSizer(wx.HORIZONTAL)
bs.Add(self.button,0,wx.ALL,30)
panel.SetSizer(bs)
random.seed()


def ButtonClicked(self, event):

dlg = wx.MessageDialog(daframe,
message='a message',caption=' ',\
style=wx.OK|wx.ICON_INFORMATION)

# the popup menu will not appear if
# this message dialog is shown
dlg.ShowModal()
dlg.Destroy()
# comment-out the above 2 lines and
# the popup menu will appear

# make and show a popup menu
menu = wx.Menu()
menu.Append(101, " menu item 1 ")
wx.EVT_MENU(self, 101, PopupClicked)
self.PopupMenu(menu)
menu.Destroy()

event.Skip()



if __name__ == '__main__':
app = wx.App()
daframe = DisFrame(None)
daframe.Show()
app.MainLoop()

[/php]
The same problem occurs whether you display a message box, color dialog or file dialog - the popup menu will only appear if you avoid displaying any type of dialog control beforehand.

How can I display the popup menu on fc5 after displaying the message box ?

_
Last edited by dunderhead; Nov 15th, 2006 at 1:02 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dunderhead is offline Offline
20 posts
since Sep 2006
Nov 15th, 2006
0

Re: popup menu does not appear if wx.FileDialog is activated beforehand

An initial observation, your popup appears at the location of the dialog box, not in the frame. Is it associated with the wrong parent?
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 15th, 2006
0

Re: popup menu does not appear if wx.FileDialog is activated beforehand

Thanks for the response vegaseat.

The popup fails to show whether I associate it with the main frame or the button (self.button).

I've experienced other problems with the popup menu on fc5 in relation to assigning id numbers via wx.NewId() during the creation of a popup, whereas there were no problems on xp.

Extremely frustrating. Thanks for the input.

_
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dunderhead is offline Offline
20 posts
since Sep 2006
Nov 26th, 2006
0

Re: popup menu does not appear if wx.FileDialog is activated beforehand

The following solution was provided by Robin Dunn on a different forum, and makes use of wx.CallAfter():

[PHP]
#!/usr/bin/python

import wx, random

def PopupClicked(event):
daframe.SetTitle(str(random.randint(0,100)))
event.Skip()

class DisFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
# make a panel with a button
panel = wx.Panel(self)
self.button = wx.Button(panel, -1, "Show popup menu")
self.button.Bind(wx.EVT_BUTTON, self.ButtonClicked)
bs = wx.BoxSizer(wx.HORIZONTAL)
bs.Add(self.button,0,wx.ALL,30)
panel.SetSizer(bs)
random.seed()

def ButtonClicked(self, event):
dlg = wx.MessageDialog(daframe,
message='a message',caption=' ',\
style=wx.OK|wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
wx.CallAfter(self.DoPopupMenu)


def DoPopupMenu(self):
menu = wx.Menu()
menu.Append(101, " menu item 1 ")
wx.EVT_MENU(self, 101, PopupClicked)
self.button.PopupMenu(menu, wx.Point(5,5))
menu.Destroy()


if __name__ == '__main__':
app = wx.App()
daframe = DisFrame(None)
daframe.Show()
app.MainLoop()

[/PHP]


_
Last edited by dunderhead; Nov 26th, 2006 at 5:50 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dunderhead is offline Offline
20 posts
since Sep 2006
Nov 27th, 2006
0

Re: popup menu does not appear if wx.FileDialog is activated beforehand

Thanks for letting us know, interesting solution!
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Loop question
Next Thread in Python Forum Timeline: pygame level problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC