Ich habe ein echt blödes Problem. Klingt einfach aber ich krieg das nich hin bzw. ich weiss nic wie ich das machen soll...
Also Ich habe einen Wert:
Code: Alles auswählen
a = 1
Code: Alles auswählen
a = 1
Code: Alles auswählen
from time import sleep
a = 0
while True:
sleep(4)
i += 1
Code: Alles auswählen
Traceback (most recent call last):
File "test.py", line 5, in <module>
i += 1
NameError: name 'i' is not defined
Hallo silentpolygon!silentpolygon hat geschrieben:Schläft mit dem Befehl sleep das komplette Programm? Soll heißen wenn ich eine GUI habe, die einen EventHandler hat, "schläft" der dann auch? Hatte das Problem mal mit wxPython und twisted.
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
import wx
import time
wx.SetDefaultPyEncoding("iso-8859-15")
class MyFrame(wx.Frame):
def __init__(self, parent = None, title = "Example"):
wx.Frame.__init__(self, parent, -1, title)
panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
panel.SetSizer(vbox)
self.label = wx.StaticText(panel, label = "<Uhrzeit im 10-Sekundentakt>")
vbox.Add(self.label, 0, wx.ALL, 10)
self.old_time = time.time()
self.timer = wx.Timer()
self.timer.Bind(wx.EVT_TIMER, self.on_timer)
self.timer.Start(200)
panel.Fit()
self.Fit()
def on_timer(self, event):
new_time = time.time()
if new_time - self.old_time >= 10:
self.old_time = new_time
self.label.SetLabel(time.strftime("%H:%M:%S"))
def main():
app = wx.PySimpleApp()
f = MyFrame()
f.Center()
f.Show()
app.MainLoop()
if __name__ == "__main__":
main()
Code: Alles auswählen
#!/usr/bin/env python
import Tkinter
from Tkconstants import *
class myWindow:
def __init__(self):
self.a = 0
self.mw = Tkinter.Tk()
self.mw.option_add("*font", ("Arial", 15, "normal"))
self.mw.title('Raising "a"')
self.mw.geometry("+250+200")
self.lab1 = Tkinter.Label(self.mw,
text = 'a = ' + str(self.a))
self.lab1.pack(padx = 100, pady = 100)
self.btn1 = Tkinter.Button(self.mw,
text = "Exit",
command = self.mw.destroy)
self.btn1.pack(padx = 10, pady = 10)
self.mw.after(ms = 4000, func = self.raiseA)
self.mw.mainloop()
def raiseA(self):
self.a += 1
self.lab1.configure(text = 'a = ' + str(self.a))
self.mw.after(ms = 4000, func = self.raiseA)
if __name__ == "__main__":
app = myWindow()