wxPython持续更新面板
我是 wxPython 的新手,无法解决一个问题。我需要不断更新面板的时钟值。我有一个解决方案,但在这种情况下我无法正常关闭窗口(alt+f4 不起作用)。 另外我不明白 .Update .Refresh 和何时应该调用 .Destroy 之间有什么区别?
有人可以推荐一本好书,《如何用 wxPython 编程》吗? 感谢您的任何帮助。
class TimeDatePanel(wx.Panel):
def __init__(self, parent, ID=ID_TIMEDATE, pos=wx.DefaultPosition, size=(50, 50), controller=None):
wx.Panel.__init__(self, parent, ID, pos, size, wx.RAISED_BORDER)
self.controller = controller
transCoded = controller.transCodes
layout = wx.GridSizer(5,2,0,10)
layout.Add(wx.StaticText(self, wx.ID_ANY, transCoded.get("Time & Date")))
layout.Add(wx.StaticText(self, wx.ID_ANY, ""), 0,flag=wx.ALL)
layout.Add(wx.StaticText(self, wx.ID_ANY, transCoded.get("Local time")), 0,flag=wx.ALL|wx.ALIGN_RIGHT)
self.LT = wx.StaticText(self, wx.ID_ANY, "")
layout.Add(self.LT)
layout.Add(wx.StaticText(self, wx.ID_ANY, transCoded.get("UTC")), 0,flag=wx.ALL|wx.ALIGN_RIGHT)
self.UTC = wx.StaticText(self, wx.ID_ANY, "")
layout.Add(self.UTC)
layout.Add(wx.StaticText(self, wx.ID_ANY, transCoded.get("Julian day")), 0,flag=wx.ALL|wx.ALIGN_RIGHT)
self.JD = wx.StaticText(self, wx.ID_ANY, "")
layout.Add(self.JD)
layout.Add(wx.StaticText(self, wx.ID_ANY, transCoded.get("Local sidereal time")), 0,flag=wx.ALL|wx.ALIGN_RIGHT)
self.LST = wx.StaticText(self, wx.ID_ANY, "")
layout.Add(self.LST)
self.SetSizer(layout)
self.updateTimeDate()
self.Fit()
wx.EVT_PAINT(self, self.onPaint)
def onPaint(self, event=None):
self.updateTimeDate()
def updateTimeDate(self):
mechanics = self.controller.mechanics
self.LT.SetLabel(str(mechanics.getLT()))
self.UTC.SetLabel(str(mechanics.getUTC()))
self.JD.SetLabel(str(mechanics.getYD()))
self.LST.SetLabel(str(mechanics.getLST()))
I am new in wxPython and can't solve one problem. I need to continuously update panel with clock value. I have a solution, but in this case I can't normally close window (alt+f4 not works).
Also I do not unsderstand what is the difference between .Update .Refresh and when .Destroy should be called?
Can some one reccomend a good book, how to program in wxPython?
Thanks for any help.
class TimeDatePanel(wx.Panel):
def __init__(self, parent, ID=ID_TIMEDATE, pos=wx.DefaultPosition, size=(50, 50), controller=None):
wx.Panel.__init__(self, parent, ID, pos, size, wx.RAISED_BORDER)
self.controller = controller
transCoded = controller.transCodes
layout = wx.GridSizer(5,2,0,10)
layout.Add(wx.StaticText(self, wx.ID_ANY, transCoded.get("Time & Date")))
layout.Add(wx.StaticText(self, wx.ID_ANY, ""), 0,flag=wx.ALL)
layout.Add(wx.StaticText(self, wx.ID_ANY, transCoded.get("Local time")), 0,flag=wx.ALL|wx.ALIGN_RIGHT)
self.LT = wx.StaticText(self, wx.ID_ANY, "")
layout.Add(self.LT)
layout.Add(wx.StaticText(self, wx.ID_ANY, transCoded.get("UTC")), 0,flag=wx.ALL|wx.ALIGN_RIGHT)
self.UTC = wx.StaticText(self, wx.ID_ANY, "")
layout.Add(self.UTC)
layout.Add(wx.StaticText(self, wx.ID_ANY, transCoded.get("Julian day")), 0,flag=wx.ALL|wx.ALIGN_RIGHT)
self.JD = wx.StaticText(self, wx.ID_ANY, "")
layout.Add(self.JD)
layout.Add(wx.StaticText(self, wx.ID_ANY, transCoded.get("Local sidereal time")), 0,flag=wx.ALL|wx.ALIGN_RIGHT)
self.LST = wx.StaticText(self, wx.ID_ANY, "")
layout.Add(self.LST)
self.SetSizer(layout)
self.updateTimeDate()
self.Fit()
wx.EVT_PAINT(self, self.onPaint)
def onPaint(self, event=None):
self.updateTimeDate()
def updateTimeDate(self):
mechanics = self.controller.mechanics
self.LT.SetLabel(str(mechanics.getLT()))
self.UTC.SetLabel(str(mechanics.getUTC()))
self.JD.SetLabel(str(mechanics.getYD()))
self.LST.SetLabel(str(mechanics.getLST()))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要经常更新时钟,为什么不使用 AnalogClock、LEDNumberCtrl 或者使用 wx.Timer 更新的 TimeCtrl?以下教程将帮助您了解计时器部分: http ://www.blog.pythonlibrary.org/2009/08/25/wxpython-using-wx-timers/
前两个小部件会自行更新。当您保留 StaticText 控件或其他普通小部件的值时,您应该必须调用 Update、Refresh 或 Layout。只需使用 SetValue 或 SetLabel 即可。
Robin Dunn 有一本旧书,名为“wxPython in Action”,大部分内容仍然很棒。今年还有一本由 Cody Precord 编写的 wxPython Cookbook。
If you need the clock updated every so often, why not use the AnalogClock, LEDNumberCtrl or maybe the TimeCtrl that's updated with a wx.Timer? The following tutorial will help you with the timer part: http://www.blog.pythonlibrary.org/2009/08/25/wxpython-using-wx-timers/
The first two widgets update themselves. You should have to call Update, Refresh or Layout when you rest a value of a StaticText control or other normal widget. Just use SetValue or SetLabel instead.
Robin Dunn has an older book called "wxPython in Action" that is still great for the most part. There's also a wxPython Cookbook by Cody Precord that came out this year.