468,472 Members | 1,773 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,472 developers. It's quick & easy.

Strange Tkinter Grid behaviour Problem

Hi,

Still struggling with my GUI exercise -

I have the following lines of code in a routine that is bound at <Key-Returnto
an instance of Entry :

self.disp.Amount_des = Label(self.disp, text = self.dis_string, fg =
'black', bg = 'yellow')
self.disp.Amount_des.grid(row = self.rownum, column=0, sticky = 'nesw')

self.disp.Amount = Label(self.disp, text = self.retstring, fg = 'black',
bg = 'green')
self.disp.Amount.grid(row = self.rownum, column=1, sticky = N+S+E+W)

The second call to the grid method fails as follows:

Exception in Tkinter callback
Traceback (most recent call last):
File "E:\PYTHON24\lib\lib-tk\Tkinter.py", line 1345, in __call__
return self.func(*args)
File "C:\WINDOWS\DESKTOP\Entry1.py", line 243, in entryend
self.disp.Amount.grid(row = self.rownum, column=1, sticky = N+S+E+W)
TypeError: cannot concatenate 'str' and 'instance' objects

If I change the N+S+E+W to the 'nsew' form, it works no problem...

Weird - at other places in the program the form: sticky = N+S+E+W works without
a problem.
I found the 'nsew' form by trial and error...

self.disp is a window different from the current one - it is used to display a
record entry as it is built up field by field. - the code fragment above is what
inserts the description of the entry and the entered data in a row in the window
used for displaying, when the user hits the enter key.

Is this a bug, or am I still doing something wrong?

- Hendrik

Aug 1 '06 #1
3 1709
H J van Rooyen wrote:
Hi,

Still struggling with my GUI exercise -

I have the following lines of code in a routine that is bound at
<Key-Returnto an instance of Entry :

self.disp.Amount_des = Label(self.disp, text = self.dis_string, fg
=
'black', bg = 'yellow')
self.disp.Amount_des.grid(row = self.rownum, column=0, sticky =
'nesw')

self.disp.Amount = Label(self.disp, text = self.retstring, fg =
'black',
bg = 'green')
self.disp.Amount.grid(row = self.rownum, column=1, sticky =
N+S+E+W)

The second call to the grid method fails as follows:

Exception in Tkinter callback
Traceback (most recent call last):
File "E:\PYTHON24\lib\lib-tk\Tkinter.py", line 1345, in __call__
return self.func(*args)
File "C:\WINDOWS\DESKTOP\Entry1.py", line 243, in entryend
self.disp.Amount.grid(row = self.rownum, column=1, sticky = N+S+E+W)
TypeError: cannot concatenate 'str' and 'instance' objects

If I change the N+S+E+W to the 'nsew' form, it works no problem...

Weird - at other places in the program the form: sticky = N+S+E+W works
without a problem.
I found the 'nsew' form by trial and error...

self.disp is a window different from the current one - it is used to
display a record entry as it is built up field by field. - the code
fragment above is what inserts the description of the entry and the
entered data in a row in the window used for displaying, when the user
hits the enter key.

Is this a bug, or am I still doing something wrong?
You have probably defined your own, say, E somewhere in your module:

E = ... # whatever

This can easily be fixed by using another name. But what you are really
doing wrong is using the

from Tkinter import *

star-import which drastically increases the likelihood of such name clashes.
I recommend using qualified names, abbreviated if you like:

import Tkinter as tk

.... tk.N + tk.S + tk.E + tk.W ... # a bit longer, but worth the effort

Of course this could still go wrong if you do

tk = 42

somewhere in your module...

Peter
Aug 1 '06 #2
On Tue, 01 Aug 2006 14:14:51 +0200, H J van Rooyen <ma**@microcorp.co.za
wrote:
Hi,

Still struggling with my GUI exercise -

I have the following lines of code in a routine that is bound at
<Key-Returnto
an instance of Entry :

self.disp.Amount_des = Label(self.disp, text = self.dis_string,
fg =
'black', bg = 'yellow')
self.disp.Amount_des.grid(row = self.rownum, column=0, sticky =
'nesw')

self.disp.Amount = Label(self.disp, text = self.retstring,fg =
'black',
bg = 'green')
self.disp.Amount.grid(row = self.rownum, column=1, sticky =
N+S+E+W)

The second call to the grid method fails as follows:

