Question

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()

So, basically, depending on which button is pressed, I want to change the label at the top to the name of the element of the button that was pressed. Can I use if-elif's with buttons? or what else do i do?

Example: If 'H' button is pressed, change label at top to say 'Hydrogen'. If 'Li' button is pressed, change label at top to say 'Lithium'... and so on...

Was it helpful?

Solution

I use two things:

  • self.topLabel.config(text="some new text") to set text
  • lambda function (command= lambda text=b:self.info(text)) to send individual text from buttons to 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()

EDIT:

To have different text on button and different text on top label you can do:

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)

EDIT:

One loop for all columns:

(I use \ to split last line of code into two rows)

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top