**我正在尝试制作子手游戏,但我觉得这样可以更容易编写任何建议?
buttonb = tkinter.Button(bg="blue", text = "B", width=2,command=ex)
buttonb.pack(side="left")
buttonc = tkinter.Button(bg="blue", text = "C",width=2, command=ex)
buttonc.pack(side="left")
buttond = tkinter.Button(bg="blue", text = "D", width=2,command=ex)
buttond.pack(side="left")
答案 0 :(得分:0)
正如@meowgoesthedog所建议的那样,简单的循环就是您的答案。
import string
buttons = {}
for letter in string.ascii_uppercase:
buttonb = tkinter.Button(bg = "blue", text = letter, width = 2,command = ex)
buttonb.pack(side = "left")
buttons[letter] = buttonb
答案 1 :(得分:0)
您可以使用循环遍历字母,并且由于按钮属于同一类,因此它们可以存储在列表等数据结构中。
buttons = []
for letter in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
button = tkinter.Button(bg="blue", text = letter, width=2,command=ex)
button.pack(side="left")
buttons.append(button)
如果愿意,可以随意使用列表理解。