Exception in Tkinter callback
Traceback (most recent call last):
File "E:\PYTHON24\lib\lib-tk\Tkinter.py", line 1345, in __call__
return self.func(*args)
File "C:\WINDOWS\DESKTOP\Entry1.py", line 243, in entryend
self.disp.Amount.grid(row = self.rownum, column=1, sticky = N+S+E+W)
TypeError: cannot concatenate 'str' and 'instance' objects

If I change the N+S+E+W to the 'nsew' form, it works no problem...

Weird - at other places in the program the form: sticky = N+S+E+W works
without
a problem.
Simple: you have in this context a local or global variable named either
N, S, W or E, shadowing the constant defined in the Tkinter module. You
can find it by inserting a line like:

print repr(N), repr(S), repr(W), repr(E)

before your self.disp.Amount.grid in your code. This line should print:

'n' 's' 'w' 'e'

but I guess it won't...

The solutions are:
- Rename your variable.
- Always use the string form, i.e 'nswe'. This is the standard in native
tk. The N, S, W and E constants are just defined for convenience in
Tkinter.
- Do not use "from Tkinter import *" to import the module, but something
like "import Tkinter as tk", and then prefix all names in this module by
'tk.'. So your code above becomes:

self.disp.Amount = tk.Label(self.disp, text = self.retstring, fg =
'black', bg = 'green')
self.disp.Amount.grid(row = self.rownum, column=1, sticky =
tk.N+tk.S+tk.E+tk.W)

(I'm personnally not a big fan of this last solution, since I find it
clobbers the code a lot and decreases readability. But it's usually the
best way to avoid name clashes...)

[snip]

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Aug 1 '06 #3

"Peter Otten" <__*******@web.dewrote:
|H J van Rooyen wrote:
|
| Hi,
| >
| Still struggling with my GUI exercise -
| >
| I have the following lines of code in a routine that is bound at
| <Key-Returnto an instance of Entry :
| >
| self.disp.Amount_des = Label(self.disp, text = self.dis_string, fg
| =
| 'black', bg = 'yellow')
| self.disp.Amount_des.grid(row = self.rownum, column=0, sticky =
| 'nesw')
| >
| self.disp.Amount = Label(self.disp, text = self.retstring, fg =
| 'black',
| bg = 'green')
| self.disp.Amount.grid(row = self.rownum, column=1, sticky =
| N+S+E+W)
| >
| The second call to the grid method fails as follows:
| >
| Exception in Tkinter callback
| Traceback (most recent call last):
| File "E:\PYTHON24\lib\lib-tk\Tkinter.py", line 1345, in __call__
| return self.func(*args)
| File "C:\WINDOWS\DESKTOP\Entry1.py", line 243, in entryend
| self.disp.Amount.grid(row = self.rownum, column=1, sticky = N+S+E+W)
| TypeError: cannot concatenate 'str' and 'instance' objects
| >
| If I change the N+S+E+W to the 'nsew' form, it works no problem...
| >
| Weird - at other places in the program the form: sticky = N+S+E+W works
| without a problem.
| I found the 'nsew' form by trial and error...
| >
| self.disp is a window different from the current one - it is used to
| display a record entry as it is built up field by field. - the code
| fragment above is what inserts the description of the entry and the
| entered data in a row in the window used for displaying, when the user
| hits the enter key.
| >
| Is this a bug, or am I still doing something wrong?
|
| You have probably defined your own, say, E somewhere in your module:
|
| E = ... # whatever
|
| This can easily be fixed by using another name. But what you are really
| doing wrong is using the
|
| from Tkinter import *
|
| star-import which drastically increases the likelihood of such name clashes.
| I recommend using qualified names, abbreviated if you like:
|
| import Tkinter as tk
|
| ... tk.N + tk.S + tk.E + tk.W ... # a bit longer, but worth the effort
|
| Of course this could still go wrong if you do
|
| tk = 42
|
| somewhere in your module...
|
| Peter

Well spotted that man! the offending line was:

def entryend(self,S):
"""This gets done on carriage return"""

The 'S" is not actually used by me - it was added because the callback passes
Something back and I got an error earlier while I was still using pack...
*grin*

This also explains why it only happens here and not elsewhere...

Problem sorted - False alarm!

- Thanks

Aug 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Richard Lewis | last post: by
2 posts views Thread by skanemupp | last post: by
1 post views Thread by viv1tyagi | last post: by
3 posts views Thread by J-Burns | last post: by
reply views Thread by Guilherme Polo | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.