Say I have a list of strings: obj = ['One','Two','Three']
, how would I be able to turn each value in this list into a function where they all carry out very similar functions? For example:
def one():
print("one")
def two():
print("two")
def three():
print("three")
Now I know you can define the functions beforehand and use a dictionary (as shown below), but say I wanted many functions to be created, it would take a lot of code to do so and therefore I would like to find out if there is a shorter way I can go about this.
import tkinter as tk
def one():
print("one")
def two():
print("two")
def three():
print("three")
obj = ['One','Two','Three']
func = {'One': one, 'Two': two, 'Three': three}
def create_btn():
btns = {}
for i in obj:
text = i
for col in range(1):
for row in range(len(obj)):
btns[row, col] = tk.Button(canvas, text=str(text),
command=func[i]).grid(column=col,
row=row)
btns[row, col] = canvas.create_window(50, row,
window = btns[row, col])
canvas.pack()
root = tk.Tk()
root.geometry = ("750x600")
btn_frame = tk.Frame(root)
canvas = tk.Canvas(root)
create_btn()
root.mainloop()