I'm learning to use loops within tkinter to create buttons and write from a two dimensional list to buttons.
My first question is what I would like to happen is the bottom two buttons (PressBtns) are named button 1 and button 2 respectively, not sure how to do this when using loops.
My second query is when these buttons are pressed "Button 1" prints the first part of the two dimensional list onto (ShowBtns) (["10", "20", "40", "80", "90", "100"]) one number to each button and when (Button 2) is pressed it prints the second part of the two dimensional list onto the (Showbtns)(["95", "90", "80", "40", "20", "10"]]) overwriting the first list. Thanks for your help in advance see my code below.
import tkinter as tk
window = tk.Tk()
Lists=[["10", "20", "40", "80", "90", "100"],
["95", "90", "80", "40", "20", "10"]]
def List1():
j= 6
for row in (Lists):
for col in row [0:j]:
j=j-1
PressBtns.config (text = col)
def List2():
j= 6
for row in (Lists):
for col in row [1:j]:
j=j-1
PressBtns.config (text = col)
for i in range (6):
ShowBtns = tk.Button (window, height = 1, width = 5)
ShowBtns.grid (row = i, column = 5)
counter = 6
for i in range (len(Lists)):
PressBtns = tk.Button (window, height = 1, width = 10, command = "")
PressBtns.grid(row = counter, column = 5)
counter = counter +1
window.mainloop()
Lists[i]as a pre-built in argument to the callback you specify as a value. There are two ways I know to do this. Either declare alambdaor a functools partial journaldev.com/33505/python-partial-function-functools-partialLists[i]as I specified in my answer, but with partial you can and it works perfectly. This is a very nice tip. The only difference is that partial requires you to define the function before setting it as the button callbackfunction.