Python tkinter Attribute Error: class has no attribute












0















I have read all similar questions asked but I still can't figure out how to solve the error. I am creating a countdown timer that will update and show the remaining time on the 'timeLeft' label(code below). However, I keep getting this error when I try to update the label with function start_count() :



self.timeLeft.config(text= str(mins) +"分"+ str(secs) +"秒")
AttributeError: 'PracticePage' object has no attribute 'timeLeft'


Below is part of my code:



    class PracticePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
timeLeft = tk.Label(self,text= "")
backButton = ttk.Button(self, text="やり直す", command = lambda: controller.show_frame(StartPage))
homeButton = ttk.Button(self, text="サインアウト", command = lambda:controller.show_frame(SignInPage))

timeLeft.pack()
backButton.pack()
homeButton.pack()
self.start_count(120)

def start_count(self,t):
global mins
global secs
time = t
while time>0:
mins, secs = divmod(time,60)
mins = round(mins)
secs = round(secs)
self.timeLeft.config(text= str(mins) +"分"+ str(secs) +"秒")
time = time-1
if (time==0):
break


Can someone help? Thank you in advance.










share|improve this question


















  • 1





    Make sure that anywhere you have timeLeft to change it to self.timeLeft.

    – Mike - SMT
    Nov 15 '18 at 14:23


















0















I have read all similar questions asked but I still can't figure out how to solve the error. I am creating a countdown timer that will update and show the remaining time on the 'timeLeft' label(code below). However, I keep getting this error when I try to update the label with function start_count() :



self.timeLeft.config(text= str(mins) +"分"+ str(secs) +"秒")
AttributeError: 'PracticePage' object has no attribute 'timeLeft'


Below is part of my code:



    class PracticePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
timeLeft = tk.Label(self,text= "")
backButton = ttk.Button(self, text="やり直す", command = lambda: controller.show_frame(StartPage))
homeButton = ttk.Button(self, text="サインアウト", command = lambda:controller.show_frame(SignInPage))

timeLeft.pack()
backButton.pack()
homeButton.pack()
self.start_count(120)

def start_count(self,t):
global mins
global secs
time = t
while time>0:
mins, secs = divmod(time,60)
mins = round(mins)
secs = round(secs)
self.timeLeft.config(text= str(mins) +"分"+ str(secs) +"秒")
time = time-1
if (time==0):
break


Can someone help? Thank you in advance.










share|improve this question


















  • 1





    Make sure that anywhere you have timeLeft to change it to self.timeLeft.

    – Mike - SMT
    Nov 15 '18 at 14:23
















0












0








0








I have read all similar questions asked but I still can't figure out how to solve the error. I am creating a countdown timer that will update and show the remaining time on the 'timeLeft' label(code below). However, I keep getting this error when I try to update the label with function start_count() :



self.timeLeft.config(text= str(mins) +"分"+ str(secs) +"秒")
AttributeError: 'PracticePage' object has no attribute 'timeLeft'


Below is part of my code:



    class PracticePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
timeLeft = tk.Label(self,text= "")
backButton = ttk.Button(self, text="やり直す", command = lambda: controller.show_frame(StartPage))
homeButton = ttk.Button(self, text="サインアウト", command = lambda:controller.show_frame(SignInPage))

timeLeft.pack()
backButton.pack()
homeButton.pack()
self.start_count(120)

def start_count(self,t):
global mins
global secs
time = t
while time>0:
mins, secs = divmod(time,60)
mins = round(mins)
secs = round(secs)
self.timeLeft.config(text= str(mins) +"分"+ str(secs) +"秒")
time = time-1
if (time==0):
break


Can someone help? Thank you in advance.










share|improve this question














I have read all similar questions asked but I still can't figure out how to solve the error. I am creating a countdown timer that will update and show the remaining time on the 'timeLeft' label(code below). However, I keep getting this error when I try to update the label with function start_count() :



self.timeLeft.config(text= str(mins) +"分"+ str(secs) +"秒")
AttributeError: 'PracticePage' object has no attribute 'timeLeft'


