问题
Right now this would work, but I would prefer to do it with for loops and a list.
I've tried, but I cannot figure out how to get the name of the buttons that were created in the for loop.
import tkinter as tk
def toggle(button):
#----------------------------
# Manages the button toggling
#----------------------------
if button == 0:
if button0.config('relief')[-1] == 'sunken':
button0.config(relief='raised')
else:
button0.config(relief='sunken')
if button == 1:
if button1.config('relief')[-1] == 'sunken':
button1.config(relief='raised')
else:
button1.config(relief='sunken')
if button == 2:
if button2.config('relief')[-1] == 'sunken':
button2.config(relief='raised')
else:
button2.config(relief='sunken')
if button == 3:
if button3.config('relief')[-1] == 'sunken':
button3.config(relief='raised')
else:
button3.config(relief='sunken')
if button == 4:
if button4.config('relief')[-1] == 'sunken':
button4.config(relief='raised')
else:
button4.config(relief='sunken')
if button == 5:
if button5.config('relief')[-1] == 'sunken':
button5.config(relief='raised')
else:
button5.config(relief='sunken')
if button == 6:
if button6.config('relief')[-1] == 'sunken':
button6.config(relief='raised')
else:
button6.config(relief='sunken')
if button == 7:
if button7.config('relief')[-1] == 'sunken':
button7.config(relief='raised')
else:
button7.config(relief='sunken')
if button == 8:
if button8.config('relief')[-1] == 'sunken':
button8.config(relief='raised')
else:
button8.config(relief='sunken')
if button == 9:
if button9.config('relief')[-1] == 'sunken':
button9.config(relief='raised')
else:
button9.config(relief='sunken')
if button == 10:
if button10.config('relief')[-1] == 'sunken':
button10.config(relief='raised')
else:
button10.config(relief='sunken')
if button == 11:
if button11.config('relief')[-1] == 'sunken':
button11.config(relief='raised')
else:
button11.config(relief='sunken')
if button == 12:
if button12.config('relief')[-1] == 'sunken':
button12.config(relief='raised')
else:
button12.config(relief='sunken')
if button == 13:
if button13.config('relief')[-1] == 'sunken':
button13.config(relief='raised')
else:
button13.config(relief='sunken')
if button == 14:
if button14.config('relief')[-1] == 'sunken':
button14.config(relief='raised')
else:
button14.config(relief='sunken')
root = tk.Tk()
root.title("Chamber Calibration")
#------------------------------------------
# Sets up the different temperature buttons
#------------------------------------------
button0 = tk.Button(root, text="-65C", width = 25, relief='raised', command=lambda: toggle(0))
button0.grid(row = 0, column = 3)
button1 = tk.Button(root, text="-40C", width = 25, relief='raised', command=lambda: toggle(1))
button1.grid(row = 1, column = 3)
button2 = tk.Button(root, text="-20C", width = 25, relief='raised', command=lambda: toggle(2))
button2.grid(row = 2, column = 3)
button3 = tk.Button(root, text="0C", width = 25, relief='raised', command=lambda: toggle(3))
button3.grid(row = 3, column = 3)
button4 = tk.Button(root, text="23C", width = 25, relief='raised', command=lambda: toggle(4))
button4.grid(row = 4, column = 3)
button5 = tk.Button(root, text="30C", width = 25, relief='raised', command=lambda: toggle(5))
button5.grid(row = 5, column = 3)
button6 = tk.Button(root, text="45C", width = 25, relief='raised', command=lambda: toggle(6))
button6.grid(row = 6, column = 3)
button7 = tk.Button(root, text="65C", width = 25, relief='raised', command=lambda: toggle(7))
button7.grid(row = 7, column = 3)
button8 = tk.Button(root, text="90C", width = 25, relief='raised', command=lambda: toggle(8))
button8.grid(row = 8, column = 3)
button9 = tk.Button(root, text="130C", width = 25, relief='raised', command=lambda: toggle(9))
button9.grid(row = 9, column = 3)
button10 = tk.Button(root, text="165C", width = 25, relief='raised', command=lambda: toggle(10))
button10.grid(row = 10, column = 3)
button11 = tk.Button(root, text="38C 15%RH", width = 25, relief='raised', command=lambda: toggle(11))
button11.grid(row = 11, column = 3)
button12 = tk.Button(root, text="38C 95%RH", width = 25, relief='raised', command=lambda: toggle(12))
button12.grid(row = 12, column = 3)
button13 = tk.Button(root, text="50C 95%RH", width = 25, relief='raised', command=lambda: toggle(13))
button13.grid(row = 13, column = 3)
button14 = tk.Button(root, text="85C 95%RH", width = 25, relief='raised', command=lambda: toggle(14))
button14.grid(row = 14, column = 3)
#-----------
# Starts GUI
#-----------
root.mainloop()
What I've tried:
import tkinter as tk
def toggle(button):
if button == 0:
if button0.config('relief')[-1] == 'sunken':
button0.config(relief='raised')
else:
button0.config(relief='sunken')
root = tk.Tk()
root.title("Chamber Calibration")
#------------------------------------------
# Sets up the different temperature buttons
#------------------------------------------
STEPS = ['-65C', '-40C', '-20C', '0C', '23C', '30C', '45C', '65C', '90C', '130C', '165C',
'38C 15%RH', '38C 95%RH', '50C 95%RH', '85C 95%RH']
count = 0
for step in STEPS:
newname = ('button' + str(count))
print(newname)
newname = tk.Button(root, text=step, width = 25, relief='raised',
command=lambda button=count: toggle(button))
newname.grid(row = count, column = 3)
count += 1
#-----------
# Starts GUI
#-----------
root.mainloop()
Any pointers would be appreciated.
回答1:
Don't try to create variable names, just store the buttons in a list:
buttons = []
for step in STEPS:
buttons.append(tk.Button(...))
...
...
buttons[i].configure(...)
回答2:
Just as a side note, you were dead on Bryan, thank you. This is the working code:
import tkinter as tk
def toggle(num):
if buttons[num].config('relief')[-1] == 'sunken':
buttons[num].config(relief='raised')
else:
buttons[num].config(relief='sunken')
root = tk.Tk()
root.title("Chamber Calibration")
#------------------------------------------
# Sets up the different temperature buttons
#------------------------------------------
STEPS = ['-65C', '-40C', '-20C', '0C', '23C', '30C', '45C', '65C', '90C', '130C', '165C',
'38C 15%RH', '38C 95%RH', '50C 95%RH', '85C 95%RH']
buttons = []
count = 0
for step in STEPS:
buttons.append(tk.Button(root, text=step, width = 25, relief='raised',
command=lambda num=count: toggle(num)))
buttons[count].grid(row = count, column = 3)
count += 1
#-----------
# Starts GUI
#-----------
root.mainloop()
来源:https://stackoverflow.com/questions/29582537/tkinter-changing-button-attributes-for-buttons-created-in-loop