Python3, Tkinter. No button is created in the function. I try to make btn [0] work only. function button_clicked1 is

from tkinter import * root=Tk() root.title('Python Tkinter Test') root.minsize(325,230) root.resizable(width=False, height=False) k = ['1', '2', '3', '4', '5', '6', '7', '8', '9'] list1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9'] st = int(0) def xstroke(k, st): for i in range(1, 10): if int(st) == int(i): if list1.count(str(i)) == 1: k[i-1] = 'X' list1.remove(str(i))#надо обновить кнопку как-то btn[i] = Button(root, bg="#DC143C", text = "\(. | .)/", command = button_clicked1, width=4,height=1) return k, btn def button_clicked1(): print('1') btn = [Button(root, bg="#87CEFA", text = k[0], command = lambda: xstroke(k, int(1)), width=4,height=1), Button(root, bg="#87CEFA", text = k[1], command = button_clicked1, width=4,height=1), Button(root, bg="#87CEFA", text = k[2], command = button_clicked1, width=4,height=1), Button(root, bg="#87CEFA", text = k[3], command = button_clicked1, width=4,height=1), Button(root, bg="#87CEFA", text = k[4], command = button_clicked1, width=4,height=1), Button(root, bg="#87CEFA", text = k[5], command = button_clicked1, width=4,height=1), Button(root, bg="#87CEFA", text = k[6], command = button_clicked1, width=4,height=1), Button(root, bg="#87CEFA", text = k[7], command = button_clicked1, width=4,height=1), Button(root, bg="#87CEFA", text = k[8], command = button_clicked1, width=4,height=1)] for i in range(0,9): btn[i].pack btn[0].place(x=35,y=35) btn[1].place(x=105,y=35) btn[2].place(x=175,y=35) btn[3].place(x=35,y=90) btn[4].place(x=105,y=90) btn[5].place(x=175,y=90) btn[6].place(x=35,y=145) btn[7].place(x=105,y=145) btn[8].place(x=175,y=145) root.mainloop() 

I try to make btn [0] work only. function button_clicked1 is

  • * I try to make btn [0] work only. the button_clicked1 function is - Vlad Solodov

1 answer 1

Just update your functions in the widget via the config

 btn[i-1].config(bg="#DC143C", text = "\(. | .)/", command = button_clicked1, width=4,height=1) 

enter image description here

  • still no button appears - Vlad Solodov
  • @ VladSolodov what do you really need at all? - Twiss
  • so that btn [i] is replaced with Button (root, bg = "# DC143C", text = "(. |.) /", command = button_clicked1, width = 4, height = 1) .pack () - Vlad Malts
  • @ VladSolodov why? just change the text and command? - Twiss
  • @Twins, yes. And the color - Vlad Solodov