tkinter:更改循环中创建的按钮的按钮属性

时间:2015-04-11 20:11:00

标签: button python-3.x tkinter

现在这可行,但我更愿意使用for循环和列表。

我已经尝试了,但我无法弄清楚如何获取在for循环中创建的按钮的名称。

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()

我尝试过的事情:

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()

任何指针都会受到赞赏。

2 个答案:

答案 0 :(得分:3)

不要尝试创建变量名称,只需将按钮存储在列表中:

buttons = []
for step in STEPS:
    buttons.append(tk.Button(...))
    ...

...
buttons[i].configure(...)

答案 1 :(得分:1)

正如旁注,你已经死了布莱恩,谢谢你。 这是工作代码:

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()