to create a button when pressed print the number on that button in the entry box in python 3.3

后端 未结 2 1204
囚心锁ツ
囚心锁ツ 2020-12-20 07:30

i want to make a button on which \'2\' is written .... now when anyone click it , it will show the number \'2\' in the entry box... the error is: before clicking , it is a

2条回答
  •  遥遥无期
    2020-12-20 07:49

    Pass a function (used lambda in the following code) instead of return value of the function.

    from tkinter import * 
    
    root = Tk()  
    e1 = Entry(root)  
    e1.pack()
    
    def add(x):  
        e1.insert(INSERT, x)  
    
    a = Button(root, text='2', command=lambda: add(2))
    a.pack()
    
    root.mainloop()
    

    In addition to that, extract Entry creation code out of the add function. Otherwise entry are create every time when the button is clicked.

提交回复
热议问题