【问题标题】:Flashing text in wxPythonwxPython中的闪烁文本
【发布时间】:2012-08-04 16:25:31
【问题描述】:

我想知道是否有人知道将闪烁文本编程到 wxPython 中的方法? (我对 wxPython 相当陌生) 它会每隔半秒左右在红色和正常之间闪烁一次,我使用的是 Python 2.7.3,而不是最新版本。

谢谢

克里斯

【问题讨论】:

    标签: text wxpython


    【解决方案1】:

    您需要了解如何即时更改fonts。通常,它只是调用小部件的 SetFont() 方法。既然您想定期执行此操作,那么您几乎肯定会想要使用 wx.Timer。如果您愿意,可以阅读我关于该主题的tutorial。我可能会使用 StaticText 小部件。

    更新:这是一个愚蠢的例子:

    import random
    import time
    import wx
    
    ########################################################################
    class MyPanel(wx.Panel):
        """"""
    
        #----------------------------------------------------------------------
        def __init__(self, parent):
            """Constructor"""
            wx.Panel.__init__(self, parent)
    
            self.font = wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
            self.flashingText = wx.StaticText(self, label="I flash a LOT!")
            self.flashingText.SetFont(self.font)
    
            self.timer = wx.Timer(self)
            self.Bind(wx.EVT_TIMER, self.update, self.timer)
            self.timer.Start(1000)
    
        #----------------------------------------------------------------------
        def update(self, event):
            """"""
            now = int(time.time())
            mod = now % 2
            print now
            print mod
            if mod:
                self.flashingText.SetLabel("Current time: %i" % now)
            else:
                self.flashingText.SetLabel("Oops! It's mod zero time!")
            colors = ["blue", "green", "red", "yellow"]
            self.flashingText.SetForegroundColour(random.choice(colors))
    
    ########################################################################
    class MyFrame(wx.Frame):
        """"""
    
        #----------------------------------------------------------------------
        def __init__(self):
            """Constructor"""
            wx.Frame.__init__(self, None, title="Flashing text!")
            panel = MyPanel(self)
            self.Show()
    
    #----------------------------------------------------------------------
    if __name__ == "__main__":
        app = wx.App(False)
        frame = MyFrame()
        app.MainLoop()
    

    【讨论】:

    • hmm,这是个好主意,你将如何让它自己启动,并且打印出的不是确切的日期和时间,而是 time.time() 数字并使用它来计算.我也在考虑使用 if else 语句,即如果计时器的整数值除以 2 的余数为 0,则显示此文本,否则显示此文本,您对如何执行此操作有任何想法吗? (对不起,如果这听起来像一个菜鸟问题)
    • 哦,我差点忘了,我可以复制和粘贴我的代码,就我所知,如果有帮助的话
    • 谢谢,这正是我要找的,我从来不知道这是在 wx 中,我的脚本在我浏览函数和小部件时从未显示过它
    猜你喜欢
    • 2023-03-17
    • 2017-03-08
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 2018-01-30
    • 1970-01-01
    • 2019-12-08
    相关资源
    最近更新 更多