2013-11-28 59 views
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'。如果按“礼”按钮,在顶部更改标签说“锂” ......等等......

回答

2

我用两件事情:

  • self.topLabel.config(text="some new text")设置文本
  • lambda功能(command= lambda text=b:self.info(text))将单个文本从按钮发送到info()

from Tkinter import * 
import Tkinter as tk 

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=lambda text=b:self.info(text)).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=lambda text=b:self.info(text)).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=lambda text=b:self.info(text)).grid(row=r,column=c) 
     r += 1 
     if r > 7: 
      r = 1 
      c += 1 

    self.mainloop() 

    def info(self, text): 
    #print text 
    self.topLabel.config(text=text) 

def main(): 
    a = App() 

if __name__ == "__main__": 
    main() 

编辑:

要对按钮和顶部标签上不同的文字不同的文字,你可以这样做:

column1 = [ 
    ('H', 'Hydrogen'), 
    ('Li', 'other'), 
    ('Na', 'other'), 
    ('K', 'other'), 
    ('Rb', 'other'), 
    ('Cs', 'other'), 
    ('Fr', 'other') 
] 

# create all buttons with a loop 
r = 1 
c = 0 
for b in column1: 
    tk.Button(self,text=b[0],width=5,height=2, bg="grey",command=lambda text=b[1]:self.info(text)).grid(row=r,column=c) 

编辑:

我为人人循环栏目:

我用\到代码最后一行分成两行

all_columns = [ 
    ('H' , 'Hydrogen' , 'grey', 1, 0), 
    ('Li', 'Li-something', 'grey', 2, 0), 
    ('Na', 'Na-something', 'grey', 3, 0), 
    ('K' , 'K-something' , 'grey', 4, 0), 
    ('Rb', 'Rb-something', 'grey', 5, 0), 
    ('Cs', 'Cs-something', 'grey', 6, 0), 
    ('Fr', 'Fr-something', 'grey', 7, 0), 

    ('Be', 'Be-something', 'green', 2, 1), 
    ('Mg', 'Mg-something', 'green', 3, 1), 
    ('Ca', 'Ca-something', 'green', 4, 1), 
    ('Sr', 'Sr-something', 'green', 5, 1), 
    ('Ba', 'Ba-something', 'green', 6, 1), 
    ('Ra', 'Ra-something', 'green', 7, 1), 

    ('Sc', 'Sc-something', 'yellow', 4, 2), 
    ('Y' , 'Y-something' , 'yellow', 5, 2), 
    ('La >|', 'La-something', 'yellow', 6, 2), 
    ('Ac >|', 'Ac-something', 'yellow', 7, 2) 
] 

# create all buttons with a loop 
for element in all_columns: 
    button_text, label_text, button_color, button_row, button_col = element 
    tk.Button(self, text=button_text, width=5, height=2, bg=button_color, command=lambda text=label_text:self.info(text)) \ 
     .grid(row=button_row, column=button_col) 
+0

我希望它使用if语句或东西不过,因为我希望它改为“氢”,而不仅仅是“H” ...有什么办法可以做到这一点? – Slabach

+0

在'column1'中使用像'('H','Hydrogen')''元组'',并且可以使用'b [0]'按钮文本和'b [1]'''lambda'函数获得顶部标签文本。看我的答案中的例子。 – furas

+0

你可以为元组添加更多信息 - 例如按钮颜色,行,列等('('Hi','Hydrogen','gray',1,0)') - 并将它用于一个'for'为所有按钮循环 – furas