我想在python中编程一个计算器一段时间,并且我的条目有一个问题,但我无法解决,虽然我没有看到任何它的问题。条目没有正确响应按钮在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”应输入的条目只有当我点击n个num_one按钮,但是当我开始自动编程数字“1”进入条目。
谢谢!我没有理解你在第4号的解释。你能不能再给我解释一遍? – TheTechGuy
请点击此处 - http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm 单击按钮时,您不会将'index'作为第一个参数。相反,您可以获取数据,然后在添加'1'后将其设置回来。 –
谢谢你!解决了问题!在函数中,我需要将它作为参数,然后将索引声明为全局变量,将第一个数字插入到条目中,并将第一个索引添加到索引中。 – TheTechGuy