Below is part of my code:



    class PracticePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
timeLeft = tk.Label(self,text= "")
backButton = ttk.Button(self, text="やり直す", command = lambda: controller.show_frame(StartPage))
homeButton = ttk.Button(self, text="サインアウト", command = lambda:controller.show_frame(SignInPage))

timeLeft.pack()
backButton.pack()
homeButton.pack()
self.start_count(120)

def start_count(self,t):
global mins
global secs
time = t
while time>0:
mins, secs = divmod(time,60)
mins = round(mins)
secs = round(secs)
self.timeLeft.config(text= str(mins) +"分"+ str(secs) +"秒")
time = time-1
if (time==0):
break


Can someone help? Thank you in advance.







python-3.x tkinter






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 13:18









Sarah TeohSarah Teoh

12




12








  • 1





    Make sure that anywhere you have timeLeft to change it to self.timeLeft.

    – Mike - SMT
    Nov 15 '18 at 14:23
















  • 1





    Make sure that anywhere you have timeLeft to change it to self.timeLeft.

    – Mike - SMT
    Nov 15 '18 at 14:23










1




1





Make sure that anywhere you have timeLeft to change it to self.timeLeft.

– Mike - SMT
Nov 15 '18 at 14:23







Make sure that anywhere you have timeLeft to change it to self.timeLeft.

– Mike - SMT
Nov 15 '18 at 14:23














1 Answer
1






active

oldest

votes


















0














You seam to misunderstand how a class attribute works or what exactly self. is doing.



The self. prefix is used to tell the class that a specific variable or function is being assigned as a class attribute or method.



Here you have created a variable in the __init__ method called timeLeft however you try to access it later with self.timeLeft. This will not work as timeLeft was defined as a local variable only to the __init__ method.



To correct this simply make sure timeLeft is updated to be self.timeLeft anywhere in your code and it should work fine from there.



Updated class:



class PracticePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.timeLeft = tk.Label(self,text= "")
backButton = ttk.Button(self, text="やり直す", command = lambda: controller.show_frame(StartPage))
homeButton = ttk.Button(self, text="サインアウト", command = lambda:controller.show_frame(SignInPage))

self.timeLeft.pack()
backButton.pack()
homeButton.pack()
self.start_count(120)

def start_count(self,t):
global mins
global secs
time = t
while time>0:
mins, secs = divmod(time,60)
mins = round(mins)
secs = round(secs)
self.timeLeft.config(text= str(mins) +"分"+ str(secs) +"秒")
time = time-1
if (time==0):
break





share|improve this answer
























  • Thank you for you explainnation and it really work!

    – Sarah Teoh
    Nov 16 '18 at 0:09











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320389%2fpython-tkinter-attribute-error-class-has-no-attribute%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You seam to misunderstand how a class attribute works or what exactly self. is doing.



The self. prefix is used to tell the class that a specific variable or function is being assigned as a class attribute or method.



Here you have created a variable in the __init__ method called timeLeft however you try to access it later with self.timeLeft. This will not work as timeLeft was defined as a local variable only to the __init__ method.



To correct this simply make sure timeLeft is updated to be self.timeLeft anywhere in your code and it should work fine from there.



Updated class:



class PracticePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.timeLeft = tk.Label(self,text= "")
backButton = ttk.Button(self, text="やり直す", command = lambda: controller.show_frame(StartPage))
homeButton = ttk.Button(self, text="サインアウト", command = lambda:controller.show_frame(SignInPage))

self.timeLeft.pack()
backButton.pack()
homeButton.pack()
self.start_count(120)

def start_count(self,t):
global mins
global secs
time = t
while time>0:
mins, secs = divmod(time,60)
mins = round(mins)
secs = round(secs)
self.timeLeft.config(text= str(mins) +"分"+ str(secs) +"秒")
time = time-1
if (time==0):
break





share|improve this answer
























  • Thank you for you explainnation and it really work!

    – Sarah Teoh
    Nov 16 '18 at 0:09
















0














