JavaScript
x
20
20
1
import Tkinter
2
from Tkinter import ttk
3
4
root = tk()
5
root.mainloop()
6
label = ttk.Label(root, text = "This text I want to change")
7
label.grid()
8
9
def select_label_text():
10
top = Toplevel()
11
top.title("Select Name")
12
13
def change_name(name):
14
label.cofigure(text=name)
15
top.destroy()
16
17
label_names = [a,b,c ] #this has undefined strings
18
for i in range(len(label_names)): #here I made a button for each different name
19
ttk.button(top, text = label_names[i], command = lambda: change_name(label_names[i])).grid()
20
I want to change the label text to the button´s name when I press it. there are undefined buttons so I can’t save each one to a variable.
JavaScript
1
2
1
ttk.Button(root, comand = select_label_text()).grid()
2
Advertisement
Answer
You have many errors. Here is a version of your code, for Python 3, not Python 2, that at least runs. It may not do exactly what you want it to do, but it works, and it’s a better starting point than what you posted. The modified code is as follows:
JavaScript
1
22
22
1
import tkinter # Tkinter
2
from tkinter import ttk
3
4
def select_label_text():
5
top = tkinter.Toplevel()
6
top.title("Select Name")
7
8
def change_name(name):
9
label.configure(text=name)
10
top.destroy()
11
12
13
label_names = ["a", "b", "c..."] #this has undefined strings
14
for i in range(len(label_names)): #here I made a button for each different name
15
ttk.Button(top, text=label_names[i], command=lambda i=i: change_name(label_names[i])).grid()
16
17
root = tkinter.Tk()
18
label = ttk.Label(root, text = "This text I want to change")
19
label.grid()
20
ttk.Button(root, command=select_label_text).grid()
21
root.mainloop()
22
You should do a diff comparison between your code and this version to see the changes.