473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 1982
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.