LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-16-2012, 10:05 PM   #1
amcleod
LQ Newbie
 
Registered: Jul 2007
Location: Barbados
Distribution: Slackware
Posts: 15

Rep: Reputation: 0
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.)
 
Old 07-17-2012, 10:36 AM   #2
amcleod
LQ Newbie
 
Registered: Jul 2007
Location: Barbados
Distribution: Slackware
Posts: 15

Original Poster
Rep: Reputation: 0
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.
 
Old 07-17-2012, 01:43 PM   #3
audriusk
Member
 
Registered: Mar 2011
Location: Klaipėda, Lithuania
Distribution: Slackware
Posts: 361

Rep: Reputation: 199Reputation: 199
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)
 
Old 07-17-2012, 11:15 PM   #4
amcleod
LQ Newbie
 
Registered: Jul 2007
Location: Barbados
Distribution: Slackware
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks! With your suggested changes, everything now works as expected. I appreciate your taking the time to explain.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I import Tkinter into Python mugglemage Linux - Software 5 07-02-2008 08:47 AM
tkinter integration to python 2.3.4 gaddargarson Programming 1 03-09-2005 04:58 AM
Python and Tkinter 1337 Twinkie Fedora 2 08-03-2004 11:35 AM
Python: Tkinter. Chu Programming 0 11-10-2003 01:56 AM
Python and Tkinter Error Gerardoj Programming 1 11-08-2003 09:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration