Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 弹出一个包含文本的窗口,并在特定时间后自动关闭_Python_Wxpython - Fatal编程技术网

Python 弹出一个包含文本的窗口,并在特定时间后自动关闭

Python 弹出一个包含文本的窗口,并在特定时间后自动关闭,python,wxpython,Python,Wxpython,这是我写的代码。它确实关闭窗口,但不显示其中的文本。我需要显示文本,然后自动关闭窗口。 我应该做些什么改变才能让它工作 谢谢 这是密码 import wx from time import sleep class Frame(wx.Frame): def __init__(self, title): wx.Frame.__init__(self, None, title=title, size=(300,200)) self.panel = wx.Pa

这是我写的代码。它确实关闭窗口,但不显示其中的文本。我需要显示文本,然后自动关闭窗口。 我应该做些什么改变才能让它工作 谢谢

这是密码

import wx
from time import sleep

class Frame(wx.Frame):
    def __init__(self, title):
        wx.Frame.__init__(self, None, title=title, size=(300,200))

        self.panel = wx.Panel(self)
        box = wx.BoxSizer(wx.VERTICAL)
        m_text = wx.StaticText(self.panel, -1, 'File Uploaded!')
        m_text.SetSize(m_text.GetBestSize())

        box.Add(m_text, 0, wx.ALL, 10)
        self.panel.SetSizer(box)
        self.panel.Layout()
        self.Bind(wx.EVT_ACTIVATE, self.onClose)

    def onClose(self, event):
        sleep(5)
        self.Destroy()

app = wx.App(redirect=True)
top = Frame('test')
top.Show()
app.MainLoop()

我建议使用wx.Timer。如果使用time.sleep(),则会阻止wxPython的主循环,从而使应用程序无响应。以下是修改后使用计时器的代码:

import wx

class Frame(wx.Frame):
    def __init__(self, title):
        wx.Frame.__init__(self, None, title=title, size=(300,200))

        self.panel = wx.Panel(self)
        box = wx.BoxSizer(wx.VERTICAL)
        m_text = wx.StaticText(self.panel, -1, 'File Uploaded!')
        m_text.SetSize(m_text.GetBestSize())

        box.Add(m_text, 0, wx.ALL, 10)
        self.panel.SetSizer(box)
        self.panel.Layout()

        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.onClose, self.timer)
        self.timer.Start(5000)

    def onClose(self, event):
        self.Close()

app = wx.App(redirect=True)
top = Frame('test')
top.Show()
app.MainLoop()
您可以在本文中阅读有关计时器的更多信息:

重复的,看一下那个。
import wx

class Frame(wx.Frame):
    def __init__(self, title):
        wx.Frame.__init__(self, None, title=title, size=(300,200))

        self.panel = wx.Panel(self)
        box = wx.BoxSizer(wx.VERTICAL)
        m_text = wx.StaticText(self.panel, -1, 'File Uploaded!')
        m_text.SetSize(m_text.GetBestSize())

        box.Add(m_text, 0, wx.ALL, 10)
        self.panel.SetSizer(box)
        self.panel.Layout()

        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.onClose, self.timer)
        self.timer.Start(5000)

    def onClose(self, event):
        self.Close()

app = wx.App(redirect=True)
top = Frame('test')
top.Show()
app.MainLoop()