Python:根据按下的按钮更改标签

时间:2013-11-28 19:25:20

标签: python tkinter tk

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吗?或者我还能做什么?

示例:如果按下“H”按钮,请将顶部的标签更改为“氢气”。如果按下“Li”按钮,请将顶部的标签更改为“Lithium”......依此类推......

1 个答案:

答案 0 :(得分: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)