You seam to misunderstand how a class attribute works or what exactly self. is doing.



The self. prefix is used to tell the class that a specific variable or function is being assigned as a class attribute or method.



Here you have created a variable in the __init__ method called timeLeft however you try to access it later with self.timeLeft. This will not work as timeLeft was defined as a local variable only to the __init__ method.



To correct this simply make sure timeLeft is updated to be self.timeLeft anywhere in your code and it should work fine from there.



Updated class:



class PracticePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.timeLeft = tk.Label(self,text= "")
backButton = ttk.Button(self, text="やり直す", command = lambda: controller.show_frame(StartPage))
homeButton = ttk.Button(self, text="サインアウト", command = lambda:controller.show_frame(SignInPage))

self.timeLeft.pack()
backButton.pack()
homeButton.pack()
self.start_count(120)

def start_count(self,t):
global mins
global secs
time = t
while time>0:
mins, secs = divmod(time,60)
mins = round(mins)
secs = round(secs)
self.timeLeft.config(text= str(mins) +"分"+ str(secs) +"秒")
time = time-1
if (time==0):
break





share|improve this answer
























  • Thank you for you explainnation and it really work!

    – Sarah Teoh
    Nov 16 '18 at 0:09














0












0








0







You seam to misunderstand how a class attribute works or what exactly self. is doing.



The self. prefix is used to tell the class that a specific variable or function is being assigned as a class attribute or method.



Here you have created a variable in the __init__ method called timeLeft however you try to access it later with self.timeLeft. This will not work as timeLeft was defined as a local variable only to the __init__ method.



To correct this simply make sure timeLeft is updated to be self.timeLeft anywhere in your code and it should work fine from there.



Updated class:



class PracticePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.timeLeft = tk.Label(self,text= "")
backButton = ttk.Button(self, text="やり直す", command = lambda: controller.show_frame(StartPage))
homeButton = ttk.Button(self, text="サインアウト", command = lambda:controller.show_frame(SignInPage))

self.timeLeft.pack()
backButton.pack()
homeButton.pack()
self.start_count(120)

def start_count(self,t):
global mins
global secs
time = t
while time>0:
mins, secs = divmod(time,60)
mins = round(mins)
secs = round(secs)
self.timeLeft.config(text= str(mins) +"分"+ str(secs) +"秒")
time = time-1
if (time==0):
break





share|improve this answer













You seam to misunderstand how a class attribute works or what exactly self. is doing.



The self. prefix is used to tell the class that a specific variable or function is being assigned as a class attribute or method.



Here you have created a variable in the __init__ method called timeLeft however you try to access it later with self.timeLeft. This will not work as timeLeft was defined as a local variable only to the __init__ method.



To correct this simply make sure timeLeft is updated to be self.timeLeft anywhere in your code and it should work fine from there.



Updated class:



class PracticePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.timeLeft = tk.Label(self,text= "")
backButton = ttk.Button(self, text="やり直す", command = lambda: controller.show_frame(StartPage))
homeButton = ttk.Button(self, text="サインアウト", command = lambda:controller.show_frame(SignInPage))

self.timeLeft.pack()
backButton.pack()
homeButton.pack()
self.start_count(120)

def start_count(self,t):
global mins
global secs
time = t
while time>0:
mins, secs = divmod(time,60)
mins = round(mins)
secs = round(secs)
self.timeLeft.config(text= str(mins) +"分"+ str(secs) +"秒")
time = time-1
if (time==0):
break






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 14:27









Mike - SMTMike - SMT

9,67621435




9,67621435













  • Thank you for you explainnation and it really work!

    – Sarah Teoh
    Nov 16 '18 at 0:09



















  • Thank you for you explainnation and it really work!

    – Sarah Teoh
    Nov 16 '18 at 0:09

















Thank you for you explainnation and it really work!

– Sarah Teoh
Nov 16 '18 at 0:09





Thank you for you explainnation and it really work!

– Sarah Teoh
Nov 16 '18 at 0:09




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320389%2fpython-tkinter-attribute-error-class-has-no-attribute%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine