472,799 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,799 software developers and data experts.

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 1919
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles...
6
by: Richard Lewis | last post by:
Hi there, I've got a tree control in Tkinter (using the ESRF Tree module) but I can't get it to layout how I want it. I'd like to have it so that it streches north/south (anchored to the top...
3
by: aldonnelley | last post by:
Hi all. Just having a weird problem with tkinter. I'm trying to make a gui that shows results from an image search, with a "forward" and "back" button so the user can compare results from...
4
by: peter | last post by:
I've come across a weird difference between the behaviour of the Tkinter checkbox in Windows and Linux. The issue became apparent in some code I wrote to display an image in a fixed size canvas...
2
by: skanemupp | last post by:
so my little calculator works perfectly now. just having some trouble with the layout. this whole tkinter-thing seems to be more tricky than it should be. how can i make the 4 column of buttons...
1
by: viv1tyagi | last post by:
Hi everyone ! ! ! I'm just a month old in the world of Python and trying to develop an application using Tkinter in which a new window pops out on a particular button press.The code for this new...
3
by: J-Burns | last post by:
Hello. Im a bit new to using Tkinter and im not a real pro in programming itself... :P. Need some help here. Problem 1: How do I make something appear on 2 separate windows using Tkinter? By...
0
by: Guilherme Polo | last post by:
On Wed, Sep 3, 2008 at 8:57 PM, Kevin McKinley <kem1723@yahoo.comwrote: Come on.. "help on lines 384-403", that is not a good way to look for help. You are supposed to post some minimal code that...
1
by: Francesco Bochicchio | last post by:
Il Mon, 18 Aug 2008 12:15:10 +0100, dudeja.rajat ha scritto: Uhm, I don't think you should use the grid manager to obtain a window like that. The grid manager is for equally distributing...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.