Unable to update TKinter graph
-
Colleagues, I am designing a GUI with two buttons and one is to display a graph, hourly temperature. The issue that I am facing is that I can not make a function(update_graph) that updates the value with self.after. This part creates page 1 and i working fine, until I call update_graph class PageOne(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) label = tk.Label(self, text="Page One!!!", font=LARGE_FONT) label.pack(pady=10,padx=10) button1 = tk.Button(self, text="Back to Home", command=lambda: controller.show_frame(StartPage)) button1.pack() button2 = tk.Button(self, text="Page Two", command=lambda: controller.show_frame(PageTwo)) button2.pack() canvas = Canvas(self, width=400, height=400, bg = 'white') canvas.pack() # create x and y axes canvas.create_line(100,250,400,250, width=2) canvas.create_line(100,250,100,50, width=2) # creates divisions for each axle for i in range(11): x = 100 + (i * 30) canvas.create_line(x,250,x,245, width=2) canvas.create_text(x,254, text='%d'% (10*i), anchor=N) for i in range(6): y = 250 - (i * 40) canvas.create_line(100,y,105,y, width=2) canvas.create_text(96,y, text='%5.1f'% (50.*i), anchor=E) self.update_graph() def update_graph(self): # here is canvas create line that causes a trouble canvas.create_line(100,250,140,200, width=2) self.after(100,self.update_graph) Whith this code I get an error "canvas is not defined". If I add self to canvas in update_graph, I get self.canvas.create_line(100,250,140,200, width=2) AttributeError: 'PageOne' object has no attribute 'canvas' What am I missing here?
-
Answer:
canvas is only defined in the scope of the constructor (__init__) method. If you want to be able to access it elsewhere in the class, you need to make it an instance variable. Instead of, canvas = Canvas(self, width=400, height=400, bg = 'white') make it, self.canvas = Canvas(self, width=400, height=400, bg = 'white') now, everywhere else in the code where you reference canvas, change it to self.canvas. This should fix the problem. On an unrelated note, a problem that I'm seeing in update_graph is that it calls itself recursively, or over and over. Perhaps you could change it to something like this: def update_graph(self): # This line is quite long. Maybe you could shorten it? self.after(100, lambda: canvas.create_line(100,250, 140,200, width=2)) Hope this helps! EDIT: My redefinition of update_graph only makes sense if you want one fixed line drawn. If you intend to add other functionality, such as periodic updates, the original code is correct, as Bryan pointed out.
Martin at Stack Overflow Visit the source
Related Q & A:
- What is the best data model and database systems to store social graph?Best solution by Quora
- How To Use the Graph API to Upload Photos to a user’s profile?Best solution by Stack Overflow
- How to approach Dynamic graph related problems?Best solution by Computer Science
- Is there a way of putting the Python Shell output in a tkinter window?Best solution by Stack Overflow
- How to find connected components of a random graph?Best solution by Mathematics
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.