使用条件语句链接按钮按下

时间:2018-07-06 05:16:21

标签: python-3.x tkinter

这个想法是,如果按下一个按钮之一,则会在左上方的输入框中输入带标签的文本。我最初的计划是在按下按钮时使用if语句,然后根据插入文本。

但是,我不确定使用什么语法可以使我做出这一行条件语句来识别被按下的按钮。这实际上可行吗?还是我需要做一个单独的功能?

from tkinter import *
from tkinter import ttk

class GUI():

    def __init__(self, master):

        self.master = master
        master.resizable(True, True)
        master.title('Conversion Calculator')

        self.tabControl = ttk.Notebook(master)
        self.tab1 = ttk.Frame(self.tabControl)  # tab set up
        self.tabControl.add(self.tab1, text='Builder')
        self.tabControl.pack(expand=1, fill="both")        


        self.builder_entrybox = ttk.Entry(self.tab1, width=24) # entry box set up
        self.builder_entrybox.grid(column=0, row=0)

        self.builder_outputbox = ttk.Entry(self.tab1, width=24)    # output box set up
        self.builder_outputbox.grid(column=0, row=1)
        self.builder_outputbox.config(state='NORMAL')
        self.builder_outputbox.config(state='readonly')

        self.CH3_Button = ttk.Button(self.tab1, text='CH3', command=self.builder) # CH3 button
        self.CH3_Button.grid(column=1, row=0)

        self.CH2_Button = ttk.Button(self.tab1, text='CH2', command=self.builder) # CH2 button
        self.CH2_Button.grid(column=2, row=0)

        self.OH_Button = ttk.Button(self.tab1, text='OH', command=self.builder) # OH button
        self.OH_Button.grid(column=1, row=1)

        self.O_Button = ttk.Button(self.tab1, text='O', command=self.builder) # O button
        self.O_Button.grid(column=2, row=1)

        self.H_Button = ttk.Button(self.tab1, text='H', command=self.builder) # H button
        self.H_Button.grid(column=3, row=1)

        self.COOH_Button = ttk.Button(self.tab1, text='COOH', command=self.builder) # COOH button
        self.COOH_Button.grid(column=3, row=0)

class Logic(GUI):

    def builder (self): # adding button text to entry box (tab1)

            self.builder_entrybox.insert(0, 'CH3')

if __name__ == "__main__":
    root = Tk()
    test = Logic(root)

1 个答案:

答案 0 :(得分:1)

是的,您将为每个按钮创建一个新功能:

class GUI():
    def __init__(self, master):
        # ...

        self.CH3_Button = ttk.Button(self.tab1, text='CH3', command=self.CH3_builder) # CH3 button
        self.CH3_Button.grid(column=1, row=0)

    def CH3_builder(self):
        self.builder_entrybox.insert('end', 'CH3')

Python可以使用functools.partial(早期绑定)或lambda(后期绑定)动态生成函数。使用它,您可以编写如下内容:

from functools import partial

class GUI():
    def __init__(self, master):
        # ...

        self.CH3_Button = ttk.Button(self.tab1, text='CH3', command=partial(self.builder_entrybox.insert, 'end', 'CH3')) # CH3 button
        self.CH3_Button.grid(column=1, row=0)

但是最好是创建一个小的子类来为您处理所有这些事情,这将使您的代码具有很高的可重用性,因此很整洁:

from tkinter import ttk
import tkinter as tk

class Copa(ttk.Button):
    """A new type of Button that moves the text into a Entry when clicked"""
    def __init__(self, master=None, **kwargs):
        ttk.Button.__init__(self, master, command=self.builder, **kwargs)
    def builder(self):
        self.master.builder_entrybox.insert('end', self['text'])

class BuilderFrame(tk.Frame):
    def __init__(self, master=None, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)

        self.builder_entrybox = ttk.Entry(self, width=24) # entry box set up
        self.builder_entrybox.grid(column=0, row=0)

        self.builder_outputbox = ttk.Entry(self, width=24)    # output box set up
        self.builder_outputbox.grid(column=0, row=1)
        self.builder_outputbox.config(state='NORMAL')
        self.builder_outputbox.config(state='readonly')

        self.CH3_Button = Copa(self, text='CH3') # CH3 button
        self.CH3_Button.grid(column=1, row=0)

        self.CH2_Button = Copa(self, text='CH2') # CH2 button
        self.CH2_Button.grid(column=2, row=0)

        self.OH_Button = Copa(self, text='OH') # OH button
        self.OH_Button.grid(column=1, row=1)

        self.O_Button = Copa(self, text='O') # O button
        self.O_Button.grid(column=2, row=1)

        self.H_Button = Copa(self, text='H') # H button
        self.H_Button.grid(column=3, row=1)

        self.COOH_Button = Copa(self, text='COOH') # COOH button
        self.COOH_Button.grid(column=3, row=0)

class GUI():
    def __init__(self, master):

        self.master = master
        master.resizable(True, True)
        master.title('Conversion Calculator')

        self.tabControl = ttk.Notebook(master)
        self.tab1 = BuilderFrame(self.tabControl)  # tab set up
        self.tabControl.add(self.tab1, text='Builder')
        self.tabControl.pack(expand=1, fill="both")

if __name__ == "__main__":
    root = tk.Tk()
    test = GUI(root)
    root.mainloop()