Tkinter code (with pmw) executing to soon please help

E

Eric_Dexter

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 +' '+ str(instr.comments)
#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
 
G

Gabriel Genellina

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
 
S

Scott David Daniels

Gabriel said:
... 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
(e-mail address removed)
 
E

Eric_Dexter

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.
 
E

Eric_Dexter

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 said:
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
 
P

Peter Otten

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
 
G

Gabriel Genellina

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top