Python attribute error in a class - python

I want to create some frames in Tkinter, that should be updated periodically.
Here is the code for on of them:
from Tkinter import *
import time
import random
class KopfFrame:
def __init__(self,master):
frame = Frame(master,bg="tan")
frame.pack(side=TOP,expand=YES, fill=BOTH)
self.ZeitLabel = Label(frame)
self.ZeitLabel.pack(side=RIGHT, expand=NO,ipadx=2, ipady=2)
self.refresh()
def refresh(self):
self.ZeitLabel.configure(text=time.strftime("%H:%M:%S"))
# call this function again in 5 seconds
#print(self)
self.after(5000, self.refresh)
root = Tk()
K = KopfFrame(root)
root.mainloop()
But, when I run it, I have the error:
AttributeError: KopfFrame instance has no attribute 'after'
I am pretty sure, that the way of calling is the problem. So if someone could help me, thaf I would either be thankful for a hint to a good tutorial for functions, classes an how to call an use them.

You are trying to call self.after(), with self being an instance of your KopfFrame class. That class does not have such a method defined.
Perhaps you wanted to call the Frame.after() method here. You'll need to store a reference to the Frame object you pass in first, then call after on that:
class KopfFrame:
def __init__(self,master):
# make frame an attribute on self
self.frame = Frame(master, bg="tan")
self.frame.pack(side=TOP,expand=YES, fill=BOTH)
self.ZeitLabel = Label(self.frame)
self.ZeitLabel.pack(side=RIGHT, expand=NO,ipadx=2, ipady=2)
self.refresh()
def refresh(self):
self.ZeitLabel.configure(text=time.strftime("%H:%M:%S"))
# now you can reach the frame and call after on that:
self.frame.after(5000, self.refresh)
What I changed:
Instead of making frame only a local in the KopfFrame.__init__ method, make it an attribute on self.
Use the self.frame reference to the frame in KopfFrame.refresh() to access the Frame.after() method.

Related

How to define Tkinter controller?

