Some basic questions about Tkinter (probably v easy for experts!)

Neil Cerutti cerutti at together.net
Wed Feb 7 11:13:03 EST 2001


Martyn Quick posted:
>On 7 Feb 2001, Neil Cerutti wrote:
>
>> For the rest of your questions, the easiest thing will be to
>> read:
>> 
>>  http://www.pythonware.com/library/tkinter/introduction/
>
>As I said in my reply to Fredrik Lundh's answer, I did *try* to
>read this, but unfortunately I found it rather difficult.  It
>doesn't actually seem to be aimed at beginners who are new to
>Object Oriented Programming.

Oh! That'll make it difficult to follow. Sorry. Using Tkinter
isn't a good way to learn OOP.

Luckily, the semantics and syntax of OOP is easy to learn
in Python, if not the practice.

>> In particular, read Section 1, "Introducing Tkinter". But don't
>> only read it; also open a Python interpreter (a simple CLI will
>> work best), and type in the examples as they appear in the
>> manual.
>
>What's a CLI?

It stands for Command Line Interface. I meant to imply that you
should use, e.g., on Windows, python.exe instead of IDLE (the
graphical integrated development environment that comes with some
versions of Python). Tkinter acts funny under IDLE, since IDLE is
written in Tkinter.

Typing along with the demos, seeing what happens as each line
is entered, should be illuminating. It's also kind of fun.

Here's a small demo. I'm not going to use "import *" here, to
hopefully make it a little clearer. You don't need to type in the
comments. ;-)

import Tkinter

# Create the root window. All widgets and other windows will be
# controlled by this window. Tk is a "class" in the Tkinter
# library. root becomes an "instance", or instantiation, of the
# class Tk after this line is run.
root = Tkinter.Tk()

# Use the title method of the window object to set the text in the
# title bar of the window.
root.title("Greetings")

# Create an instance of Tkinter Button class, and call it
# hello_button. The Button class needs some parameters in order
# to be created properly. The first parameter tells the button
# who its "master", or controller, is. The next argument contains
# both a keyword, and its value. "text" is an option of the
# Tkinter button widget; its value becomes the text printed on
# the button. Only the first parameter (root) is required, but
# including keyword arguments is the quickest way to set the
# initial options for new widgets.
hello_button = Tkinter.Button(root, text="Hello World")

# The button has been created, but it doesn't know where to
# appear in its master. For that, you must use a "geometry
# manager". There are several available in Tkinter, but the
# easiest is "pack". "pack" is a powerful but squinchy geometry
# manager that always uses the minimum possible space.
hello_button.pack()

# There will be a button, but it doesn't do anything. You can set
# the "command" option of your button widget instance to tell
# Tkinter to run arbitrary code when the button is pressed. I
# could have set this option in the line above when the button
# was created; but since the button is already in existence, I
# use the "config" method.

# This is just a function.
def change_hello_message():
  hello_button.config(text="Salutations Planet")

hello_button.config(command=change_hello_message)

# The window won't appear and your other widgets won't function
# until:
root.mainloop()


Have fun.

-- 
Neil Cerutti <cerutti at together.net>



More information about the Python-list mailing list