如果有多个条目,如何使用按钮专门输入每个tkinter条目?

时间:2016-05-18 12:15:31

标签: python user-interface tkinter

如果有多个条目,如何使用专门针对每个条目的按钮进行输入(因此,如果单击该按钮,则不会填充所有条目,只会填充您所在的条目)。例如,专门使用按钮输入a,b,d,k,c(如果单击按钮,则输入之前单击的条目中的数字)

class PageDeg2 (tk.Frame):
    def __init__(self, parent, controller):
        def add_num(x):
           a.insert(16,x)
           b.insert(16,x)
           c.insert(16,x)
        tk.Frame.__init__(self, parent)
        mainFunction = tk.Label (self, text = "y = ax^2 + bx + c", font = FONT)
        mainFunction.grid (row = 0, column = 0) ####

        label1 = tk.Label(self, text = "a = ", font = FONT, fg = 'red')
        label1.grid (row = 1, column = 0)

        label2 = tk.Label(self, text = "b = ", font = FONT, fg = 'orange')
        label2.grid (row = 2, column = 0)

        label3 = tk.Label (self, text = "c = ", font = FONT, fg = 'green')
        label3.grid (row = 3, column = 0)

        a = tk.Entry (self)
        a.grid (row = 2, column = 1)

        b = tk.Entry (self)
        b.grid (row = 1, column = 1)

        c = tk.Entry (self)
        c.grid (row = 3, column = 1)

        sLabel = tk.Label (self, text = "from ", font = FONT) 
        sLabel.grid (row = 4, column = 0)
        start = tk.Entry (self)
        start.grid (row = 4, column = 1)
        eLabel = tk.Label (self, text = " to ", font = FONT)
        eLabel.grid (row = 4, column = 2)
        end = tk.Entry (self)
        end.grid (row = 4, column = 3)
        buttonG  = tk.Button (self, text = "Graph") #function o Graph def graph
        buttonG.grid (row = 5, column = 2)        
        buttonBack = tk.Button (self, text = "Back",command = lambda: controller.show_frame (PagePolynomial)) 
        buttonBack.grid (row = 6, column = 2)

        Button(self,text='7',width=10,height=5,bg="WHITE",command=lambda:add_num('7')).grid(row=9,column=0,sticky=E+W+N+S)
        Button(self,text='8',width=10,height=5,bg="WHITE",command=lambda:add_num('8')).grid(row=9,column=1,sticky=E+W+N+S)
        Button(self,text='9',width=10,height=5,bg="WHITE",command=lambda:add_num('9')).grid(row=9,column=2,sticky=E+W+N+S)


        Button(self,text='4',width=10,height=5,bg="WHITE",command=lambda:add_num('4')).grid(row=10,column=0,sticky=E+W+N+S)
        Button(self,text='5',width=10,height=5,bg="WHITE",command=lambda:add_num('5')).grid(row=10,column=1,sticky=E+W+N+S)
        Button(self,text='6',width=10,height=5,bg="WHITE",command=lambda:add_num('6')).grid(row=10,column=2,sticky=E+W+N+S)


        Button(self,text='1',width=10,height=5,bg="WHITE",command=lambda:add_num('1')).grid(row=11,column=0,sticky=E+W+N+S)
        Button(self,text='2',width=10,height=5,bg="WHITE",command=lambda:add_num('2')).grid(row=11,column=1,sticky=E+W+N+S)
        Button(self,text='3',width=10,height=5,bg="WHITE",command=lambda:add_num('3')).grid(row=11,column=2,sticky=E+W+N+S)



        Button(self,text='0',width=10,height=5,bg="WHITE",command=lambda:add_num('0')).grid(row=12,column=0,sticky=E+W+N+S)
        Button(self,text='C',width=10,height=5,bg="BLUE",command=lambda:e1.delete(0,END)).grid(row=12,column=1,sticky=E+W+N+S)
        Button(self,text='.',width=10,height=5,bg="WHITE",command=lambda:add_num('.')).grid(row=12,column=2,sticky=E+W+N+S)

1 个答案:

答案 0 :(得分:0)

通常,当您以编程方式插入要插入到具有键盘焦点的任何窗口小部件中的文本时,通常您希望在插入光标处执行此操作。您可以使用tkinter方法focus_get获得键盘焦点,并且可以将插入光标引用为"insert"

例如:

def add_num(x):
    widget = self.focus_get()
    if widget and hasattr(widget, "insert"):
        widget.insert("insert", x)

注意:如果您始终希望在条目后附加数字,请使用"end"而不是"insert"