【问题标题】:How to set Focus after calling MessageBox from a Dialog?从对话框调用 MessageBox 后如何设置焦点?
【发布时间】:2021-11-23 15:59:34
【问题描述】:

使用 wxPython 我想做以下事情:

  • 创建框架
  • 调用一个对话框(从那个框架)
  • 调用 MessageBox(从该对话框)
  • 在 MessageBox 上单击“确定”
  • 之后焦点应该放在对话框上

Dialog 仍然在前台,但焦点在 Frame 上!

我用这个软件:

  • wxPython:4.1.0 gtk3(凤凰)wxWidgets 3.1.4
  • Python:Python 3.8.10
  • 操作系统:5.4.0-90-generic #101-Ubuntu

这是一个小示例程序:


import wx


class Dialog1(wx.Dialog):

    def __init__(self, parent):
        wx.Dialog.__init__(self, id=wx.ID_ANY, name='', parent=parent,
              pos=wx.Point(500, 100), size=wx.Size(500, 200),
              style=wx.DEFAULT_DIALOG_STYLE, title='My Dialog')
                
        box = wx.BoxSizer(wx.VERTICAL)

        button1 = wx.Button(self, wx.ID_ANY, "open MessageBox")
        button1.Bind(wx.EVT_BUTTON, self.DisplayMessage)
        box.Add(button1, 0, wx.ALL, 10)
        
        self.orderNr = wx.TextCtrl(self, id=wx.ID_ANY, style=0, value='')
        box.Add(self.orderNr, 0, wx.ALL, 10)

        self.SetSizer(box)
        self.Layout()
        
        
    def DisplayMessage(self, event):
        #wx.MessageBox('Important Message')
        wx.MessageBox('Important Message', parent=self)
        self.orderNr.SetFocus()
        #event.Skip()
        
        
class Frame(wx.Frame):

    def __init__(self, title):
        wx.Frame.__init__(self, None, title=title, pos=(150, 150), size=(350, 200))

        panel = wx.Panel(self)
        box = wx.BoxSizer(wx.VERTICAL)

        m_text = wx.StaticText(panel, -1, "Hello World!")
        box.Add(m_text, 0, wx.ALL, 10)

        button1 = wx.Button(panel, wx.ID_ANY, "open Dialog")
        button1.Bind(wx.EVT_BUTTON, self.OpenDialog)
        box.Add(button1, 0, wx.ALL, 10)
        
        panel.SetSizer(box)
        panel.Layout()


    def OpenDialog(self, event):
        dialog1 = Dialog1(self)
        dialog1.ShowModal()
        #event.Skip()


app = wx.App(None)
top = Frame("My Frame")
top.Show()
app.MainLoop()

【问题讨论】:

  • 你没有说你正在使用什么版本的wxPython,但是在带有'4.1.1 gtk3 (phoenix) wxWidgets 3.1.5'的Linux上,它显然可以按要求工作,即一旦消息框被关闭,self.orderNr.SetFocus() 选择 TextCtrl in对话框和下一个键盘输入在该控件中。
  • 嗨 Rolf,谢谢,这有助于开始 :) 我使用这个版本:4.1.0 gtk3 (phoenix) wxWidgets 3.1.4 我会更新原帖。
  • 好的,我已经尝试过,使用 4.1.1 版本,但这在这里也不起作用。 @RolfofSaxony:您使用哪个 Python 版本?
  • 也适合我。我怀疑问题本身不在 wxPython 中,而是在操作系统的窗口管理器中。
  • 可能是这样。我使用 Xfce 4.14(xfwm4 用于窗口管理器)。稍后我会尝试使用不同的。

标签: wxpython


【解决方案1】:

作为一种解决方法:当我隐藏和显示对话框时它可以工作:

    def DisplayMessage(self, event):
        wx.MessageBox('Important Message')
        self.Hide()
        self.Show()
        self.orderNr.SetFocus()

【讨论】:

  • self.Refresh() 怎么样?正如 Petr 指出的那样,看起来窗口管理器需要轻推。
  • 不,这没有帮助:-/
猜你喜欢
  • 2011-12-04
  • 1970-01-01
  • 2012-07-03
  • 1970-01-01
  • 2020-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多