Newbie Tkinter questions

Discussion in 'Python' started by Doru-Catalin Togea, Jun 27, 2003.

  1. Hei!

    I am trying to get up and working with Tkinter fast. I have some
    documentation like Fredrik Lundh's An Introduction to Tkinter.

    Can you please tell what I am missing in the following programme:

    from Tkinter import *

    ########################################################################
    # GLOBAL VARIABLES
    ########################################################################

    root = Tk()
    schools = [("HIO", "1"), ("NITH", "2"),]
    rbSchools = None # initialize later on to a Radiobutton
    # object
    lArchiveNo = None # Label
    eArchiveNo = None # Entry
    v = StringVar()
    v.set("0") # For the Radiobutton: no choice

    ########################################################################
    # FUNCTIONS
    ########################################################################

    def chooseSchool(value):
    print "you have chosen Radiobutton no.", value

    ########################################################################
    # MAIN PROGRAMME
    ########################################################################

    lArchiveNo = Label(root, text = "Sak nr.: ")
    lArchiveNo.grid(row = 0, column = 0)
    lArchiveNo.pack(side=LEFT)

    eArchiveNo = Entry(root, width = 20)
    eArchiveNo.grid(row = 0, column = 1)
    eArchiveNo.pack(side=LEFT)

    # make the radio button for schools:
    rbSchools = Radiobutton(root, name = "rbSchools")
    #rbSchools.bind("<Button-1>", chooseSchool)
    #print str(rbSchools)
    for text, value in schools:
    rbSchools = Radiobutton(root, text=text, variable=v, \
    value=value, command=chooseSchool(value))
    rbSchools.grid(row = 1, column = 0)
    rbSchools.pack(anchor=W)

    root.mainloop()

    1) Is the grid used with the Label and the Entry the same as the grid in
    the for-loop? (it doesn't seem so, I want the Entry after the Label in the
    1st row and the Radiobutton in the 2nd row, first cell or columnspan = 2).

    2) How do I capture the change on the Radiobutton? As it is now, the
    programme calls chooseSchool with the right value at creation time, not
    when I click the Radiobutton. (Or maybe it is called when I click the
    Radiobutton as well, but the event is captured and ignored before it
    reaches the Radiobutton???)

    Thanks if you can help. Is there any documentation which is more detailed
    than Lundh's introduction?

    Catalin

    --

    <<<< ================================== >>>>
    << We are what we repeatedly do. >>
    << Excellence, therefore, is not an act >>
    << but a habit. >>
    <<<< ================================== >>>>
    Doru-Catalin Togea, Jun 27, 2003
    #1
    1. Advertising

  2. Doru-Catalin Togea <> wrote in message news:<>...
    <code snipped>
    Hello,

    > 1) Is the grid used with the Label and the Entry the same as the grid in
    > the for-loop? (it doesn't seem so, I want the Entry after the Label in the
    > 1st row and the Radiobutton in the 2nd row, first cell or columnspan = 2).

    Yes. Within the same window/frame, the same grid is used. See below,
    and the appropriate section in the Introduction to Tkinter by Frederik
    Lundh.

    > 2) How do I capture the change on the Radiobutton? As it is now, the
    > programme calls chooseSchool with the right value at creation time, not
    > when I click the Radiobutton. (Or maybe it is called when I click the
    > Radiobutton as well, but the event is captured and ignored before it
    > reaches the Radiobutton???)

    Linking a Radiobutton(or Checkbox, for that matter) to a String/IntVar
    is generally to best way. Changing the Var automatically updates the
    state of the Radio/Check.

    From the code, there are a few comments I'd make:

    -The grid(, place) and pack geometry managers should not be used
    within the same container widget (meaning Frame,Toplevel,etc). Tk will
    happily spend the rest of your lifetime trying to find a placement
    that all are happy with. I think what you want to use is the grid
    manager with the 'sticky' option. See grid manager doc in the back of
    the I2Tkinter

    -In Python, names are bound to objects the first time they are used.
    There is no need to bind them to objects you aren't going to use. It
    looks like you're trying to declare names, which you don't need to do.

    -This is a very minor point. However, when using Tkinter, you only
    need to bind names to object you want to keep a track of. Labels, for
    example, unless you want to change the contents at some point, can be
    created without a name. Radiobuttons, too, if you've linked the button
    state to a variable, don't need names. Tk will keep a track of them.
    i.e.:
    # Static label
    Label(root, text="My label").grid(row=0,column=0,sticky=W)
    # Radiobutton linked to IntVar
    curstate=IntVar() # This is what matters
    curstate.set(0)
    Radiobutton(root,text="On/Off",value=1,variable=curstate).grid(column=2,row=0)
    #But!
    myentry=Entry(root) # Entrybox you'll want to access
    myentry.grid(column=1,row=0)

    -Names can only apply to one object at a time. In your code, you
    rebind the rbSchools name 4 times. If you want to dynamically create
    instances, append them to a list. It's more flexible. However, it
    looks like what you really what is to give all your Radiobuttons the
    same IntVar as a variable option. Then you dpn't need to give them
    names at all. See previous point.

    -If you want real control over your gridding of widgets, don't put
    them in the same grid square. What will happen is that the first
    widget will get put in the right place, and the second will be put
    anywhere the manager considers the next best (usually the line below,
    which is why it works right in your code).

    > Thanks if you can help. Is there any documentation which is more detailed
    > than Lundh's introduction?


    IMO, between the Python Library Reference
    (http://www.python.org/doc/lib/) and the Introduction you have, you
    should have enough to start. Section II really is indispensible when
    you're trying something new. Make sure to read the option list and
    description for each new widget and experiment!

    Alternatively read the Tk documentation, GIYF. However, for a newbie
    it can be a little daunting. Get to know how the system works before
    trying to translate Tk->Tkinter.

    HTH,
    Paul

    >
    > Catalin
    Paul Simmonds, Jun 27, 2003
    #2
    1. Advertising

  3. Doru-Catalin Togea

    Peter Abel Guest

    Doru-Catalin Togea <> wrote in message news:<>...
    > Hei!
    >
    > I am trying to get up and working with Tkinter fast. I have some
    > documentation like Fredrik Lundh's An Introduction to Tkinter.
    >
    > Can you please tell what I am missing in the following programme:
    >
    > from Tkinter import *
    >
    > ########################################################################
    > # GLOBAL VARIABLES
    > ########################################################################
    >
    > root = Tk()
    > schools = [("HIO", "1"), ("NITH", "2"),]
    > rbSchools = None # initialize later on to a Radiobutton
    > # object
    > lArchiveNo = None # Label
    > eArchiveNo = None # Entry
    > v = StringVar()
    > v.set("0") # For the Radiobutton: no choice
    >
    > ########################################################################
    > # FUNCTIONS
    > ########################################################################
    >
    > def chooseSchool(value):
    > print "you have chosen Radiobutton no.", value
    >
    > ########################################################################
    > # MAIN PROGRAMME
    > ########################################################################
    >
    > lArchiveNo = Label(root, text = "Sak nr.: ")
    > lArchiveNo.grid(row = 0, column = 0)
    > lArchiveNo.pack(side=LEFT)
    >


    grid and pack doen't work togather. Use either grid or pack.

    > eArchiveNo = Entry(root, width = 20)
    > eArchiveNo.grid(row = 0, column = 1)
    > eArchiveNo.pack(side=LEFT)
    >


    grid and pack doe ....

    > # make the radio button for schools:
    > rbSchools = Radiobutton(root, name = "rbSchools")
    > #rbSchools.bind("<Button-1>", chooseSchool)
    > #print str(rbSchools)
    > for text, value in schools:
    > rbSchools = Radiobutton(root, text=text, variable=v, \
    > value=value, command=chooseSchool(value))
    > rbSchools.grid(row = 1, column = 0)
    > rbSchools.pack(anchor=W)


    grid and pack doe ....

    >
    > root.mainloop()
    >
    > 1) Is the grid used with the Label and the Entry the same as the grid in
    > the for-loop?


    Yes it is.

    > (it doesn't seem so, I want the Entry after the Label in the
    > 1st row and the Radiobutton in the 2nd row, first cell or columnspan = 2).
    >


    But you have to use a variable for row or column and no constants.
    Otherwise several widgets in each loopcount are in the same gridcell.

    > 2) How do I capture the change on the Radiobutton? As it is now, the
    > programme calls chooseSchool with the right value at creation time, not
    > when I click the Radiobutton. (Or maybe it is called when I click the
    > Radiobutton as well, but the event is captured and ignored before it
    > reaches the Radiobutton???)
    >


    You have to declare a Tkintervariable and assign its name to the option
    **variable**.
    A group of Radiobuttons must have the same Tkintervariable. Furthermore you
    have assign a value to the option **value*, where the variable's value will
    be set to if the equivalent Radiobutton has been pressed by the user.

    > Thanks if you can help. Is there any documentation which is more detailed
    > than Lundh's introduction?
    >


    I think Lundh's introduction is very helpfull and detailed though it might
    be not quite complete. He seems to me as one of the **Tkinter-Popes**.
    He has some more information around Python, Tkinter etc. at
    http://effbot.org/zone/.
    In addition have a look on the doku at
    http://www.nmt.edu/tcc/help/pubs/lang.html.

    > Catalin


    Regards
    Peter
    Peter Abel, Jun 27, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Epler
    Replies:
    0
    Views:
    504
    Jeff Epler
    Aug 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    450
    Jeff Epler
    Aug 23, 2004
  3. Ali Syed
    Replies:
    3
    Views:
    551
    Mark McIntyre
    Oct 13, 2004
  4. Pierre Dagenais
    Replies:
    0
    Views:
    308
    Pierre Dagenais
    Aug 3, 2008
  5. Hidekazu IWAKI
    Replies:
    1
    Views:
    376
    Peter Otten
    Dec 14, 2009
Loading...

Share This Page