【问题标题】:How to have different text per button, rather than having it all the same如何每个按钮有不同的文本,而不是完全一样
【发布时间】:2013-06-04 15:34:28
【问题描述】:

我想要有 5 个不同的按钮,每个按钮相应地标记为 A、B、C、D 和 E。到目前为止,我有:

from tkinter import *
from tkinter.ttk import *
window = Tk()
for i in range (5):
    button = Button(window, text="A")
    button.grid(row=i//5, column=i%5)
window.mainloop()

但是我不知道如何更改单个按钮的文本。 任何帮助将不胜感激。

【问题讨论】:

    标签: python button python-3.x tkinter


    【解决方案1】:

    您可以使用enumerate 来遍历按钮的字母和索引:

    for i, text in enumerate(['A', 'B', 'C', 'D', 'E']):
        button = Button(window, text=text)
        button.grid(row=i//5, column=i%5)
    

    【讨论】:

      猜你喜欢
      • 2018-12-20
      • 1970-01-01
      • 2019-09-27
      • 2018-03-07
      • 2021-05-03
      • 2019-01-16
      • 2011-02-12
      • 2019-09-18
      • 2023-04-01
      相关资源
      最近更新 更多