user interface - Python Tkinter attribute error entry in random letter typing test -


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     self.keeptime(num1)   file "c:/users/brett/documents/shellrunpractice", line 44, in keeptime     self.entry.selection_range(0, tkinter.end)   file "c:\python27\lib\lib-tk\tkinter.py", line 1845, in __getattr__     return getattr(self.tk, attr) attributeerror: entry 

here code:

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(self,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):         self.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() 

i don't know why receiving problem starting learn tkinter , not yet.

the 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.

i 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.

any general comments how improve coding appreciated, comments on how write program want, feel current method not practical/functional.

command= expect function name - without () , parameters.

using command=self.usertypetest(0) run self.usertypetest(0)
, result assigned command=.

but self.usertypetest(0) use self.entry wasn't defined yet.

self.entry defined after tkinter.button(...command=self.usertypetest(0)...)

if need assing function parameters use lambda

command=lambda:self.usertypetest(0) 

and works.


edit:

full working version:

(i changed variable names)

import tkinter import random  class simpleapp_tk(tkinter.tk):      def __init__(self,parent):         tkinter.tk.__init__(self,parent)         self.parent = parent         self.initialize()         self.started = false # true when timer running      def initialize(self):         self.grid()          self.infovariable = tkinter.stringvar()         self.labelinfo = tkinter.label(self, textvariable=self.infovariable, anchor="w", fg="blue", bg="gold")         self.labelinfo.grid(column=0, row=0, sticky='ew')         self.infovariable.set(u"press button begin!")          self.buttonstart = tkinter.button(self, text=u"start", command=self.on_buttonstart, fg="black", bg="green")         self.buttonstart.grid(column=2, row=0)          self.timevariable = tkinter.stringvar()         self.labeltime = tkinter.label(self, textvariable=self.timevariable, anchor="w", fg="blue", bg="gold")         self.labeltime.grid(column=1, row=0, sticky='ew')         self.timevariable.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.entry.bind('<key>', self.on_key) # event          self.grid_columnconfigure(0,weight=1)         self.grid_rowconfigure(0,weight=1)         self.resizable(true,true)       def on_buttonstart(self):         if not self.started:             # reset values             self.started = true             self.number_of_letters = 20             self.score = 0             self.time = 0.0             # select first letter             self.randletter = random.choice('qwer')             # show first letter , score             self.infovariable.set('score:' + str(self.score)+' | press ' + self.randletter)             # start timer             self.after(100, self.timer)      def on_key(self, event):         if not self.started:             self.entryvariable.set('')         else:             if event.char == self.randletter:                 print 'correct', event.char                 self.score += 5             else:                 print 'incorrect', event.char                 self.score -= 2              self.number_of_letters -= 1              if self.number_of_letters == 0:                 self.started = false                 self.entryvariable.set('')                 self.infovariable.set('score:' + str(self.score))             else:                 self.randletter = random.choice('qwer')                 self.infovariable.set('score:' + str(self.score)+' | press ' + self.randletter)      def timer(self):         if self.started:             self.time += .1             self.timevariable.set('time: ' + str(round(self.time,1)) )             self.after(100, self.timer)   if __name__ == "__main__":     app = simpleapp_tk(none)     app.title('lol practice')     app.geometry("700x50")     app.configure(background='black')     app.mainloop() 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -