введите определенный виджет с помощью цифровой клавиатуры tkinter

Привет, я готовлю графический интерфейс для проекта, но, к сожалению, я не могу сразу ввести конкретный виджет с помощью цифровой клавиатуры. Если я нажму число на цифровой клавиатуре сенсорного экрана, цифра появится в обоих виджетах. Как я могу изменить это, что я могу ввести только цифру в виджете сразу? Я хотел попробовать метод фокуса, но не очень получилось...

Спасибо за вашу помощь.

Вот мой код:

# globally declare the expression variable 
expression = ""


# Function to update expression 
# in the text entry box 
def press(num): 
    # point out the global expression variable 
    global expression
    # concatenation of string 
    expression = expression + str(num)
    # update the expression by using set method
        equation1.set(expression)
        equation2.set(expression)

    
    
# Function to clear the contents 
# of text entry box 
def clear(): 
    global expression 
    expression = ""
    equation1.set("")
    equation2.set("")




#create GUI
# Driver code 
if __name__ == "__main__": 
    # create a GUI window 
    gui = Tk()
    gui.title("GUI")
     

    equation1 = StringVar()
    equation2 = StringVar()
    equation1.set("")
    equation2.set("")
   
    #Label 1
    label1 = tkinter.Label(text ="Pumpenhöhe 1 [cm]")
    label1.grid (row =0 , column =0 , padx = xdis , pady = ydis )

    #Eingabefeld 1 definieren
    eingabe1 = tkinter.Entry(gui, textvariable=equation1, width=4, bg ='#ffffff')
    eingabe1.grid(row=0, column=1, padx=xdis, pady = ydis)
    

    #Label 2
    label2 = tkinter.Label (text ="Pumpenhöhe 2 [cm]") 
    label2.grid(row=1,column =0 , padx = xdis ,pady = ydis)

    #Eingabefeld 2
    eingabe2 = tkinter.Entry(gui, textvariable=equation2, width=4,  bg ='#ffffff')
    eingabe2.grid(row=1, column=1, padx=xdis, pady = ydis)


  

    #button obj on framework to send values
    set_setpoints = tkinter.Button(text ="Send", command = set_setpoints)
    set_setpoints.grid(row=2, column=2, padx= xdis, pady = ydis)
    
    #create exit button
    ex_bt = tkinter.Button(gui, text='Exit', command=gui.destroy)
    ex_bt.grid(row=7, column=2, sticky=tkinter.W, padx=xdis, pady=ydis)
    
        
    #buttons for numpad

    button1 = Button(gui, text=' 1 ',  
                     command=lambda: press(1), height=1, width=7) 
    button1.grid(row=3, column=0) 
  
    button2 = Button(gui, text=' 2 ',  
                     command=lambda: press(2), height=1, width=7) 
    button2.grid(row=3, column=1) 
  
    button3 = Button(gui, text=' 3 ',  
                     command=lambda: press(3), height=1, width=7) 
    button3.grid(row=3, column=2) 
  
    button4 = Button(gui, text=' 4 ',  
                     command=lambda: press(4), height=1, width=7) 
    button4.grid(row=4, column=0) 
  
    button5 = Button(gui, text=' 5 ', 
                     command=lambda: press(5), height=1, width=7) 
    button5.grid(row=4, column=1) 
  
    button6 = Button(gui, text=' 6 ', 
                     command=lambda: press(6), height=1, width=7) 
    button6.grid(row=4, column=2) 
  
    button7 = Button(gui, text=' 7 ',  
                     command=lambda: press(7), height=1, width=7) 
    button7.grid(row=5, column=0) 
  
    button8 = Button(gui, text=' 8 ', 
                     command=lambda: press(8), height=1, width=7) 
    button8.grid(row=5, column=1) 
  
    button9 = Button(gui, text=' 9 ', 
                     command=lambda: press(9), height=1, width=7) 
    button9.grid(row=5, column=2) 
  
    button0 = Button(gui, text=' 0 ',
                     command=lambda: press(0), height=1, width=7) 
    button0.grid(row=6, column=0)  
  
    clear = Button(gui, text='Clear',
                   command=clear, height=1, width=7) 
    clear.grid(row=6, column='1') 
  
    Decimal= Button(gui, text='.', 
                    command=lambda: press('.'), height=1, width=7) 
    Decimal.grid(row=6, column=2) 


    
