LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Unexpected behaviour with Python / Tkinter (https://www.linuxquestions.org/questions/programming-9/unexpected-behaviour-with-python-tkinter-4175417072/)

amcleod 07-16-2012 10:05 PM

Unexpected behaviour with Python / Tkinter
 
New to Python & Tkinter, so need some help.

When you do
Code:

Label( framename, ...)
Isn't the label supposed to appear inside the named frame? I'm running the following script:

Code:

#!/usr/bin/python

from Tkinter import *

# create toplevel container widget
root = Tk()
root.config( bg='blue' )

# create separate container frames for header line and three data lines
hdr = Frame( root, height=16, bg='yellow').pack(side=TOP, fill=X)
ln1 = Frame( root, height=16, bg='green').pack(side=TOP, fill=X)
ln2 = Frame( root, height=16, bg='pink').pack(side=TOP, fill=X)
ln3 = Frame( root, height=16, bg='orange').pack(side=TOP, fill=X)

d0 = Label( hdr,  bg='cyan', width=10, text='DATE' ).pack( side=LEFT )
d1 = Label( ln1, bg='gray', width=10, text='2007-06-13' ).pack( side=LEFT )
d2 = Label( ln2, bg='cyan', width=10, text='2012-04-09' ).pack( side=LEFT )
d3 = Label( ln3, bg='gray', width=10, text='1997-11-27' ).pack( side=LEFT )

t0 = Label( hdr, bg='magenta', width=6, text='TIME' ).pack( side=LEFT )
t1 = Label( ln1, bg='purple', width=6, text='12:45' ).pack( side=LEFT )
t2 = Label( ln2, bg='magenta', width=6, text='13:25' ).pack( side=LEFT )
t3 = Label( ln3, bg='purple', width=6, text='11:06' ).pack( side=LEFT )

root.mainloop()

Labels aren't appearing inside container frames as I expect, but are inside the root frame after the container frames. Am I mistaken in thinking that a label should appear inside it's parent? If so, how do I get a label to appear inside a particular frame of choice? (Weird set of colours solely so I can see what ends up where.)

amcleod 07-17-2012 10:36 AM

In comparison, this script:

Code:

#!/usr/local/bin/perl -w

use Tk;

$root = new MainWindow( -bg => 'blue' );

$hdr = $root->Frame( -height=>16, -bg=>'yellow' )->pack( -side=>'top', -fill=>'x' );
$ln1 = $root->Frame( -height=>16, -bg=>'green' )->pack( -side=>'top', -fill=>'x' );
$ln2 = $root->Frame( -height=>16, -bg=>'pink' )->pack( -side=>'top', -fill=>'x' );
$ln3 = $root->Frame( -height=>16, -bg=>'orange' )->pack( -side=>'top', -fill=>'x' );

$d0 = $hdr->Label( -bg=>'cyan', -width=>10, -text=>'DATE' )->pack( -side=>'left' );
$d1 = $ln1->Label( -bg=>'gray', -width=>10, -text=>'2007-06-13' )->pack( -side=>'left' );
$d2 = $ln2->Label( -bg=>'cyan', -width=>10, -text=>'2012-04-09' )->pack( -side=>'left' );
$d3 = $ln3->Label( -bg=>'gray', -width=>10, -text=>'1997-11-27' )->pack( -side=>'left' );

$t0 = $hdr->Label( -bg=>'magenta', -width=>6, -text=>'TIME' )->pack( -side=>'left' );
$t1 = $ln1->Label( -bg=>'purple', -width=>6, -text=>'12:45' )->pack( -side=>'left' );
$t2 = $ln2->Label( -bg=>'magenta', -width=>6, -text=>'13:25' )->pack( -side=>'left' );
$t3 = $ln3->Label( -bg=>'purple', -width=>6, -text=>'11:06' )->pack( -side=>'left' );

MainLoop;

behaves just as I would have expected.

audriusk 07-17-2012 01:43 PM

This is because when you instantiate Frame class like this
Code:

hdr = Frame(root, height=16, bg='yellow').pack(side=TOP, fill=X)
first you get its instance, but the instance method pack(), which is called immediately, doesn't return the instance itself. In Python functions, that don't have return statement, return None. So, your hdr variable ends up refering to None. Here's what you should do instead:
Code:

hdr = Frame(root, height=16, bg='yellow')
hdr.pack(side=TOP, fill=X)


amcleod 07-17-2012 11:15 PM

Thanks! With your suggested changes, everything now works as expected. I appreciate your taking the time to explain.


All times are GMT -5. The time now is 07:32 PM.