Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tkinter question again

Status
Not open for further replies.

lw22

Programmer
Feb 19, 2005
7
US
alright i'm consulting a tkinter forum right now on this but i figured that i would ask here too.

i have a basic program that i would like to put into a class so i can use functions inside it. for example this is what i got so far.

Code:
from Tkinter import *
root = Tk()
root.geometry('600x200')
c1 = "black"
p1 = 2
wd = 4
typeguess = StringVar()
Entry(root, textvariable = typeguess).grid(row = 4, column = 2, padx = p1, pady = p1, columnspan = 3)
Label(root, text = "Pick a Letter ").grid(row = 4, column = 0, padx = p1, pady = p1, columnspan = 2)
Button(root, text = "Go ", fg = c1, width = 3).grid(row = 4, column = 5, padx = p1, pady = p1)
Button(root, text = "Q", fg = c1, borderwidth = 3, relief = 'raised', width = wd).grid(row = 3, column = 0, padx = p1, pady = p1)
Button(root, text = "W", fg = c1, borderwidth = 3, relief = 'raised', width = wd).grid(row = 3, column = 1, padx = p1, pady = p1)
Button(root, text = "E", fg = c1, borderwidth = 3, relief = 'raised', width = wd).grid(row = 3, column = 2, padx = p1, pady = p1)
Button(root, text = "R", fg = c1, borderwidth = 3, relief = 'raised', width = wd).grid(row = 3, column = 3, padx = p1, pady = p1)
Button(root, text = "T", fg = c1, borderwidth = 3, relief = 'raised', width = wd).grid(row = 3, column = 4, padx = p1, pady = p1)
Button(root, text = "Y", fg = c1, borderwidth = 3, relief = 'raised', width = wd).grid(row = 3, column = 5, padx = p1, pady = p1)
root.mainloop()


i tried to put it into a class and this is what i came out with...

Code:
from Tkinter import *
root = Tk()
class App:
    def __init__(self):
        self.geometry('600x200')
        c1 = "black"
        p1 = 2
        wd = 4
        self.typeguess = StringVar()
        self.Entry(root, textvariable = typeguess).grid(row = 4, column = 2, padx = p1, pady = p1, columnspan = 3)
        self.Label(root, text = "Pick a Letter ").grid(row = 4, column = 0, padx = p1, pady = p1, columnspan = 2)
        self.Button(root, text = "Go ", fg = c1, width = 3).grid(row = 4, column = 5, padx = p1, pady = p1)
        self.Button(root, text = "Q", fg = c1, borderwidth = 3, relief = 'raised', width = wd).grid(row = 3, column = 0, padx = p1, pady = p1)
        self.Button(root, text = "W", fg = c1, borderwidth = 3, relief = 'raised', width = wd).grid(row = 3, column = 1, padx = p1, pady = p1)
        self.Button(root, text = "E", fg = c1, borderwidth = 3, relief = 'raised', width = wd).grid(row = 3, column = 2, padx = p1, pady = p1)
        self.Button(root, text = "R", fg = c1, borderwidth = 3, relief = 'raised', width = wd).grid(row = 3, column = 3, padx = p1, pady = p1)
        self.Button(root, text = "T", fg = c1, borderwidth = 3, relief = 'raised', width = wd).grid(row = 3, column = 4, padx = p1, pady = p1)
        self.Button(root, text = "Y", fg = c1, borderwidth = 3, relief = 'raised', width = wd).grid(row = 3, column = 5, padx = p1, pady = p1)
app  = App(root)
root.mainloop()



any help?
 
First problem, when you pass root to the constructor, root is not the first argument.

Example:
Code:
class mine:
  def __init__( self, thing ):
    pass

inst = mine( 1 )

When you make that call, self is a pointer to the instance, and *thing* is equal to 1.

In order to do what you want, first I need to ask if "Tk" in the second line is a class or just a function?

If it's a call, then you would subclass it with:
Code:
class mine( Tk ):
  def __init__( self ):
    # now self is the same as root
    # but you have to call the parent constructor
    Tk.__init__( self )

Note: Tk.__init__( self ) works because you are calling the __init__ method of the Tk class as if it were a common function (statically) and not instantiating the class, therefore passing self to the function makes self the first arguement of the function.

If it isn't a class, but simple a function in the Tkinter namespace, then you'll do this:
Code:
class mine:
  def __init__( self, root ):
    self.root = root
    # now your can operate on self.root

inst = mine( Tk() )

or:
Code:
class mine:
  def __init__( self ):
    self.root = Tk()

And, by the way, since this isn't a question about using Tk function in Tkinter, but actually a question about python classes, it is most definitely appropriate for this forum.
 
Change:
If it's a call, then you would subclass it with:

to:
If it's a class, then you would subclass it with:
 
ok well to my understanding this is how you would set up a 300x200 grid using the method above...

Code:
from Tkinter import *
class App:
    def __init__(self):
        Tk.__init__( self )
        self.geometry('300x200')
root = Tk()
app  = App(root)
root.mainloop()

that returns this error

Traceback (most recent call last):
File "C:/python2.4/class setup1.py", line 7, in ?
app = App(root)
TypeError: __init__() takes exactly 1 argument (2 given

what am i missing?
 
im sorry i don't understand. i'm trying to figure it out as i go along. im about as much as a beginner as you can get.
 
I don't know if I can explain it any more clearly. "self" is an implicit argument, it is passed by python and does *not* correspond to the first arguement given in the instantiation call. The first argument you pass when creating a new object is the *second* named argument in the constructor declaration.
 
i completely understand now. it just took me a little bit. at first i thought when you send argument i thought you meant the line beneath the class. you are a god ericbrunson.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top