welcome to the fest
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

ESC key in wx.Dialog doesn't trigger button with ID_CANCEL

 
Post new topic   Reply to topic    Sinfest Forum Index -> Casual Chat
View previous topic :: View next topic  
Author Message
Michael



Joined: 09 Jul 2006
Posts: 10749

PostPosted: Tue Dec 02, 2014 8:32 pm    Post subject: ESC key in wx.Dialog doesn't trigger button with ID_CANCEL Reply with quote

According to the docs, dialogs created in wxPython and shown with ShowModal should handle the escape (ESC) key by searching for a button with ID_CANCEL and simulating a click (i.e. triggering its EVT_BUTTON event).

This was working in all but one of my dialogs. After a lot of debugging, I discovered that the cancel button - or any other button! - should have focus. In other words, as long as I call .SetFocus() on one of the buttons I create, the ESC key works fine.

Does anybody know what's going on here? Do dialogs displayed with ShowModal() not automatically get focus? Should they? Or am I misunderstanding something?

In the example code below, uncomment the line b.SetFocus() to see the difference:

Code:
import wx
class MainWindow(wx.Frame):
    def __init__(self, parent):
        super(MainWindow, self).__init__(parent)
        self.Show()
        d = SomeDialog(self)
        d.ShowModal()
        self.Destroy()
class SomeDialog(wx.Dialog):
    def __init__(self, parent):
        super(SomeDialog, self).__init__(parent)
        button = wx.Button(self, wx.ID_CANCEL, 'Cancel')
        button.Bind(wx.EVT_BUTTON, self.action_cancel)
        #button.SetFocus()
    def action_cancel(self, e):
        self.EndModal(wx.ID_CANCEL)
if __name__ == '__main__':
    app = wx.App(False)
    frame = MainWindow(None)
    app.MainLoop()
Back to top
View user's profile Send private message
Michael



Joined: 09 Jul 2006
Posts: 10749

PostPosted: Tue Dec 02, 2014 8:33 pm    Post subject: Reply with quote

I am really interested in hearing your opinions on this.
Back to top
View user's profile Send private message
mouse



Joined: 10 Jul 2006
Posts: 17600
Location: under the bed

PostPosted: Tue Dec 02, 2014 11:36 pm    Post subject: Reply with quote

my opinion is that you should not be using language like that on this forum.
_________________
aka: neverscared!
Back to top
View user's profile Send private message
Thy Brilliance



Joined: 09 Jul 2006
Posts: 3661
Location: Relative

PostPosted: Sun Dec 07, 2014 3:47 am    Post subject: Re: ESC key in wx.Dialog doesn't trigger button with ID_CANC Reply with quote

Michael wrote:
According to the docs, dialogs created in wxPython and shown with ShowModal should handle the escape (ESC) key by searching for a button with ID_CANCEL and simulating a click (i.e. triggering its EVT_BUTTON event).

This was working in all but one of my dialogs. After a lot of debugging, I discovered that the cancel button - or any other button! - should have focus. In other words, as long as I call .SetFocus() on one of the buttons I create, the ESC key works fine.

Does anybody know what's going on here? Do dialogs displayed with ShowModal() not automatically get focus? Should they? Or am I misunderstanding something?

In the example code below, uncomment the line b.SetFocus() to see the difference:

Code:
import wx
class MainWindow(wx.Frame):
    def __init__(self, parent):
        super(MainWindow, self).__init__(parent)
        self.Show()
        d = SomeDialog(self)
        d.ShowModal()
        self.Destroy()
class SomeDialog(wx.Dialog):
    def __init__(self, parent):
        super(SomeDialog, self).__init__(parent)
        button = wx.Button(self, wx.ID_CANCEL, 'Cancel')
        button.Bind(wx.EVT_BUTTON, self.action_cancel)
        #button.SetFocus()
    def action_cancel(self, e):
        self.EndModal(wx.ID_CANCEL)
if __name__ == '__main__':
    app = wx.App(False)
    frame = MainWindow(None)
    app.MainLoop()


Describe the evil terrorist activities this code will support so that I can decide whether I can help you.

(Personal or business)
Back to top
View user's profile Send private message
Usagi Miyamoto



Joined: 09 Jul 2006
Posts: 2230
Location: wish you were here

PostPosted: Sun Dec 07, 2014 11:36 am    Post subject: If you're really interested in opinions, Reply with quote

I think it's best to always explicitly set focus to a child control in your dialog, as a matter of coding style. The one you want the user to interact with first, generally, or your default button. (Bonus: it fixes your bug as an unintended side-effect.) Otherwise, your dialogs and container controls usually pass focus on to their first child, which may or may not be what you want.
_________________
The reward for a good life is a good life.
Back to top
View user's profile Send private message
Michael



Joined: 09 Jul 2006
Posts: 10749

PostPosted: Tue Dec 09, 2014 10:58 pm    Post subject: Reply with quote

<3
Back to top
View user's profile Send private message
Martian Kyo



Joined: 12 Jul 2006
Posts: 1534

PostPosted: Wed Dec 10, 2014 7:57 am    Post subject: Reply with quote

I think this is a philosophical issue.

Modal windows are initially used as notifications, as they disable access to parent windows. These are for important notifications something that the user needs to take notice of (or in technical terms SetFocus()) and take action. They indeed do have focus, but they should not be allowed to be escaped.

I must go now, got a meeting.

If you need further clarification of my point I will gladly expend on it at a later date.
_________________
Tsosm! Hydrogen fuel and Grumpy Cat coffee
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Sinfest Forum Index -> Casual Chat All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group