Code Background: trying to have a popup window call a function in another class.
I am having trouble defining controller and configuring my functions correctly. I get an error about controller not being defined before it passes to the popupWindow class which makes sense because I have not defined controller somewhere else but I don't know where to do that:
NameError: name 'controller' is not defined
I have studied these past answers for help but am still stuck:
Calling Tkinter frame controller from function rather then button command
Calling functions from a Tkinter Frame to another
Here is my simplified code (Note: I define the various variables in the values function in other functions within the class BoundingBox but I have not included those functions for clarity and to shorten the code):
I also don't understand how to make the get_page function work which is part of my problem with figuring out how and where to define controller.
import tkinter as tk
from tkinter import *
class BoundingBox(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
def get_page(self, page_class):
return self.frames[page_class]
def popup(self):
self.w=popupWindow(self.master,controller)
def values(self):
print(self.start_x, self.start_y, self.end_x, self.end_y, self.totwidth, self.totheight, self.value1, self.value2)
self.allcord.append([self.start_x, self.start_y, self.end_x, self.end_y, self.totwidth, self.totheight, self.value1, self.value2])
self.allrect.append(self.rect)
print (len(self.allcord))
class popupWindow(tk.Toplevel):
def __init__(self, master, controller):
super().__init__(master)
self.controller = controller
self.l1=Label(self,text="Breed")
self.l1.grid(row=0, column=0)
self.e1=Entry(self)
self.e1.grid(row=0, column=1)
self.l2=Label(self,text="Color")
self.l2.grid(row=1, column=0)
self.e2=Entry(self)
self.e2.grid(row=1, column=1)
self.b=Button(self,text='Save',command=self.cleanup)
self.b.grid(row=2, column=1)
def cleanup(self):
self.value1=self.e1.get()
self.value2=self.e2.get()
self.controller.values()
self.top.destroy()
if __name__ == "__main__":
draw = BoundingBox()
draw.mainloop()
In this specific case, controller simply refers to the main window. So, you simply need to pass self as the controller argument:
def popup(self):
self.w=popupWindow(self.master, self)
Notice how the cleanup method calls self.controller.values(). values is defined in BoundingBox, so it's clear that popupwindow was designed to have BoundingBox as the controller.

Looking for a super explanation /tkinter direction (getting max recursion depth without super)

So I'm trying to use my "controller" to call my "view." But if I don't use super, I get an infinite recursion when buildTK tries to build iiBar. Using Super everything is fine. I'd just like to understand why that is.
import inputhandler as iH
import buildtk as tA
import scanhandler as aS
class ControlHandler:
def __init__(self):
#self.view = tA.buildTK() #does not work
self.view = super(tA.buildTK) #works
self.smodel = aS.aScan()
self.imodel = iH.InputHandler()
The buildTK class:
import tkinter as tt
import controlhandler as cH
class buildTK(tt.Frame):
def __init__(self, master = None):
self.frame = tt.Frame(master, bg="tan")
self.frame.grid()
self.ibar = iiBar(self.frame)
...
class iiBar:
def __init__(self, master):
print(repr(self)) #prints forever
self.mbar = tt.Frame(master, relief = 'raised', bg="blue")
self.mbar.grid(column=0, row=0) #Show File Bar
self.tryFile()
self.tryTool()
self.tryH()
EDIT: those try methods have no effect when commented out, but the basic code is:
def tryTool(self):
# Create tools menu
self.toolsbutton = tt.Menubutton(self.mbar, text = 'Tools', )
self.toolsbutton.grid(row=0, column=2)
self.toolsmenu = tt.Menu(self.toolsbutton, tearoff=0)
self.toolsbutton['menu'] = self.toolsmenu
# Populate tools menu
self.toolsmenu.add('command', label = 'tools', command = root.destroy)
And out of curiosity, is there a best-practice for going about what I'm trying to do? Eventually I'd like a "build handler" to instantiate the form (either tkinter, html, xml) - in which case the controller would instantiate the build handler which would determine what to build.
Found my issues. Some python oversights.
the import seems to take care of the instantiation
part of this has to do with the inconsistency I used at the end of my classes, i.e;
a. #if name == "main": ControlHandler()
forgot that was commented out
b. buildTK
root = tt.Tk()
all = buildTK(root)
root.mainloop()
c. buildTK0
def main():
root = tk.Tk()
app = Application(root)
app.mainloop()
if __name__ == "__main__":main()
else: main()
Super appreciate the feedback guys! Gave me lots to consider/review!
What you show here is not a good example of inheritance - it is somehwat messed up. The same thing goes for yout attempeted uses of super - it just does not crash because you are not doing anything with the super-object at all, after creating it.
So., first, define if you are inheriting Tkinter components of compositing, having attributes of your classes that reference Tkinter objects. This code snippet:
class buildTK(tt.Frame):
def __init__(self, master = None):
self.frame = tt.Frame(master, bg="tan")
self.frame.grid()
self.ibar = iiBar(self.frame)
does a little of both.
I consider (and a lot of the internet) that trying to inherit from a complex GUI class is a waste of time and effort - although OO purists from the 90's thought that tobe a good idea and documented it that way. Tkinter classes have hundreds of methods and attributes - if you try to subclass one of those, your chances of name-clashing sky-rocket, to start with.
Otherwise, if you just don't try to inherit from any tkinter component, and just use them, and give-up on your non-use of supe, your app have a nice chance of working -
as for what "super" does: it provides a clean mean to call the method you are overriding in a sublass on its superclasses, respecting Python's method-resolution-order (mro), and without hardcoding the super-class reference inside the method. supershould be used only for that - for example, if you were to properly inherit tkinter.frame:
class buildTK(tt.Frame):
def __init__(self, master = None):
super(buildTK, self).__init__(master, bg="tan")
# this is for composition: self.frame = tt.Frame(master, bg="tan")
self.grid()
# a prefix on our attributes and methods avoid clashing with
# tkinter:
self._myapp_ibar = iiBar(self.frame)

Getting the value of a checkbox

This is code I found when searching to understand and learn about Tkinter, but it gives an error on check box toggle.
from Tkinter import *
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Windows")
Label(text="Contact List").grid(row=0,column=0,columnspan=2)
Text(width=30,height=15).grid(row=1,rowspan=9, column=0,columnspan=2,padx=20)
Button(text="Display Contact").grid(row=10, column=0,columnspan=2,pady=10)
Label(text="Last Name:").grid(row=11, column=0,pady=10)
Entry().grid(row=11,column=1)
Button(text="Search").grid(row=12,column=0,columnspan=2)
Label(text="New Contact").grid(row=0,column=2,columnspan=2)
Label(text="First Name:").grid(row=1,column=2,sticky=E)
Entry().grid(row=1,column=3)
Label(text="Last Name:").grid(row=2,column=2,sticky=E)
Entry().grid(row=2,column=3)
Label(text="Phone #:").grid(row=3,column=2,sticky=E)
Entry().grid(row=3,column=3)
friend_check = IntVar()
Checkbutton(variable=friend_check, command = self.friend_box, onvalue=1, offvalue=0, text = "Friend").grid(row=4,column=3,sticky=W)
#Label(text="Friend").grid(row=4,column=3,padx=20,sticky=W)
Label(text="Email:").grid(row=5,column=2,sticky=E)
Entry().grid(row=5,column=3)
Label(text="Birthday:").grid(row=6,column=2,sticky=E)
Entry().grid(row=6,column=3)
Button(text="Add Contact").grid(row=7,column=3,sticky=E)
def friend_box(self):
if self.friend_check.get() == 1:
print '1'
else:
print '0'
def main():
root = Tk()
root.geometry("600x450+900+300")
root.resizable(0,0)
app = Example(root)
root.mainloop()
if __name__ == '__main__':
main()
This is the error mentioned above:
AttributeError: Example instance has no attribute 'friend_check'
Exception in Tkinter callback
How can I avoid this error?
You're taking too many shortcuts in your code. Let's look at an example:
Label(text="Contact List").grid(row=0,column=0,columnspan=2)
This creates a Label, but doesn't save a reference to it. It will display in the GUI, but if you ever want to refer back to it, you'll be unable to. This is important when you have something like an Entry widget, to which you're pretty much guaranteed to want to use again (for the get()).
Another issue is that you have the geometry management chained to the widget creation. If you did save a reference to this, it would simply point to None, which is the value returned by geometry management methods.
To fix this, unchain the statements and save a reference:
self.cl_label = Label(text="Contact List")
self.cl_label.grid(row=0,column=0,columnspan=2)
Do this for each widget you create.
For friend_check, you need to make it an instance variable instead of a local variable, as local variables are not usable outside their scope and get discarded when the containing function ends. Do this by prepending self. to the reference name.
self.friend_check = IntVar()

pong game two players tkinter error

i tried to execute this code:
import Tkinter as tk
import tkFont
import functools
import math
import random
import time
class Pong(tk.Canvas):
DEFAULTS = dict(width=640, height=480,background='black',highlightthickness=0)
def main(cls):
root = tk.Tk()
root.title('Pong')
root.resizable(False, False)
root.bind_all('<Escape>', lambda event: root.destroy())
game = cls(tkFont.Font(family='Book Antiqua', size=15, weight='bold'), 5, 100,background='black', width=640, height=480)
game.grid()
root.mainloop()
but i had those errors:
Traceback (most recent call last):
line 413, in <module>
pong.main()
, line 17, in main
game = cls(tkFont.Font(family='Book Antiqua', size=15, weight='bold'), 5, 100,background='black', width=640, height=480)
AttributeError: Pong instance has no __call__ method
I can see one main problem so far
cls coming in as a variable
def main(cls):
Then you are using it like a function
game = cls(tkFont.Font(family='Book Antiqua', size=15, weight='bold'), 5, 100,background='black', width=640, height=480)
As you mentioned in the comments, this is how you are creating pong and calling main:
if __name__ == '__main__':
pong = Pong()
pong.main()
At the time where you call Pong() you are creating an instance. As such the main method you are calling is an instance method where the first implicit parameter is the instance itself. Usually this parameter is named self for instance methods—not cls as you did.
So if we rename it, it’s clear what’s failing afterwards:
game = self(tkFont.Font(…), 5, 100, background='black', width=640, height=480)
Keep in mind that self is an object, and instance of Pong. So it’s not a function and not a constructor you can call; you are actually trying to call the instance which won’t work here.
I think what you actually want to do instead is have the main as a “static” method, being the entry point to your application. In that case, you should add the #classmethod decorator in front of the method declaration:
#classmethod
def main(cls):
root = tk.Tk()
# …
In that case, cls will be indeed the type Pong, which you can call later to create a Pong object.
You should change your __name__ == '__main__ part to just Pong.main() though, as you are no longer calling main for an instance but the whole type.
if __name__ == '__main__':
Pong.main()
You are doing a very strange thing -- you are creating a subclass of tk.Canvas, yet you don't initialize tk until after you create this object. That's not the right way to be using tkinter. You should initialize Tk outside of the scope of this class, or don't inherit from tk.Canvas.
I recommend you restructure your code so that you initialize tkinter first, and then create the instance of your game.
For example:
import Tkinter as tk
class Pong(tk.Canvas):
def __init__(self, root):
tk.Canvas.__init__(self, root)
<put other "main" code here if you want>
if __name__ == "__main__":
root = tk.Tk()
pong = Pong(root)
pong.pack(fill="both", expand=True)
root.mainloop()

{PYTHON + TKINTER} "Type cast" problem: How to retrieve Custom frame objects from a Pmw.NoteBook?

I am writing a GUI Python application.
I am using Tkinter + PythonMegaWidgets for semplicity reasons.
Going straight to the point, I need to extend Tkinter.Frame baseclass, by adding some custom member functions which provides extra-functionalities.
These "Custom Frames" will be added to single tabs of a Pmw.NoteBook object.
Official related docs can be found at: http://pmw.sourceforge.net/doc/NoteBook.html
Later, I need to retrieve "Custom Frame" instances from NoteBook, invoking custom member functions that I have added; here the problems begins...
despite the principle of Duck Typing, I can not access ANY of these "extra-methods" because the methods of Pmw.NoteBook class can only return Frame objects!.
I can not find any solution.
Below a piece of sample (more simplified) code which describes in detail my issue.
from Tkinter import *
from Pmw import NoteBook
# I define a custom frame, by extending Tkinter.Frame baseclass
class CustomPanel(Frame):
def __init__(self, master, _text):
Frame.__init__(self, master)
self.label = Label(self, text=_text)
self.localVariable = "Hello, world!" # I define a new local variable
self.label.pack()
self.pack()
# I define a custom member function, which I _ABSOLUTELY_ want to be accessible.
def customMethod(self):
print self.localVariable
# main frame of application
class MyFrame(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.noteBook = NoteBook(self) # I create a NoteBook object...
tab1 = self.noteBook.add("tab 1") # then, I add one (empty) tabs to it
panel1 = CustomPanel(tab1, "hello")
self.button = Button(self, text="Call CustomMethod()!", command=self.callCustomMethod) # I add a button test
self.noteBook.grid()
self.button.grid(row=1)
self.pack()
self.mainloop()
# I define click handler for button,
def callCustomMethod(self):
panel1 = self.noteBook.page(0) # I try to get frame contained in current tab
# pane11 is supposed to be a 'CustomPanel' object;
try:
panel1.customMethod() # ...then, custom method should be accessible
except AttributeError:
print 'AttributeError!'
# for illustration purpose only, I show that Panel1 is a 'Frame'(superclass) object only!
print panel1.__class__
frame = MyFrame() # create a MyFrame instance
Pressing the button, console output is:
AttributeError!
Tkinter.Frame
to anticipate objections:
1- Set panel1.class attribute, as showed below,
try:
panel1.__class__ = CustomPanel
panel1.customMethod() # ...then, custom method should be accessible
except AttributeError:
print 'AttributeError!'
DON'T work, because customMethod() could not access in any case to localVariable, which is declared in CustomPanel subclass only;
2- I can not even recall CustomPanel constructor, because this will RESET original member variables, which I want to retrieve with their original values.
Any help is appreciated.
IT
I don't think you need to change the class of the notebook tab, you can just add your custom frame inside that tab. Just tell the frame what your inner frame is, and remember to pack your inner frame inside the tab frame.
For example:
class MyFrame(Frame):
def __init__(self, master=None):
...
panel1 = CustomPanel(tab1, "hello")
panel1.pack(fill="both", expand=True)
tab1.inner_panel = panel1
...
def callCustomMethod(self):
tab1 = self.noteBook.page(0)
panel1 = tab1.inner_panel
...

Categories