[Tutor] some tkinter questions

Hans Nowak hnowak@cuci.nl
Sat, 18 Dec 1999 00:08:25 +0100


On 17 Dec 99, at 15:12, Suzanne Little wrote:

> Hello, 
> 
> I've been learning Python for a little over 3 weeks now and I've run into
> a couple of snags. 
> 
> 1. I've created a window that pops up using toplevel and I want to click
> 'cancel' in that window and close only that window. Specifically the code
> is w1 = Toplevel()
>    w1.window = createWin(w1)
>    w1.window.pack()

I've stumbled upon this problem last week; I used a slightly 
different approach, but I think you can use w1.destroy() or maybe 
w1.window.destroy(). w1.quit() will quit the whole application, as 
you will probably have found out.

> 2. Communication between modules/classes: I've got a main gui window
> running with a listbox. In anther module I want to add/delete/access
> an item from the listbox and I'm having some problems. I've got two
> functions written in the main module which will get the selection and
> update the list both of which work successfully but I can't call them from
> the other module. I suspect that this may be because both the modules
> import each other. That is main imports the sub modules and the sub
> modules import main, therefore when I start the program by running main it
> tries to use one of the submodules and gets to the point in it where it
> tries to access a function in main and fails. What do you think?

Normally it should be perfectly possible for two modules to import 
each other. 

----m1.py----
# m1.py

def foo(x): return x

import m2

m2.bar(1)
----end of m1.py

----m2.py----
# m2.py

def bar(x): return x-1

import m1

m1.foo(2)
----end of m2.py

This code works; I can start m1.py and m2.py. It doesn't seem to work 
if you move the import statements to the top, before the function 
definitions.  ...Often, mutual importing is a sign of bad design. I'm 
not saying this is the case here (and I've had several projects 
myself where I was obliged to use such constructions), but if moving 
the import statements (like shown above) doesn't work, maybe you 
could consider and try redesigning the code.

> 3. Is there any easy way of getting the representation of an instance? For
> example if I create a = myClass() and add a to a kjset, can I then ask
> what is at some point in the set and get back a as opposed to <myClass at
> instance 1209ef> or something? 

I'm assuming a kjset behaves somewhat like a list? If you define the 
__repr__ method in myClass, you can define what will be printed:

(Example IDLE session)

>>> class MyClass:
	def __repr__(self):
		return "I am a happy class at " + str(id(self))
		
		
>>> a = MyClass()
>>> a
I am a happy class at 8430352
>>> 

> 4. I've also had some problems attaching a scroll to the listbox. I can
> get one showing but it will only move one position down. Any suggestions?

Did you try the example mentioned at 

http://www.pythonware.com/library/tkinter/introduction/x5075-
patterns.htm

...? It works for me...

> 5. Can I implement drop down menus in tkinter?

I don't know if it is included with "standard" Tkinter... I know that 
Pmw has them though.

Hope this helps,

--Hans Nowak (zephyrfalcon@hvision.nl)
Homepage: http://fly.to/zephyrfalcon
You call me a masterless man. You are wrong. I am my own master.