gui.mainloop()

person Murat999    schedule 20.12.2020    source источник


Ответы (1)


Я мог бы решить это так:

#add text inside widget
def set_text(text):
    widget = gui.focus_get()
    if widget in [entry1, entry2]:
        widget.insert("insert", text)
    
def backspace():#Delete one digit at a time into perticular entry field
    pass

def clear():#Clear text in perticular entry field 
    entry1.delete(0,END)
    entry2.delete(0,END)



#create GUI
# Driver code 
if __name__ == "__main__": 
    # create a GUI window 
    gui = Tk()
    gui.title("GUI") 

   
    #Label 1
    label1 = tkinter.Label(text ="Pumpenhöhe 1 [cm]")
    label1.grid (row =0 , column =0 , padx = xdis , pady = ydis )

    #Eingabefeld 1 definieren
    entry1 = tkinter.Entry(gui, textvariable=StringVar(), width=4, bg ='#ffffff')
    entry1.grid(row=0, column=1, padx=xdis, pady = ydis)
    

    #Label 2
    label2 = tkinter.Label (text ="Pumpenhöhe 2 [cm]") 
    label2.grid(row=1,column =0 , padx = xdis ,pady = ydis)

    #Eingabefeld 2
    entry2 = tkinter.Entry(gui, textvariable=StringVar(), width=4,  bg ='#ffffff')
    entry2.grid(row=1, column=1, padx=xdis, pady = ydis)
    
    
    #create exit button
    ex_bt = tkinter.Button(gui, text='Exit', command=gui.destroy)
    ex_bt.grid(row=4, column=2, sticky=tkinter.W, padx=xdis, pady=ydis)
    
    #button obj to start thread
    #start_thread = tkinter.Button(text ="start thread(main loop)", command=start_thread)
    #start_thread.grid(row=2, column=1, padx=xdis, pady = ydis)
    
    #button obj to stop thread
    #stop_thread = tkinter.Button(text ="Stop", command=stop_thread)
    #stop_thread.grid(row=2, column=3, padx=xdis, pady = ydis)

    #button obj on framework to send values
    set_setpoints = tkinter.Button(text ="Send", command = set_setpoints)
    set_setpoints.grid(row=2, column=2, padx= xdis, pady = ydis)
    
    
    
        
    #buttons for numpad
    tkinter.Button(gui, text="7", command=lambda: set_text("7"),height=1, width=7).grid(row=5, column=0)
    tkinter.Button(gui, text="8", command=lambda: set_text("8"),height=1, width=7).grid(row=5, column=1)
    tkinter.Button(gui, text="9", command=lambda: set_text("9"),height=1, width=7).grid(row=5, column=2)
    tkinter.Button(gui, text="4", command=lambda: set_text("4"),height=1, width=7).grid(row=6, column=0)
    tkinter.Button(gui, text="5", command=lambda: set_text("5"),height=1, width=7).grid(row=6, column=1)
    tkinter.Button(gui, text="6", command=lambda: set_text("6"),height=1, width=7).grid(row=6, column=2)
    tkinter.Button(gui, text="1", command=lambda: set_text("1"),height=1, width=7).grid(row=7, column=0)
    tkinter.Button(gui, text="2", command=lambda: set_text("2"),height=1, width=7).grid(row=7, column=1)
    tkinter.Button(gui, text="3", command=lambda: set_text("3"),height=1, width=7).grid(row=7, column=2)
    tkinter.Button(gui, text="0", command=lambda: set_text("0"),height=1, width=7).grid(row=7, column=1)
    
    backspace = tkinter.Button(gui, text='<-', command = lambda:backspace())                      
    backspace.grid(row=3,column=1, padx=xdis, pady=ydis)
    
    clear_btn = Button(gui, text='C', command = lambda:clear())
    clear_btn.grid(row=3,column=2, padx=xdis, pady=ydis)


    
gui.mainloop()
person Murat999    schedule 20.12.2020