2014-09-29 108 views
0

我是Pyhton GUI的新手,一段时间以来一直在玩下面的代码。下面的拉丁语到英语翻译器的代码通过显示三个按钮来实现,每个按钮上都有一个拉丁文字。按下时,GUI的标签中会出现英文等同物。我想让输出显示在“英文翻译”的右边,但如果按下另一个按键,则会被另一个输出替换。取而代之的是,在按下几个按钮后,它会一遍又一遍地显示翻译,从而导致框区域变得越来越大。有没有办法只是交换输出而不是以前的输出?提前感谢您,我很感谢任何帮助将我转向解决方案。Python 3拉丁语译成英语简单的gui按钮翻译器

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

回答

2

而不是创建和打包为每个按钮按下一个新的标签,在__init__创建单个标签,并改变文本(参见例如Changing the text on a label)按钮被按下时。另外请注意,你的convert函数是微不足道的,几乎相同,因此可以完全使用functools.partial来分解。单按钮的例子,让你开始:

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

partial命令等同于

 ..., command=sin_command) 

... 

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

请注意,我已经采取了更多的面向对象的方法,使得GUI的Tk一个子类(请参阅例如Inheriting from Frame or not in a Tkinter application),并已按照the style guide删除了不必要的反斜杠。

+0

您提供的“Python 3 tkinter更改标签文本”链接对我理解该概念非常有帮助。感谢您花时间指出我的方向。 – WillyJoe 2014-09-29 16:48:43