python - Tkinter global name keeptime not defined -
import tkinter import random time import sleep class simpleapp_tk(tkinter.tk): def __init__(self,parent): tkinter.tk.__init__(self,parent) self.parent = parent self.initialize() def initialize(self): self.grid() self.labelvariable = tkinter.stringvar() label = tkinter.label(self,textvariable=self.labelvariable,anchor="w",fg="blue",bg="gold") label.grid(column=0,row=0,sticky='ew') self.labelvariable.set(u"press button begin!") self.button = tkinter.button(self,text=u"start", command=self.usertypetest(0),fg="black",bg="green") self.button.grid(column=2,row=0) self.labelvariable2 = tkinter.stringvar() label2 = tkinter.label(self,textvariable=self.labelvariable2,anchor="w",fg="blue",bg="gold") label2.grid(column=1,row=0,sticky='ew') self.labelvariable2.set(u'time') self.entryvariable = tkinter.stringvar() self.entry = tkinter.entry(self,textvariable=self.entryvariable,fg="black",bg="white") self.entry.grid(column=0,row=1,columnspan=2,sticky='ew') self.entryvariable.set(u"") self.grid_columnconfigure(0,weight=1) self.grid_rowconfigure(0,weight=1) self.resizable(true,true) def keeptime(num1): self.entry.selection_range(0, tkinter.end) timer = num1 t = 0 while timer < 1: self.labelvariable2.set(t) sleep(.01) t += .01 def usertype(self): randletter = random.choice('qwer') self.labelvariable.set("press "+randletter) userinput = self.entryvariable.get while userinput == '': pass if userinput == randletter: return 'correct' else: return 'incorrect' def usertypetest(self,num1): keeptime(num1) x in range(20): result = usertype() print result if result == 'correct': y = y+5 else: y = y-2 timer += 1 self.labelvariable.set(str(y)+' score') if __name__ == "__main__": app = simpleapp_tk(none) app.title('lol practice') app.geometry("700x50") app.configure(background='black') app.mainloop()
that program. goal of program when user clicks start, timer start, printing label, , shown letters randomly chosen (q,w,e,r), must type fast possible. program time them , give them score based on errors @ end, , stop timer. problem having says:
traceback (most recent call last): file "c:/users/brett/documents/shellrunpractice", line 83, in <module> app = simpleapp_tk(none) file "c:/users/brett/documents/shellrunpractice", line 9, in __init__ self.initialize() file "c:/users/brett/documents/shellrunpractice", line 22, in initialize command=self.usertypetest(0),fg="black",bg="green") file "c:/users/brett/documents/shellrunpractice", line 66, in usertypetest keeptime(num1) nameerror: global name 'keeptime' not defined
this has been fixed!
also, before made changes, code did not produce errors, when clicked button program hung. wondering if guys find other errors in code causing that, , if how it. realize code bad, i'm starting in tkinter, , i'm not entirely sure how works yet.
you should use self.keeptime(num1)
instead of keeptime(num1)
access class methods , should use self
first argument of keeptime
method
def keeptime(self,num1): self.entry.selection_range(0, tkinter.end) timer = num1 t = 0 while timer < 1: self.labelvariable2.set(t) sleep(.01) t += .01
Comments
Post a Comment