473,725 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Amoun t_des = Label(self.disp , text = self.dis_string , fg =
'black', bg = 'yellow')
self.disp.Amoun t_des.grid(row = self.rownum, column=0, sticky = 'nesw')

self.disp.Amoun t = Label(self.disp , text = self.retstring, fg = 'black',
bg = 'green')
self.disp.Amoun t.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\li b\lib-tk\Tkinter.py", line 1345, in __call__
return self.func(*args )
File "C:\WINDOWS\DES KTOP\Entry1.py" , line 243, in entryend
self.disp.Amoun t.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 2005
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.Amoun t_des = Label(self.disp , text = self.dis_string , fg
=
'black', bg = 'yellow')
self.disp.Amoun t_des.grid(row = self.rownum, column=0, sticky =
'nesw')

self.disp.Amoun t = Label(self.disp , text = self.retstring, fg =
'black',
bg = 'green')
self.disp.Amoun t.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\li b\lib-tk\Tkinter.py", line 1345, in __call__
return self.func(*args )
File "C:\WINDOWS\DES KTOP\Entry1.py" , line 243, in entryend
self.disp.Amoun t.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.Amoun t_des = Label(self.disp , text = self.dis_string ,
fg =
'black', bg = 'yellow')
self.disp.Amoun t_des.grid(row = self.rownum, column=0, sticky =
'nesw')

self.disp.Amoun t = Label(self.disp , text = self.retstring, fg =
'black',
bg = 'green')
self.disp.Amoun t.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\li b\lib-tk\Tkinter.py", line 1345, in __call__
return self.func(*args )
File "C:\WINDOWS\DES KTOP\Entry1.py" , line 243, in entryend
self.disp.Amoun t.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.Amoun t.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.Amoun t = tk.Label(self.d isp, text = self.retstring, fg =
'black', bg = 'green')
self.disp.Amoun t.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.zmz 5(17l8(%,5.Z*(9 3-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.Amoun t_des = Label(self.disp , text = self.dis_string , fg
| =
| 'black', bg = 'yellow')
| self.disp.Amoun t_des.grid(row = self.rownum, column=0, sticky =
| 'nesw')
| >
| self.disp.Amoun t = Label(self.disp , text = self.retstring, fg =
| 'black',
| bg = 'green')
| self.disp.Amoun t.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\li b\lib-tk\Tkinter.py", line 1345, in __call__
| return self.func(*args )
| File "C:\WINDOWS\DES KTOP\Entry1.py" , line 243, in entryend
| self.disp.Amoun t.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
5981
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 filling the remainder of the window. Mind you, this is a contrived test application to help me understand Tkinter and Python, not an actual application yet. I've trivially subclassed Tkinter.Canvas into ColorCanvas, added a bunch of ColorCanvases...
6
8252
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 and bottom), is of a fixed width and is anchored to the left hand side. Here's my code (its derived from one of the examples from the ESRF web site):
3
2356
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 different pages. All that's working fine... The problem I'm having is that the images don't show onscreen the first time the "first page" of results shows up. When I click the "search again" button, and have more than the original results page to...
4
3048
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 widget. If a checkbox was set then the image should be shrunk as necessary to fit the canvas while if cleared it should appear full size with scrollbars if necessary. The code worked fine under Linux (where it was developed). But under Windows,...
2
3528
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 have the same distance and size between them as the other 3 columns? and how can i make the top entry end where the 2nd row entry ends(meaning the top entry will be longer)? why are the 4th row split from the others? hard to fix the problems...
1
1832
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 window is in "AddKlas.py" file. The problem is all the content of the new window is overwritten on the previous window. Below is the code of the two files
3
3918
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 this I mean that the format would be something like this: You have Page1 : This has 2-3 buttons on it. Clicking on each button opens up a new window respectively having
0
1508
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 demonstrates the problem. Anyway, this demonstrates what you are getting (independent of python version): import Tkinter
1
1696
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 widgets both horizontally and vertically. And I'm not sure that you can realize that window look with Tkinter. You could get close by horizontally packing each widget row in a frame and then vertically packing the frames in the window. But the look...
0
9390
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9161
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8065
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5997
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4772
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3211
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2617
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2150
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.