0

I'm new to the Pyhton GUI and have been playing around, for some time now, with the code below. The Latin to English translator code below works by displaying three buttons with each having a Latin word on them. When pressed a English equivalent appears in the label of the GUI. I am trying get the output to show up to the right of "English translation" but then be replaced with another output if another button is pressed. Instead after pressing several buttons it displays the translation over and over causing the box area to get larger and larger. Is there a way to just swap the output in place of where the previous output was? Thank you in advance and I appreciate any help in steering me toward a solution.

import tkinter
import tkinter.messagebox

class LatConvGUI:
    def __init__(self):

        self.main_window = tkinter.Tk()
        self.top_frame = tkinter.Frame()
        self.bottom_frame = tkinter.Frame()
        self.prompt_label = tkinter.Label(self.top_frame, \
                    text='English Translation is:')

        self.prompt_label.pack(side='left')

        self.sin_button = tkinter.Button(self.bottom_frame, \
                                     text='sinister', \
                                     command=self.convert)

        self.dex_button = tkinter.Button(self.bottom_frame, \
                                text='dexter', \
                                command=self.convert2)

        self.med_button = tkinter.Button(self.bottom_frame, \
                                text='medium', \
                                command=self.convert3)

        self.label2 = tkinter.Label(self.bottom_frame, \
                                    text='Latin word is:')

        self.label2.pack(side='left')

        self.sin_button.pack(side='left')
        self.dex_button.pack(side='left')
        self.med_button.pack(side='left')
        self.top_frame.pack()
        self.bottom_frame.pack()

        tkinter.mainloop()

    def convert(self):

        self.label1 = tkinter.Label(self.top_frame, \
                                    text='left')
        self.label1.pack(side = 'top')

    def convert2(self):

        self.label3 = tkinter.Label(self.top_frame, \
                                    text='right')
        self.label3.pack(side = 'top')

    def convert3(self):

        self.label4 = tkinter.Label(self.top_frame, \
                                    text='center')
        self.label4.pack(side = 'top')


eng_conv = LatConvGUI()
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
WillyJoe
  • 119
  • 1
  • 3
  • 9

1 Answers1

2

Rather than create and pack a new label for each button press, create a single label in __init__ and change the text (see e.g. Changing the text on a label) when the button is pressed. Also, note that your convert functions are trivial and almost identical, so can be factored out entirely by using functools.partial. A single-button example to get you started:

from functools import partial
import tkinter
import tkinter.messagebox

class LatConvGUI(tkinter.Tk):

    def __init__(self):
        super().__init__()

        self.top_frame = tkinter.Frame(self)
        self.bottom_frame = tkinter.Frame(self)

        self.prompt_label = tkinter.Label(self.top_frame,
                                          text='English Translation is:')
        self.prompt_label.pack(side='left')

        self.label1 = tkinter.Label(self.top_frame, text='')
        self.label1.pack(side='top')

        self.label2 = tkinter.Label(self.bottom_frame,
                                    text='Latin word is:')
        self.label2.pack(side='left')

        self.sin_button = tkinter.Button(self.bottom_frame,
                                         text='sinister',
                                         command=partial(self.label1.config,
                                                         text='left'))
        self.sin_button.pack(side='left')

        self.top_frame.pack()
        self.bottom_frame.pack()


eng_conv = LatConvGUI()
eng_conv.mainloop()

The partial command is equivalent to

     ..., command=sin_command)

...

def sin_command(self):
    self.label1.config(text='left')

Note that I have taken a more object-oriented approach, making the GUI a subclass of Tk (see e.g. Inheriting from Frame or not in a Tkinter application) and have removed the unnecessary backslashes per the style guide.

Community
  • 1
  • 1
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • The link of "Python 3 tkinter change label text" that you provided was very helpful for me to understand the concept. Thank you for taking the time to point me in that direction. – WillyJoe Sep 29 '14 at 16:48