tkinter:更改在循环中创建的按钮的按钮属性 [英] tkinter: changing button attributes for buttons created in loop

查看:62
本文介绍了tkinter:更改在循环中创建的按钮的按钮属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在这可以工作,但是我更喜欢使用for循环和列表.

Right now this would work, but I would prefer to do it with for loops and a list.

我已经尝试过,但是我不知道如何获取在for循环中创建的按钮的名称.

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

我尝试过的事情:

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

任何指针将不胜感激.

推荐答案

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

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

这篇关于tkinter:更改在循环中创建的按钮的按钮属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