【问题标题】:wxpython: pause code as long as second frame is openwxpython:只要第二帧打开就暂停代码
【发布时间】:2021-06-25 22:40:48
【问题描述】:

我几乎是一个 Python 初学者,最近才开始使用 wxpython,现在有以下问题:

我正在编写一个应用程序,其中一个窗口打开,通过单击一个按钮,用户打开第二个窗口,他们应该在其中输入他们的姓名并单击另一个按钮。我的问题是我不知道如何暂停通过单击 Button1 执行的“事件代码”,直到用户在第二个窗口中完成他们的操作。

有没有办法暂停 OnButton1 中的代码(在 secondframe.Show() 行之后,直到用户在 SecondFrame 中输入他们的姓名并点击 OnButton2?

基本上我的目标是首先打印用户名,然后才打印完成

class MainFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)

        self.panel = wx.Panel(self)
        self.Button1 = wx.Button(self.panel, label='Button 1', pos=(20, 10), size=(90, 25))

        #Set event handlers
        self.Button1.Bind(wx.EVT_BUTTON, self.OnButton1)

    def OnButton1(self, e):
        secondframe = SecondFrame(None)
        secondframe.Show()
        print("Done")



class SecondFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)

        self.panel = wx.Panel(self)
        self.Button2 = wx.Button(self.panel, label='Button 2', pos=(50, 20), size=(90, 25))
        self.NameBox = wx.TextCtrl(self.panel, value="Type Your Name", pos=(50, 60), size=(200, -1))


        #Set event handlers
        self.Button2.Bind(wx.EVT_BUTTON, self.OnButton2)

    def OnButton2(self, e):
        Name = self.NameBox.GetValue()
        print(Name)


app = wx.App(False)
frame = MainFrame(None)
frame.Show()
app.MainLoop()

【问题讨论】:

  • 常用对话框类和函数封装了常用的对话框需求。它们都是“模态的”,在用户关闭对话框之前获取控制流,使它们在应用程序中易于使用。

标签: python pycharm event-handling wxpython frame


【解决方案1】:

我不知道有什么方法可以真正阻止那里的代码。 (此外,阻塞通常不是一个好主意。)
但是你可以得到同样的行为:

  • 在您打开第二个窗口的位置,在第一个窗口中调用self.Disable()
  • 在创建第二个窗口时,您还将第一个窗口的引用传递给它(即secondframe = SecondFrame(None, self)),调整构造函数(def __init__(self, parent, main):)并将其存储在一个变量中(self.mainFrame = main)。李>
  • 您向 MainFrame 添加一个方法,例如reenable(self)
  • 你打电话给self.Enable()
  • 完成第二帧后,您只需致电self.mainFrame.reenable()

总而言之,这会是这样的:

class MainFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)

        self.panel = wx.Panel(self)
        self.Button1 = wx.Button(self.panel, label='Button 1', pos=(20, 10), size=(90, 25))

        #Set event handlers
        self.Button1.Bind(wx.EVT_BUTTON, self.OnButton1)

    def OnButton1(self, e):
        secondframe = SecondFrame(None, self)
        self.Disable()
        secondframe.Show()

    def reenable(self):
        self.Enable()
        print("done")



class SecondFrame(wx.Frame):
    def __init__(self, parent, main):
        wx.Frame.__init__(self, parent)
        self.mainFrame = main

        self.panel = wx.Panel(self)
        self.Button2 = wx.Button(self.panel, label='Button 2', pos=(50, 20), size=(90, 25))
        self.NameBox = wx.TextCtrl(self.panel, value="Type Your Name", pos=(50, 60), size=(200, -1))


        #Set event handlers
        self.Button2.Bind(wx.EVT_BUTTON, self.OnButton2)

    def OnButton2(self, e):
        Name = self.NameBox.GetValue()
        print(Name)
        self.mainFrame.reenable()


app = wx.App(False)
frame = MainFrame(None)
frame.Show()
app.MainLoop()

【讨论】:

  • 看起来不错!我有类似的想法,但不知道如何执行,谢谢!
  • 我的荣幸。实际上,您可能希望在关闭时重新启用第一个窗口,而不是单击按钮。实现它的另一种方法是使第二个窗口成为自定义对话框并将其显示为模态 - 如果它的生命周期较短,这可能会更容易,但对于较长的生命周期,框架可能是正确的调用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多