2.wxPython图形界面加入wx.Timer定时器

时间:2022-07-22
本文章向大家介绍2.wxPython图形界面加入wx.Timer定时器,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

界面程序中加入wx.Timer定时器,可以让窗体延时执行一些动作。wx.Timer通过wx.EVT_TIMER事件来调用一个事件处理函数执行我们需要的动作,具体实现如代码所示。

#加入wx.Timer定时器
import wx
class myFrame(wx.Frame):
    def __init__(self):
        super().__init__(parent=None,pos=[100,100],
                         size=[390,420],title="商贾三国")
        self.SetIcon(wx.Icon("poem.ico"))
        # self.i=1
        self.panel = wx.Panel(self)
        self.panel.SetBackgroundColour((220, 210, 0))
        poem = "步出夏门行·观沧海 n作者:曹操  汉代 "
        font = wx.Font(20, wx.SCRIPT, wx.NORMAL, wx.NORMAL)
        self.mytext = wx.StaticText(parent=self.panel,
                                    pos=[30, 30], size=[330, 380], label=poem)
        self.mytext.SetFont(font)
        self.timer=wx.Timer(self)
        self.Show()
        self.Bind(wx.EVT_TIMER,self.onTimer,self.timer)
        self.timer.StartOnce(2000)
    def onTimer(self,event):
        poem = "步出夏门行·观沧海 n作者:曹操  汉代 n东临碣石,以观沧海。n" 
               "水何澹澹,山岛竦峙。n树木丛生,百草丰茂。n秋风萧瑟,洪波涌起。n" 
               "日月之行,若出其中。n星汉灿烂,若出其里。n幸甚至哉,歌以咏志。"
        # poem=str(self.i)
        self.mytext.SetLabel(poem)
        # self.i=self.i+1
        # print(self.i)
        # self.timer.Stop()
myapp=wx.App()
myframe=myFrame()
myapp.MainLoop()

sanguoguancanghai.png

poem.ico文件需要读者自己制作添加。

poem.jpg