【问题标题】:wxpython-can we add ok button on pybusyinfo dialog?wxpython-我们可以在 pybusyinfo 对话框上添加确定按钮吗?
【发布时间】:2017-03-08 12:52:05
【问题描述】:

我创建了 pybusyinfo 对话框来显示消息,并且在休眠 30 秒后它会自动关闭。

现在我需要在该对话框中添加确定按钮以获取用户输入,并且他们没有按下该对话框应在 30 秒内关闭的按钮。

我们可以将该按钮添加到 wx.lib.agw.pybusyinfo 对话框中吗?

或者wx小部件的其他对话框是否会在一段时间后自动关闭?

import wx
import wx.lib.agw.pybusyinfo as PBI
retVal = showmsg("dialog will be closed in 30 secs...")
time.sleep(30)
retVal = None

def showmsg(msg):
    app = wx.App(redirect=False)
    title = 'Message!'
    retVal = PBI.PyBusyInfo(msg, title=title)
    return retVal

【问题讨论】:

    标签: wxpython wxwidgets


    【解决方案1】:

    编写您自己的忙碌对话框,其中包含一个显示忙碌的仪表。
    使用wxTimer 循环显示。
    这应该可以帮助您开始:

    import wx
    class MyFrame(wx.Frame):
        def __init__(self, parent):
            wx.Frame.__init__(self, parent, -1, "Busy Dialog",size=(500,200))
            self.panel = wx.Panel(self)
            sizer = wx.BoxSizer(wx.VERTICAL)
            self.log = wx.TextCtrl(self.panel, wx.ID_ANY, size=(400,100),style = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL)
            self.button = wx.Button(self.panel, label="Click me")
            sizer.Add(self.log, 0, wx.EXPAND | wx.ALL, 10)
            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):
            dlg = Busy(parent = self.panel) 
            dlg.ShowModal()
            if dlg.result_text:
                self.log.AppendText("Text Input: "+dlg.result_text+"\n")
            dlg.Destroy()
    
    class Busy(wx.Dialog):
        def __init__(self, parent):
            wx.Dialog.__init__(self, parent, wx.ID_ANY, "Busy", size= (420,240))
            self.panel = wx.Panel(self,wx.ID_ANY)
            self.label = wx.StaticText(self.panel, label="Input", pos=(20,20))
            self.textinput = wx.TextCtrl(self.panel, value="", pos=(80,20), size=(300,-1))
            self.gauge = wx.Gauge(self.panel,size=(300,20),pos=(80,80), style=wx.GA_HORIZONTAL)
            self.livelabel = wx.StaticText(self.panel, label="Time to live:", pos=(80,110))
            self.lltime = wx.StaticText(self.panel, label="30", pos=(160,110))
            self.saveButton =wx.Button(self.panel, label="Save Input", pos=(80,160))
            self.closeButton =wx.Button(self.panel, label="Cancel", pos=(180,160))
            self.timeoutButton =wx.Button(self.panel, label="Timer Off", pos=(280,160))
            self.saveButton.Bind(wx.EVT_BUTTON, self.SaveBusyString)
            self.closeButton.Bind(wx.EVT_BUTTON, self.OnQuit)
            self.timeoutButton.Bind(wx.EVT_BUTTON, self.OnNoTimeout)
            self.Bind(wx.EVT_CLOSE, self.OnQuit)
            self.timer = wx.Timer(self)
            self.Bind(wx.EVT_TIMER,self.OnTimer, self.timer)
            self.lifetimer = wx.Timer(self)
            self.Bind(wx.EVT_TIMER,self.OnLifeTimer, self.lifetimer)
            self.timer.Start(100)
            self.lifetimer.Start(1000)
            self.timeoutbutton_pressed = False
            self.gauge.SetBackgroundColour(wx.Colour(0, 127, 255, 255)) #Slate Blue
            self.gauge.SetRange(100)
            self.gauge.SetValue(0)
            self.life = 30
            self.direction = 1
            self.Show()
    
        def OnTimer(self, evt): #Update gauge
            x = int(self.gauge.GetValue())
            if x == 0:
                self.direction = 1
            elif x == 100:
                self.direction = -1
            x+=self.direction
            self.gauge.SetValue(x)
    
        def OnLifeTimer(self, evt): #Update time to live
            if self.timeoutbutton_pressed == True:
                return
            self.life -= 1
            self.lltime.SetLabelText(str(self.life))
            if self.life < 1:
                self.OnQuit(None)
    
        def OnNoTimeout(self, evt): # toggle time to live
            if self.timeoutbutton_pressed == False:
                self.timeoutbutton_pressed = True
                self.timeoutButton.SetLabel("Timer On")
            else:
                self.timeoutbutton_pressed = False
                self.timeoutButton.SetLabel("Timer Off")
    
        def OnQuit(self, event):
            self.timer.Stop()
            self.lifetimer.Stop()
            self.result_text = None
            self.Destroy()
    
        def SaveBusyString(self, event): # return input
            self.result_text = self.textinput.GetValue()
            self.timer.Stop()
            self.lifetimer.Stop()
            self.Destroy()
    
    app = wx.App()
    frame = MyFrame(None)
    frame.Show()
    app.MainLoop()
    

    编辑:
    您需要一个框架,因为对话框需要父级,因此最简单的方法是制作一个您看不到的框架。
    去掉上面的 MyFrame 类,把代码中的 app 部分改成这样:

    app = wx.App()
    frame = wx.Frame(None,-1,"",size=(1,1))
    frame.Show()
    dlg = Busy(parent = frame)
    dlg.ShowModal()
    print dlg.result_text # Do whatever here with the result I just print it
    dlg.Destroy()
    

    【讨论】:

    • 哦,这太棒了..工作起来很迷人..但我不想要 Frame.Just 对话框。我可以从没有 Frame 的对话框中获取返回值吗?
    • 您需要busy 类和OnButton 部分中定义的代码,尽管您需要将parent 参数设置为与您的代码相关的值。
    • 我想直接打开繁忙的对话框并单击任何按钮并从类中获取值以作为参数传递给其他函数..你能帮我得到那个吗?
    • ..getting "PyDeadObjectError" on print dlg.result_text :-(
    • 您需要在编辑问题时发布您的代码,我猜不出您在做什么;)
    猜你喜欢
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    相关资源
    最近更新 更多