473,385 Members | 1,402 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,385 software developers and data experts.

Tkinter code (with pmw) executing to soon please help

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
6 1856
<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
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
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
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
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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: wang xiaoyu | last post by:
Hello,everyone. my program runs well in windows,i use tkSimpleDialog to receive some input,but when i copy my program into Linux RH8.0,entrys in my tkSimpleDialog derived Dialog have a vital...
0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
2
by: Stewart Midwinter | last post by:
I would like to link the contents of three OptionMenu lists. When I select an item from the first list (call it continents), the contents of the 2nd list (call it countries) would update. And in...
0
by: Stewart Midwinter | last post by:
I've got a Tkinter app that draws three histograms. At this point I am simulating real data by drawing rectangles whose size is defined by random numbers; in the future there would be real data...
1
by: midtoad | last post by:
I'm trying to display a GIF image in a label as the central area to a Tkinter GUI. The image does not appear, though a space is made for it. Why is this so? I notice that I can display a GIF...
7
by: SeeBelow | last post by:
Do many people think that wxPython should replace Tkinter? Is this likely to happen? I ask because I have just started learning Tkinter, and I wonder if I should abandon it in favor of...
1
by: Laura Conrad | last post by:
I'm writing a GUI application in python, which will need to work on a Windows XP box. Mostly it does, but the feature where results are graphed in real time, which works fine on a debian LINUX...
4
by: Alex Hunsley | last post by:
Can anyone recommend some code for creating drop-down menus in tkinter? To be absolutely clear, here's an example of a drop-down: http://www.google.co.uk/preferences?hl=en (see the language...
0
by: Stewart Midwinter | last post by:
I have a Tkinter app running on cygwin. It includes a Test menu item that does nothing more than fetch a directory listing and display it in a Toplevel window (I'd use a tkMessageBox showinfo...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.