你如何让这个按钮集中在wxPython ScrolledMessageDialog中?

时间:2018-03-12 19:44:59

标签: python-3.x user-interface wxpython

我使用一个简单的函数在wxPython 4应用程序中显示错误消息。它显示一条消息和一个" OK"它下面的按钮:

dlg = wx.lib.dialogs.ScrolledMessageDialog(self, msg, "Error")
dlg.ShowModal()

问题:当出现对话框时,输入焦点在消息上,而不是按钮(至少在macOS上)。这意味着击中键盘上的返回什么也不做。如果它与点击OK按钮具有相同的效果会更方便。

如果出现此对话框,如何在默认情况下让按钮获得焦点?

2 个答案:

答案 0 :(得分:0)

鉴于您的评论,为什么不创建自己的对话框。 有点像:

import wx
class MyFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, "Custom Dialog",size=(200,100))
        self.panel = wx.Panel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.button = wx.Button(self.panel, label="Dialog")
        sizer.Add(self.button, 0, wx.EXPAND | wx.ALL, 10)
        self.panel.SetSizer(sizer)
        self.Bind(wx.EVT_BUTTON, self.OnButton)

    def OnButton(self,event):
        msg = "Please select one of the options below\n"
        msg += "Help is available if you have encountered this issue\n"
        dlg = MessageDialog(self.panel,msg,"Message Dialog Heading")
        dlg.ShowModal()
        if dlg.result == wx.ID_HELP:
            print ("Help text was displayed")
        elif dlg.result == wx.ID_OK:
            print ("Ok was Selected")
        elif dlg.result == wx.ID_CANCEL:
            print ("Cancel was Selected")
        else:
            print ("Dialog Exited")
        dlg.Destroy()

class MessageDialog(wx.Dialog):
    def __init__(self, parent, message, title):
        wx.Dialog.__init__(self, None, -1, title)
        ok = wx.Button(self, wx.ID_OK)
        no = wx.Button(self, wx.ID_CANCEL)
        help = wx.Button(self, wx.ID_HELP)
        self.text = wx.StaticText(self, -1, message)
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add(self.text,1,0,0)
        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        hbox2.Add(ok,0,0,0)
        hbox2.Add(no,0,0,0)
        hbox2.Add(help,0,0,0)
        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(hbox, 0, wx.ALIGN_CENTER)
        vbox.Add(hbox2, 0, wx.ALIGN_CENTER)
        self.SetSizer(vbox)
        self.Bind(wx.EVT_BUTTON,self.OnButton)
        self.Bind(wx.EVT_CLOSE,self.OnExit)
        self.result = None
        help.SetFocus()

    def OnButton(self,event):
        self.result = event.GetId()
        if event.GetId() == wx.ID_HELP:
            wx.MessageBox('This is my help text dialog', 'Help',wx.OK | wx.ICON_INFORMATION)
        self.Destroy()

    def OnExit(self,event):
        self.result = None
        self.Destroy()

app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()

答案 1 :(得分:0)

创建自定义对话框窗口时,您需要使用wx.Button.SetDefault()来将焦点设置在按钮上,至少在macOS上。例如:

class MyCustomDialog(wx.Dialog):
    """"""
    def __init__(self, parent, title=''):
        """"""
        super().__init__(parent, title=title)
        #region ------------------------------------------------------ Widgets
        #--> StaticText
        self.stText = wx.StaticText(
            self, 
            label="The label"
        )
        #--> TextCtrl
        self.tcText = wx.TextCtrl(
            self, 
            value="The values",
            size=(300, 23),
        )
        #--> CheckBox
        self.cbLogP  = wx.CheckBox(self, label='Check A')
        self.cbCorrP = wx.CheckBox(self, label='Check B')
        #--> Buttons
        self.btnOK     = wx.Button(self, wx.ID_OK)
        self.btnCancel = wx.Button(self, wx.ID_CANCEL)

        #### The line below is the one that set the focus on the Ok button ####
        self.btnOK.SetDefault()

        #endregion --------------------------------------------------- Widgets

        #region ------------------------------------------------------- Sizers
        self.sizer = wx.BoxSizer(wx.VERTICAL)

        self.sizer.Add(self.stText, 0, wx.ALL, 5)
        self.sizer.Add(self.tcText, 0, wx.ALL, 5)

        self.box_check = wx.BoxSizer(wx.HORIZONTAL)
        self.box_check.Add(self.cbLogP, 0, wx.ALL, 5)
        self.box_check.Add(self.cbCorrP, 0, wx.ALL, 5)
        self.sizer.Add(self.box_check, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALL, 5)

        self.sizer_btn = wx.StdDialogButtonSizer()
        self.sizer_btn.AddButton(self.btnOK)
        self.sizer_btn.AddButton(self.btnCancel)
        self.sizer_btn.Realize()
        self.sizer.Add(self.sizer_btn, 0, wx.ALIGN_RIGHT|wx.ALL, 5)

        self.SetSizer(self.sizer)
        self.sizer.Fit(self)
        #endregion --------------------------------------------------- Sizers
    #---
#---