0
from Tkinter import *
class App(Tk):
def __init__(self):
Tk.__init__(self)
self.title("Periodic Table of the Elements")
self.topLabel = Label(self, text = "Click the element you would like information about.", font=20)
self.topLabel.grid(row=0,column=0,columnspan=18)
column1 = [
'H',
'Li',
'Na',
'K',
'Rb',
'Cs',
'Fr']
# create all buttons with a loop
r = 1
c = 0
for b in column1:
tk.Button(self,text=b,width=5,height=2, bg="grey",command=info).grid(row=r,column=c)
r += 1
if r > 7:
r = 1
c += 1
column2 = [
'Be',
'Mg',
'Ca',
'Sr',
'Ba',
'Ra']
r = 2
c = 1
for b in column2:
tk.Button(self,text=b,width=5,height=2, bg="green",command=info).grid(row=r,column=c)
r += 1
if r > 7:
r = 1
c += 1
column3 = [
'Sc',
'Y',
'La >|',
'Ac >|']
r = 4
c = 2
for b in column3:
tk.Button(self,text=b,width=5,height=2, bg="yellow",command=info).grid(row=r,column=c)
r += 1
if r > 7:
r = 1
c += 1
self.mainloop()
def info(self):
******PROBLEM IS HERE*******
def main():
a = App()
if __name__ == "__main__":
main()
所以,基本上,取决于按下哪个按钮,我想要将顶部的标签更改为按下的按钮元素的名称。我可以使用if-elif的按钮吗?或者我还能做什么?Python:根据按下哪个按钮更改标签
示例:如果按下'H'按钮,将标签更改为'Hydrogen'。如果按“礼”按钮,在顶部更改标签说“锂” ......等等......
我希望它使用if语句或东西不过,因为我希望它改为“氢”,而不仅仅是“H” ...有什么办法可以做到这一点? – Slabach
在'column1'中使用像'('H','Hydrogen')''元组'',并且可以使用'b [0]'按钮文本和'b [1]'''lambda'函数获得顶部标签文本。看我的答案中的例子。 – furas
你可以为元组添加更多信息 - 例如按钮颜色,行,列等('('Hi','Hydrogen','gray',1,0)') - 并将它用于一个'for'为所有按钮循环 – furas