Seite 1 von 1
[gelöst]Fenster nach 5 Sekunden terminieren lassen
Verfasst: Montag 19. Juli 2010, 16:37
von Schaf220
Hallo liebe Community,
ich wollte gerne das mein GUI nach 5 Sekunden automatisch terminiert, wie mach ich das am besten?
MfG
Schaf220
Re: Fenster nach 5 Sekunden terminieren lassen
Verfasst: Montag 19. Juli 2010, 17:28
von ms4py
Re: Fenster nach 5 Sekunden terminieren lassen
Verfasst: Montag 19. Juli 2010, 17:37
von str1442
Mit meiner dürftigen wxPython Erfahrung:
Code: Alles auswählen
import sys
import wx
from functools import partial
class WaitQuitButton(wx.Button):
def __init__(self, parent, id, title, delay):
wx.Button.__init__(self, parent, id, title)
self._delay = delay
self.Bind(wx.EVT_BUTTON, self.OnClick, id=id)
def OnClick(self, event):
wx.FutureCall(self._delay, partial(sys.exit, 0))
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
button = WaitQuitButton(self, -1, "Close after five seconds", 5000)
class MyApp(wx.App):
def OnInit(self):
f = MyFrame(None, -1, "FiveSecondTerminator")
f.Show(True)
f.Centre()
return True
app = MyApp(0)
app.MainLoop()
Das ist wahrscheinlich nicht wx idiomatisch (wx.Frame hat eine Close() Methode, vielleicht muss die aufgerufen werden?). Du könntest auch wx.Timer benutzen.
(In Scala Swing:
Code: Alles auswählen
import swing.{SimpleGUIApplication, MainFrame, Button, Action}
import java.awt.event.{ActionListener, ActionEvent}
import javax.swing.Timer
object FiveSecondTerminator extends SimpleGUIApplication {
private val fiveSecondTimer = new Timer(5000, new ActionListener {
override def actionPerformed(e: ActionEvent): Unit = System.exit(0)
})
override def top: MainFrame = new MainFrame {
title = "FiveSecondTerminator"
preferredSize = (512, 512)
contents = new Button {
action = Action("Destroy after 5 seconds") {
fiveSecondTimer.start()
}
}
}
}
)
Re: Fenster nach 5 Sekunden terminieren lassen
Verfasst: Dienstag 20. Juli 2010, 12:46
von Dav1d
wie str1442 schon gesagt hat, mit wx.Timer bzw. wx.FutureCall (in diesem Fall sinnvoller) sollte das kein Problem sein.
Code: Alles auswählen
>>> import wx
>>> class Frame(wx.Frame):
... def __init__(self):
... wx.Frame.__init__(self, None, -1, "foo")
... btn = wx.Button(self, -1, "Click me")
... self.Bind(wx.EVT_BUTTON, self.on_clicked, btn)
... def on_clicked(self, evt):
... wx.FutureCall(5000, self.Close)
...
>>> app = wx.App(0)
>>> f = Frame()
>>> f.Show()
>>> app.MainLoop()