【问题标题】:Creating multiple Frames in for loop wxPython在for循环wxPython中创建多个框架
【发布时间】:2014-09-03 17:52:21
【问题描述】:

我是 wxPython 的新手。我在一个文件中有多个记录,每个记录都必须从文件中读取并显示在不同的框架中。我正在尝试在 for 循环中创建帧,但我希望仅在第一帧被销毁后才创建第二帧。以下是我的代码:

import wx
import textentry

class Frame(wx.Frame):  
    def __init__(self, fargs, **kwargs):
        wx.Frame.__init__(self, fargs, **kwargs)
        #self.Bind(wx.EVT_CLOSE, self.OnClose)
        self.panel = wx.Panel(self)
        self.box = wx.BoxSizer(wx.VERTICAL)

        self.m_text = wx.StaticText(self.panel, -1, label = suggested.lstrip())
        self.m_text.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD))
        self.m_text.SetSize(self.m_text.GetBestSize())
        self.box.Add(self.m_text, 0, wx.ALL, 10)


        self.filler_text = wx.StaticText(self.panel, -1, "************ Description ****************")
        self.filler_text.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD))
        self.filler_text.SetSize(self.filler_text.GetBestSize())
        self.box.Add(self.filler_text, 0, wx.ALL, 10)



        self.d_text = wx.StaticText(self.panel, -1, label = description.lstrip())
        self.d_text.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD))
        self.d_text.SetSize(self.d_text.GetBestSize())
        self.box.Add(self.d_text, 0, wx.ALL, 10)

        self.m_close = wx.Button(self.panel, wx.ID_EDIT, "Edit")
        self.m_close.Bind(wx.EVT_BUTTON, self.onReject)
        self.box.Add(self.m_close, 0, wx.ALL, 10)

        self.m_skip = wx.Button(self.panel, wx.ID_EDIT, "Skip")
        self.m_skip.Bind(wx.EVT_BUTTON, self.onSkip)
        self.box.Add(self.m_skip, 0, wx.ALL, 10)

        self.m_accept = wx.Button(self.panel, wx.ID_YES, "Accept")
        self.m_accept.Bind(wx.EVT_BUTTON, self.OnAccept)
        self.box.Add(self.m_accept, 0, wx.ALL, 10)
        self.box.SetSizeHints(self)
        self.panel.SetSizer(self.box)
        self.panel.Layout()

    def onReject(self, event):
        dialog = textentry.TextEntryDialog(None, 'Edit License Info', 'Enter License information')
        dialog.Center()
        dialog.SetValue(self.m_text.GetLabel())
        if dialog.ShowModal() == wx.ID_OK:
            self.m_text.SetLabel(dialog.GetValue())
            self.box.SetSizeHints(self)
            self.panel.Layout()
            dialog.Destroy()

    def onSkip(self, event):
        self.Destroy()

    def OnClose(self, event):
        dlg = wx.MessageDialog(self,"Do you really want to close this application?","Confirm Exit", wx.OK|wx.CANCEL|wx.ICON_QUESTION)
        result = dlg.ShowModal()
        dlg.Destroy()
        if result == wx.ID_OK:
            self.Destroy()

    def OnAccept(self, event):
        f = open("AcceptedLicense.txt", "a")
        print self.m_text.GetLabel().encode('utf-8')
        f.write(self.m_text.GetLabel().encode('utf-8') + '\n')
        f.close()
        self.Destroy()

panel =''
app = wx.App(False)
f = open("License.txt", "r")
main_message = f.read()
f.close()
if main_message != None:
    for m in main_message.split('Suggested License Text:'):
        if m != "":
            desc = [d for d in m.split("Description:")]
            suggested = desc[0]
            description = desc[1]
            top = Frame(None)
            top.Show()
app.MainLoop()

This is the code I use to create multiple frames:

if main_message != None:
    for m in main_message.split('Suggested License Text:'):
        if m != "":
            desc = [d for d in m.split("Description:")]
            suggested = desc[0]
            description = desc[1]
            top = Frame(None)
            top.Show()

非常感谢任何帮助。

【问题讨论】:

  • 您还没有提出问题。如果代码产生错误,则记录它。 SO 不是代码审查服务。

标签: python wxpython


【解决方案1】:

您需要先启动app.MainLoop(),然后才能显示帧。在您的代码中,所有帧都是在运行 MainLoop() 之前创建的,这就是为什么所有帧都会同时显示的原因。与其创建多个框架,不如创建多个面板,只需隐藏之前的面板即可。

还要控制框架的创建,尝试在destroy() 函数调用之前的事件处理程序中返回一些内容。并在主框架中检查返回以创建下一个面板。

【讨论】:

    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    相关资源
    最近更新 更多