[gelöst]Fenster nach 5 Sekunden terminieren lassen

Plattformunabhängige GUIs mit wxWidgets.
Antworten
Benutzeravatar
Schaf220
User
Beiträge: 113
Registriert: Montag 11. August 2008, 16:00
Wohnort: Bremen
Kontaktdaten:

Hallo liebe Community,
ich wollte gerne das mein GUI nach 5 Sekunden automatisch terminiert, wie mach ich das am besten?

MfG
Schaf220
Zuletzt geändert von Schaf220 am Samstag 24. Juli 2010, 11:33, insgesamt 1-mal geändert.
ms4py
User
Beiträge: 1178
Registriert: Montag 19. Januar 2009, 09:37

Code: Alles auswählen

import time
import sys

time.sleep(5)
sys.exit()
„Lieber von den Richtigen kritisiert als von den Falschen gelobt werden.“
Gerhard Kocher

http://ms4py.org/
Benutzeravatar
str1442
User
Beiträge: 520
Registriert: Samstag 31. Mai 2008, 21:13

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()
      }
    }
  }
}
)
Dav1d
User
Beiträge: 1437
Registriert: Donnerstag 30. Juli 2009, 12:03
Kontaktdaten:

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()
the more they change the more they stay the same
Antworten