Hello, Again.
When you write larger programs, it's usually a good idea to wrap your code up into one or more classes. The following example is adapted to the "Hello World" program in Matt Conway's A tkinter life preserver (dead link).
It's a good idea to wrap code into one or more classes when writing large applications. The following example is adapted from the "Hello World" in Matt Conway's A Tkinter life preserver (dead link). Our Second tkinter
From tkinter Import *
class App:
def __init__ (self, Master):
frame = FRAME (master)
Frame.pack ()
Self.button = button (
frame, text= "QUIT", fg= "Red", Command=frame.quit
)
self.button.pack (Side=left)
self.hi_there = button (frame, text= "Hello", Command=self.say_hi)
self.hi_there.pack (side=left)
def Say_hi (self):
print "Hi there, everyone!"
root = Tk ()
app = app (root)
root.mainloop ()
Root.destroy () # optional; description below
Running the Example
When you run this example, the following window appears.
When you run this program, the following window will appear Running the (using Tk 8.0 on a Windows box)
If you click on the right button, the text "Hi there, everyone!" are printed tothe console. If you click the "left button", the program stops.
If you click the button on the right, you will print "Hi there, everyone!" inside the console. If you click the button on the left, the program will stop.
Note: Some Python development environments have problems running tkinter like this one. The problem is usually this enviroment uses Tkinter itself, and the Mainloop call and the Quit Interact with the environment ' s expectations. Other environments could misbehave if you are leave out of the explicit destroy call. If The example doesn ' t behave as expected, check for tkinter-specific documentation for your development.
Note: Some python development environments may have problems running like the Tkinter example above. The problem is usually that the environment uses the Tkinter itself, and the call to Mainloop and quit affects the environmental expectations. If you omit an explicit call to destroy, the other environment produces an abnormal behavior. If this example does not behave as expected, check the tkinter-specific documentation for the development environment. Details
This sample application is written as a class. The constructor (the __init__method) are called with a parent widget (the master) to which it adds a num ber of child widgets. The constructor starts by creating a Frame widget. A frame is a simple container, and are in this case only used to hold two widgets.
This example application is written to a class. This constructor (__init__ method) is invoked by a parent component (master). constructor to create a frame component to begin with. This framework is a simple container that is used to accommodate two other components.
Class App:
def __init__ (self, Master):
frame = FRAME (master)
Frame.pack ()
The frame instance is stored into a local variable called frame. After creating the widget, we immediately call the pack method to make the frame visible.
This frame instance is stored in a local variable of a written frame. After the creation is complete, you can immediately invoke the pack method to make the frame visible.
We then create two Button widgets, as children to the frame.
Create two button components as a child of the frame.
Self.button = button (frame, text= "QUIT", fg= "Red", Command=frame.quit)
self.button.pack (side=left)
Self.hi_ there = button (frame, text= "Hello", Command=self.say_hi)
self.hi_there.pack (Side=left)
This time, we pass a number of
OptionsTo the constructor, as keyword arguments. The "QUIT" is labelled "" "," "" "" and "" Made Red "(
FGis short for
foreground). The second is labelled "Hello". Both Buttons also take a
CommandOption. This option specifies a function, or (as) a bound method, which would be called when the button is clicked.
This time passed some options for this construct as a key parameter. The first button is labeled "QUIT" and is labeled Red (FG is the abbreviation for foreground). The second is labeled "Hello". Both buttons have a command option that assigns a function or a bound method that is invoked when the button is clicked.
The button instances are stored in instance attributes. They are both packed, but this time with the side=left argument. This means that they would be placed as far left as possible in the frame; The the ' the ' the ' the ' placed at the ' second ', and the ' the ' is placed just to the EFT edge of the remaining spaces in the frame, which is. By default, widgets are packed relative to their parent (which are master for the frame widget, and the frame Itse LF for the buttons). If the side is isn't given, it defaults to top.
The button instance is stored in the instance property. They are all packaged with a parameter side=left. This means that they are placed on the leftmost side of the frame. The first button is positioned at the far left of the frame, and the second button is immediately to the right of the first button (this is the leftmost part of the frame's remaining space). In general, component packaging is about the parent component (Master for a framework component and the framework itself for a button). If the side parameter is not given, the default value is top.
The "Hello" button callback is given next. It simply prints a message to the console everytime the button is pressed:
Next, the callback function for the "Hello" button is assigned. It simply prints a message in the console when the button is pressed.
def say_hi (self):
print "Hi There,everypne!"
Finally, we provide some script level code that creates a
TkRoot widget, and one instance of the
Appclass using the root widget as its parent:
Finally, provide some scripting-level code to create a TK root component and an app class instance that uses the root component as the parent component
root = Tk ()
app = app (root)
root.mainloop ()
root.destory ()
The
MainloopCall enters the Tk event loop, in which the application would stay until the
quitThe method is called (just click the QUIT button), or the window is closed.
The Mainloop call goes into the TK event loop until the Quit method is invoked (just click on the Quit button) or the application will stay in the event loop until the window is closed.
The destroy call are only required if your run this example under certain development; It explicitly destroys the main window when the event loop is terminated. Some development environments won ' t terminate the Python process unless this are done.
You need to invoke the Destroy method only if you are running this example in a particular development environment. When the event loop ends it will explicitly destroy the main window. If you don't, some development environments will not end the Python process. More on widget references
In the second example, the "frame widget is stored in a" variable named frame, while the button widgets are S Tored in two instance attributes. Isn ' t there a serious problem hidden in here:what happens when the __init__ function returns and the frame c4> variable goes out of scope?
In the second example, the frame component is stored in a local variable named frame, and the button component is stored in two instance properties. This is not a serious problem to hide "what happens when the __init__ function returns and the frame variable goes out of range."
Just Relax; There ' s actually no need to keep a reference to the widget instance. Tkinter automatically maintains a widget tree (via the master and children attributes of each widget ins tance), so a widget won ' t disappear then the application ' s last reference goes; It must be explicitly destroyed before this happens (using the Destroy method). But If you wish to do something and the widget after it has been created, you better keep a reference to the widget Insta nCE yourself.
Relax, in fact you don't need to keep a reference to the hold reference component instance. Tkinter automatically maintains a component tree (through the master and children attributes of each component instance), so the component does not disappear until the last reference in the application is left, and must be shown to destroy it (using the Destroy method). But if you want to do something about a component after it is created, it's best to keep the reference to the component instance.
Note this if you don ' t need to keep a reference to a widget, it might is tempting to create and pack it in a single line:
Note If you do not need to keep a reference to a component, you can try to create and pack it by one line.
button (frame, text= "Hello", Command=self.hello). Pack (Side=left)
The result of Don ' t store is from this operation; You'll only be disappointed when you try to use this value (the
Packmethod returns
None). To is on the safe side, it might is better to always separate construction from packing:
Do not store the return value of this operation, and you will be disappointed when doing so (the pack method returns none). To be safe, it's best to separate to construct.
w = button (frame, text = "Hello", command = Self.hello)
w.pack (Side=left)
More on widget names
Another source of confusion, especially for those who have some experience of programming Tk-using TCL, is Tkinter ' s notio N of the widget name. In TCL, your must explicitly name each widget. For example, the following TCL command creates a button named "OK", as a child to a widget named "Dialog" (which In turn are a child of the root window, ".").
Another confusion, especially for those who have the experience of using TCL for TK programming, is the Tkinter component name concept. In TCK, you must explicitly name each component, such as the following TCL command to create a button named "OK" as a child of the component named "dialog".
Button.dialog.ok
The corresponding Tkinter call would look like:
The correct tkinter call should be this:
OK = Button (Dialog)
However, in the "Tkinter case", OK and dialog are references to widget instances, not the actual of The widgets. Since Tk itself needs the names, Tkinter automatically a unique name to each new widget. In the above case, the dialog name are probably something like ". 1428748," and the button could ". 1428748.1432920" . If you are wish to get the full name of a Tkinter widget, simply use the " str " function on the widget instance:
In short, in the Tkinter example, OK and dialog are references to component instances, not real names. Because TK itself requires a name, Tkinter automatically assigns a unique name to each new component. In the example above, the dialog name might be something like ". 1428748", and the button might be ". 128748.1432920". If you want the full name of a Tkinter component, you can use the STR function of the component instance:
>>>print str (OK)
1428748.1432920
(If you print something, Python automatically uses the STR function to find out what to print.) But Obviously, a operation like "name = OK" won ' t does the that, so make sure always to explicitly use str if you Need the name).
(If you want to print something, Python automatically uses the STR function to find out what to print.) But obviously an operation like "name = OK" does not do this, so use STR explicitly when you need to get a name
If you are really need to specify the name of a widget, you can use the name option while you create the widget. One (and most likely the "only") reason for the ' is ' if you need to interface with code written in TCL.
If you need to specify a name for a component, you can use the name option when you create the component. The reason for this may be that you need the TCL code interface.
In the following example, the resulting widget is named ". Dialog.ok" (or, if your forgot to name the dialog, Somet Hing like ". 1428748.ok"):
In the following example, the result is that the component is named ". Dialog.ok" (or something like ". 1428748" If you forget to name the session)
OK = Button (dialog, name = ' OK ')
To avoid conflicts with Tkinter's naming scheme, don ' t use names which only contain digits. Also Note that
nameis a "creation only" option; You are cannot change the name once you ' ve created the widget.
In order to avoid and tkinter clearly normative conflict, is to use the name of all numbers. Note option name is a "creation only" option, and once you create a component, you cannot change its name.