【问题标题】:wxPython: show number repeatedlywxPython:重复显示数字
【发布时间】:2014-08-22 06:24:03
【问题描述】:

我是 Python 编程的初学者,现在我想知道为什么我的小部件不能重复显示数字。 也就是说,我想让文本显示从 1 到 9 的数字,但在 while 循环中,它只显示 9.... 有什么建议吗?

这是我的代码(Python 版本:2.6):

#!/user/bin/python


import wx

class Frame(wx.Frame):
def __init__(self,parent,id):
    wx.Frame.__init__(self, parent, id,
                      'Show Number',
                      size = (200,150),
                      style=wx.MINIMIZE_BOX | wx.RESIZE_BORDER 
| wx.SYSTEM_MENU | wx.CAPTION |  wx.CLOSE_BOX)
    self.initUI()

def initUI(self):

    widgetPanel=wx.Panel(self, -1)
    widgetPanel.SetBackgroundColour('white')

    # Buttons for play the simulation
    playButton = wx.Button(widgetPanel, -1, "Play", pos=(10,10), size=(30,30))

    self.Bind(wx.EVT_BUTTON, self.play, playButton)
    playButton.SetDefault()

    # Time
    self.timeText = wx.TextCtrl(widgetPanel, -1, "", pos=(10, 50), 
                            size =(100,30), style=wx.TE_CENTER)
    self.timeText.SetBackgroundColour('white')
    self.timeText.SetFont(wx.Font(20, wx.DECORATIVE, wx.NORMAL, wx.NORMAL))

def play(self, event):
    #self.timeText.SetValue("19:32")
    self.show_num()

def show_num(self):
    x = 0
    while(x < 10):
        self.timeText.SetValue(str(x))
        x += 1

if __name__ == "__main__":
    app = wx.App(False)
    frame = Frame(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

【问题讨论】:

    标签: python wxpython wxtextctrl


    【解决方案1】:

    你需要让你的应用有机会更新......最简单和最正确的方法是使用计时器而不是循环......我提供了一个最小的例子

    import wx
    app = wx.App(redirect=False)
    frame = wx.Frame(None,-1,"Counter")
    btn = wx.Button(f,-1,"Click Me!")
    timer = wx.Timer() # this will update the count
    
    def onUpdateButton(evt):
        next_int = int(btn.GetLabel())+1
        btn.SetLabel(str(next_int))
        if next_int > 10:
            timer.Stop()
            btn.Enable(True)
    
    
    def onButtonClick(event):
        btn.SetLabel("0")
        btn.Enable(False)
        timer.Start(1000) # 1 second updates
    
    timer.Bind(wx.EVT_TIMER,onUpdateButton)    
    btn.Bind(wx.EVT_BUTTON,onButtonClick)
    frame.Show()
    app.MainLoop()
    

    这对大多数用户来说可能是显而易见的,但可能值得一提的是,我只是使用标签字符串而不是使用当前计数来保存变量

    【讨论】:

      【解决方案2】:

      就像其他答案所说的那样,您需要给您的应用时间来更新。 我在您的代码中添加了一些内容以使其正常工作:

      import wx
      import time
      import thread
      
      class Frame(wx.Frame):
          def __init__(self,parent,id):
              wx.Frame.__init__(self, parent, id,
                                'Show Number',
                                size = (200,150),
                                style=wx.MINIMIZE_BOX | wx.RESIZE_BORDER 
          | wx.SYSTEM_MENU | wx.CAPTION |  wx.CLOSE_BOX)
              self.initUI()
      
          def initUI(self):
      
              widgetPanel=wx.Panel(self, -1)
              widgetPanel.SetBackgroundColour('white')
      
              # Buttons for play the simulation
              playButton = wx.Button(widgetPanel, -1, "Play", pos=(10,10), size=(30,30))
      
              self.Bind(wx.EVT_BUTTON, self.play, playButton)
              playButton.SetDefault()
      
              # Time
              self.timeText = wx.TextCtrl(widgetPanel, -1, "", pos=(10, 50), 
                                      size =(100,30), style=wx.TE_CENTER)
              self.timeText.SetBackgroundColour('white')
              self.timeText.SetFont(wx.Font(20, wx.DECORATIVE, wx.NORMAL, wx.NORMAL))
      
          def play(self, event):
              #self.timeText.SetValue("19:32")
              #self.show_num()
              thread.start_new_thread(self.show_num,())
      
          def show_num(self):
              x = 0
              while(x < 10):
                  self.timeText.SetValue(str(x))
                  time.sleep(1)
                  x += 1
      
      if __name__ == "__main__":
          app = wx.App(False)
          frame = Frame(parent=None,id=-1)
          frame.Show()
          app.MainLoop()
      

      请注意,time.sleep(1) 将 while 循环暂停 1 秒,以便更新显示。您可以根据需要减少或增加暂停时间。

      【讨论】:

      • 太棒了!在@Joran Beasley 的回答之后,我尝试了计时器并且它起作用了!现在你的代码似乎比我的还要好!谢谢!
      【解决方案3】:

      我遇到了更新问题:

      self.timeText.SetValue(str(x))
      time.sleep(1)
      x += 1
      

      我在睡眠前添加了一个 wx.YieldIfNeeded():

      self.timeText.SetValue(str(x))
      wx.YieldIfNeeded()
      wx.sleep(1)
      x += 1
      

      这可确保更新 GUI 并处理其他事件。 我也会使用 wx.Sleep()。这可以防止使用其他库...

      【讨论】:

        猜你喜欢
        • 2019-03-29
        • 1970-01-01
        • 2010-12-03
        • 2017-04-03
        • 2011-03-03
        • 2018-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多