【问题标题】:Python 3 Latin to English simple gui button translatorPython 3 拉丁语到英语简单的 gui 按钮翻译器
【发布时间】:2014-11-24 02:48:47
【问题描述】:

我是 Pyhton GUI 的新手,并且已经使用下面的代码玩了一段时间了。下面的拉丁语到英语翻译器代码通过显示三个按钮来工作,每个按钮上都有一个拉丁词。按下时,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()

【问题讨论】:

    标签: python user-interface python-3.x tkinter


    【解决方案1】:

    与其为每次按下按钮创建和打包一个新标签,不如在__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 中不必要的反斜杠。

    【讨论】:

    • 您提供的“Python 3 tkinter change label text”链接对我理解这个概念很有帮助。感谢您花时间为我指明方向。
    猜你喜欢
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    相关资源
    最近更新 更多