条目不能正确响应python中按钮的绑定

条目不能正确响应python中按钮的绑定,python,tkinter,calculator,tkinter-entry,Python,Tkinter,Calculator,Tkinter Entry,我现在正试图用python编写一个计算器,但我的条目有一个问题,我无法解决它,尽管我看不出有任何问题 下面是我的代码示例: from Tkinter import * window =Tk() window.title("Calculator") #creating an entry string=StringVar entry = Entry(window, width=40,textvariable=string ) entry.grid(row=0, column=0, columnsp

我现在正试图用python编写一个计算器,但我的条目有一个问题,我无法解决它,尽管我看不出有任何问题

下面是我的代码示例:

from Tkinter import *

window =Tk()
window.title("Calculator")
#creating an entry
string=StringVar
entry = Entry(window, width=40,textvariable=string )
entry.grid(row=0, column=0, columnspan=6, ipady=10)
entry.focus()


#basically I have a function for creating buttons but here I will do it the traditional way.

num_one=Button(window,text="1",width=2,height=2,padx=20,pady=20,)
num_one.grid(row=1,column=0,padx=1,pady=1)

#crating an index for the calculator
index=0
#creating a function to insert the number one to the entry in the index position and then add one to the index

def print_one(index):
    entry.insert(index,"1")

binding the num_one button to the function above

num_one.bind("Button-1",print_one(index))

现在的问题是,只有在我单击“数字一”按钮时,字符串1才应该输入到条目中,但当我自动启动程序时,数字1进入条目中。

我在您的代码中注意到了很多问题-

string=StringVar-您需要像StringVar一样调用它,否则您只是将StringVar类而不是其对象设置为`string

当你这样做的时候-

num_one.bind("Button-1",print_one(index))
实际上,您首先调用函数并绑定返回值,您应该在不调用它的情况下绑定函数对象,例如-

num_one.bind("<Button-1>",print_one)

正如Anand所说,您当前的代码在语法和设计方面都存在各种问题

我不确定您为什么要自己跟踪条目的索引,因为条目小部件已经这样做了。要在当前光标位置插入文本,可以在entry.insert方法调用中使用Tkinter.insert

看起来您打算为每个数字按钮编写一个单独的回调函数。这是不必要的,而且可能会变得一团糟

下面的代码显示了对多个按钮使用单个回调函数的方法。我们将按钮的编号作为属性附加到按钮本身。回调函数可以轻松访问该数字,因为调用回调的事件对象参数包含将其作为属性激活的小部件

请注意,我的代码使用import Tkinter作为tk,而不是从Tkinter import*。当然,这会使代码更加冗长,但它可以防止名称冲突

import Tkinter as tk

window = tk.Tk()
window.title("Calculator")

entry_string = tk.StringVar()
entry = tk.Entry(window, width=40, textvariable=entry_string)
entry.grid(row=0, column=0, columnspan=6, ipady=10)
entry.focus()

def button_cb(event):
    entry.insert(tk.INSERT, event.widget.number)

for i in range(10):
    y, x = divmod(9 - i, 3)
    b = tk.Button(window, text=i, width=2, height=2, padx=20, pady=20)
    b.grid(row=1+y, column=2-x, padx=1, pady=1)
    #Save this button's number so it can be accessed in the callback
    b.number = i
    b.bind("<Button-1>", button_cb)

window.mainloop()

理想情况下,GUI代码应该放在一个类中,因为这样可以使小部件更容易共享数据,而且会使代码更整洁。

谢谢!我不明白你在第四题中的解释。你能再给我解释一下吗?检查这里-当点击按钮时,你不会得到索引作为第一个参数。相反,你可以获取数据,然后在添加1后将其设置回原来的位置。谢谢你!解决了这个问题!在函数中,我需要将它作为一个参数设置为event,然后将index声明为一个全局变量。在条目中插入数字1,然后在索引中添加数字1。好的,如果答案对您有帮助,也很酷。我想建议您通过单击答案左侧的勾号来接受答案,这将对社区有帮助。@TheTechGuy:FWIW,使用字符串作为变量名不是一个好主意,因为它是一个变量的名称。这样做实际上并不是一个错误,如果您的代码没有导入该模块,也不会造成任何伤害,但这可能会让阅读代码的人感到困惑。如果你真的因为某种原因改变了你的代码来导入字符串,那么你会得到神秘的bug。哇,这真的让代码变得容易多了!在你的代码中我唯一不认识的是divemode。它与里面的值有什么关系?@TheTechGuy:divmod是一个内置函数,可以进行整数除法。它以元组的形式返回商和余数。有关更多信息,请参阅官方Python文档。
import Tkinter as tk

window = tk.Tk()
window.title("Calculator")

entry_string = tk.StringVar()
entry = tk.Entry(window, width=40, textvariable=entry_string)
entry.grid(row=0, column=0, columnspan=6, ipady=10)
entry.focus()

def button_cb(event):
    entry.insert(tk.INSERT, event.widget.number)

for i in range(10):
    y, x = divmod(9 - i, 3)
    b = tk.Button(window, text=i, width=2, height=2, padx=20, pady=20)
    b.grid(row=1+y, column=2-x, padx=1, pady=1)
    #Save this button's number so it can be accessed in the callback
    b.number = i
    b.bind("<Button-1>", button_cb)

window.mainloop()