By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,478 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

Tkinter code (with pmw) executing to soon please help

P: n/a
Instead of creating my buttons and waiting for me to press them to
execute they are executing when I create them and won't do my callback
when I press them.. thanks for any help in advance

button[num] = Tkinter.Button(frame,text = returnstring,
command=callback(returnstring))#

this line executes on creation

my output on startup is (should output when I choose an option)

1 String pad
10 Chorus
25 Reverb
30 Mixer
The buttons do label correctly.


title = 'csd instrument list'

# Import Pmw from this directory tree.
import sys
sys.path[:0] = ['../../..']

import Tkinter
import Pmw
import csoundroutines

class Demo:
def __init__(self, parent):
frame = Tkinter.Frame(parent)
frame.pack(fill = 'both', expand = 1)

button = {}

instr = [[]]
instr = csoundroutines.csdInstrumentList2(sys.argv[1])

num = 0 #instr_number

buttonBox = Pmw.ButtonBox(parent)
for i in range(0, len(instr.instrnum)):
num += 1
returnstring = instr.instrnum[i] +' '+ str(instr.comments[i])
#printstring = zz + x[num]
button[num] = Tkinter.Button(frame,text = returnstring,
command=callback(returnstring))#kinter.Buttonstr(x[0]) + ' ' + x[1])# +
comments)
button[num].pack()
#returnstring = zz
button[num].grid(column=num, row =
0)#,command=callback(returnstring))
#button[num].pack()

frame.grid_rowconfigure(3, weight=1)
frame.grid_columnconfigure(3, weight=1)
frame.pack()
def callback(text):
print text
################################################## ####################

# Create demo in root window for testing.
if __name__ == '__main__':
global filename
filename = (sys.argv[1])
root = Tkinter.Tk()
Pmw.initialise(root)
root.title(title)

exitButton = Tkinter.Button(root, text = 'Exit', command =
root.destroy)
exitButton.pack(side = 'bottom')
widget = Demo(root)
root.mainloop()

http://www.dexrow.com

Jan 14 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
<Er*********@msn.comescribió en el mensaje
news:11*********************@l53g2000cwa.googlegro ups.com...
Instead of creating my buttons and waiting for me to press them to
execute they are executing when I create them and won't do my callback
when I press them.. thanks for any help in advance
This is a very frequent beginner's mistake. You are *calling* the event
handler at the moment you create the buttons. You have to provide a
*callable* but not call it yet.
button[num] = Tkinter.Button(frame,text = returnstring,
command=callback(returnstring))#
So `callback` should return a function, like this:

def callback(text):
def handler(event):
print text

--
Gabriel Genellina

Jan 14 '07 #2

P: n/a
Gabriel Genellina wrote:
>... So `callback` should return a function, like this:

def callback(text):
def handler(event):
print text
Even better than that:
def callback(text):
def handler(event):
print text
return handler

Otherwise callback returns the spectacularly un-useful value None.

--Scott David Daniels
sc***********@acm.org
Jan 14 '07 #3

P: n/a
C:\dex_tracker\csdlist.py bay-at-night.csd
Traceback (most recent call last):
File "C:\dex_tracker\csdlist.py", line 58, in
root.mainloop()
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1023, in mainloop
self.tk.mainloop(n)
File "../../..\Pmw\Pmw_1_2\lib\PmwBase.py", line 1751, in __call__
File "../../..\Pmw\Pmw_1_2\lib\PmwBase.py", line 1777, in _reporterror
TypeError: unsupported operand type(s) for +: 'type' and 'str'
Script terminated.

It doesn't like the return handler part of it.

Scott David Daniels wrote:
Gabriel Genellina wrote:
... So `callback` should return a function, like this:

def callback(text):
def handler(event):
print text

Even better than that:
def callback(text):
def handler(event):
print text
return handler

Otherwise callback returns the spectacularly un-useful value None.

--Scott David Daniels
sc***********@acm.org
Jan 14 '07 #4

P: n/a
button[num] = Tkinter.Button(frame,text = returnstring,
command=callback(returnstring))#

I understand this part of it

def callback(text):
def handler(event):
print text

It stopped calling it automaticaly but will not do anything when I
click on the button. Does something have to change on this line as
well.

button[num] = Tkinter.Button(frame,text = returnstring,
command=callback(returnstring)


Gabriel Genellina wrote:
<Er*********@msn.comescribió en el mensaje
news:11*********************@l53g2000cwa.googlegro ups.com...
Instead of creating my buttons and waiting for me to press them to
execute they are executing when I create them and won't do my callback
when I press them.. thanks for any help in advance

This is a very frequent beginner's mistake. You are *calling* the event
handler at the moment you create the buttons. You have to provide a
*callable* but not call it yet.
button[num] = Tkinter.Button(frame,text = returnstring,
command=callback(returnstring))#

So `callback` should return a function, like this:

def callback(text):
def handler(event):
print text

--
Gabriel Genellina
Jan 14 '07 #5

P: n/a
Er*********@msn.com wrote:
Scott David Daniels wrote:
>Gabriel Genellina wrote:
>... So `callback` should return a function, like this:

def callback(text):
def handler(event):
print text

Even better than that:
def callback(text):
def handler(event):
print text
return handler

Otherwise callback returns the spectacularly un-useful value None.
C:\dex_tracker\csdlist.py bay-at-night.csd
Traceback (most recent call last):
File "C:\dex_tracker\csdlist.py", line 58, in
root.mainloop()
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1023, in mainloop
self.tk.mainloop(n)
File "../../..\Pmw\Pmw_1_2\lib\PmwBase.py", line 1751, in __call__
File "../../..\Pmw\Pmw_1_2\lib\PmwBase.py", line 1777, in _reporterror
TypeError: unsupported operand type(s) for +: 'type' and 'str'
Script terminated.

It doesn't like the return handler part of it.
Probably because a Tkinter.Button command callback doesn't accept any
arguments. Try

def callback(text):
def handler():
print text
return handler

Note that 'make_callback' would be a better name than 'callback' because the
function 'callback' actually creates the callback (called 'handler').

Peter
Jan 14 '07 #6

P: n/a
<Er*********@msn.comescribió en el mensaje
news:11**********************@a75g2000cwd.googlegr oups.com...
button[num] = Tkinter.Button(frame,text = returnstring,
command=callback(returnstring))#

I understand this part of it

def callback(text):
def handler(event):
print text
It stopped calling it automaticaly but will not do anything when I
click on the button. Does something have to change on this line as
well.
Sorry, I overlooked your example. For a button, command should be a function
with no arguments (and I forget to return the handler, as someone already
pointed out):

def callback(text):
def handler():
print text
return handler

--
Gabriel Genellina

Jan 14 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.