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() ########################################### > I really have no idea how to do this, but this is what I wrote. The class is a bit odd. I think you may need to review how you define methods. > ==================================== > > #!/usr/bin/python3 > from tkinter import * > > class form(object): In Python 3 you don't need to explicitly inherit object, its done automatically. > def __init__(self, x, reply, master, z,bu): > self.x=x > self.reply=reply > self.master=master > self.z=z > self.bu=bu This is fine but your later code never seems to create an instance of form so this code never runs. > def change(x, reply, z, b): Note that this has no self parameter so is not really a method (or more specifically if you call it as a method x will be treated as self...) > #f contains the alternative command (as a string) > f=["lambda x=vars, txt=reply,fu=0 bu=b1 :form.change(x, txt, > fu, bu)", "lambda x=vars, txt=first, fu=1 bu=b1: form.change(x, txt, fu, > bu)"] Why store the commands as strings? Just store the actual command objects. Functions(including lambdas) are objects in Python just like any other object. > for i in range(4): > x[i].set(reply[i]) Where does the 4 come from? Its not at all clear what x is supposed to be? And you never call it. > #attempt to change command clause > set.button(f[z]) And where is z defined? And set.button should refer to the button attribute of set. But set has no such attribute. I don;t know what you are trying to do here? > def draw(master): Again this is not a method of form, it has no self. Its just a function inside a class. > vars = [] > label = [] > button = StringVar You just set button to be the StringVar class. I suspect you wanted an instance so should have parens? > for i in range(4): > var = StringVar() > vars.append(var) > label.append("") > label[i] = Label( master, textvariable=var, relief=RAISED > ).grid(row=i, column=0) grid always returns None so you are just adding Nones to your list. > vars[i].set(first[i]) Since you are still inside the loop you could just use var directly: var.set(first[i]) > b1=Button(master, text="change", command=button).grid(row=i+1, > column=0) You have set command to be the StringVar class. When you execute the button it will create an instance of StringVar which will then be deleted. > reply=["now I don't know", "Happy birthday", "Many happy returns", "Next > it's my turn",1] > first=["What's your name?", "My name is Fred", "I have just had my > birthday", "your's is next!",2] > master=Tk() > > form.draw(master) > master.mainloop() > > ================================= > > How should I do this, Hopefully my code will give you a clue. I'm not totally clear what the code above is supposed to be doing since it never changes the GUI, rather the draw function creates some buttons and the rest of your code is never used. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor