On 01/07/18 02:17, Alan Gauld via Tutor wrote:
On 30/06/18 03:55, Chris Roy-Smith wrote:
I am trying to change the command of a tkinter Button in my program.
Eventually I want to be able to do this to many buttons.
Since I'm not 100% sure if you mean the command or the label or both
here is a simple example that does both...
############################################
import tkinter as tk
def cmd1(): print('This is command 1')
def cmd2(): print('This is number 2')
def swapCmd():
c = b1['command']
# kluge to get the function name from Tcl id
if str(c).endswith("cmd1"):
b1['command'] = cmd2
else:
b1['command'] = cmd1
def swapText():
t = b1['text']
if t == "Cool":
b1['text'] = "Hot"
else:
b1['text'] = "Cool"
# make GUI
top = tk.Tk()
win = tk.Frame(top)
win.pack()
b1 = tk.Button(win,text="Cool", command=cmd1)
b1.pack()
b2 = tk.Button(win, text="Swap text", command=swapText)
b2.pack()
b3 = tk.Button(win, text="Swap cmd", command=swapCmd)
b3.pack()
top.mainloop()
###########################################
Thank you Alan, you have covered what I think I wanted to achieve. For
me, programming is a continual learning experience, unfortunately I seem
to forget nearly as much as I learn, Python is the first language I have
attempted since macro assembler for CP/M. Python seems to be another world.
It appears that I broke the code I started experimenting with, to try
changing the command, and that may have added to my confusion.
I'll play with your example to try an understand what is going on.
Regards, Chris Roy-Smith
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor