如何使用 <entry_box_name>.insert 在输入框中正确输入数字
How do I put numbers the right way around in Entry box using <entry_box_name>.insert
我目前正在 Python 中开发一个计算器应用程序,当您单击一个按钮时,它会将该按钮上的符号输入到显示在 window 顶部的输入框中。但是,当我单击一个数字按钮时 - 假设我单击上面带有“4”的按钮,然后单击上面带有“6”的按钮,输入框显示数字 64,而不是数字 46。
下面是单击“1”按钮时将数字 1 插入输入框(名为 user_output)的代码:
def insert_one():
user_output.insert(0, "1")
如果您在计算器中输入 563,您会得到以下结果:http://prntscr.com/k7zugb
有人知道如何解决这个问题吗?
编辑: 一位名叫 Bryan Oakley 的用户要求提供插图以显示部分代码。这是演示,因此您可以看到将数字输入 user_output:
时代码如何工作
try:
from Tkinter import *
except ImportError:
from tkinter import *
window = Tk()
window.title("Demo")
window.geometry("323x75")
def insert_one():
user_output.insert(0, "1")
def insert_two():
user_output.insert(0, "2")
def insert_three():
user_output.insert(0, "3")
user_output = Entry(width=53, justify=LEFT)
one = Button(text="1", height=3, width=2, justify=LEFT, command=insert_one)
two = Button(text="2", height=3, width=2, justify=LEFT, command=insert_two)
three = Button(text="3", height=3, width=2, justify=LEFT, command=insert_three)
user_output.grid(row=0, columnspan=3)
one.grid(row=1, column=0, sticky=N+S+E+W)
two.grid(row=1, column=1, sticky=N+S+E+W)
three.grid(row=1, column=2, sticky=N+S+E+W)
window.mainloop()
简单的解决方案是使用索引 "end" 而不是零。
你的问题是你如何使用插入。 user_output.insert(0, "1")
。在这里,您在零索引处进行估算,换句话说,每次您按下按钮时,它总是会在前面插入数字。而只是使用 "end" 作为您的索引。
try:
from Tkinter import *
except ImportError:
from tkinter import *
window = Tk()
window.title("Demo")
window.geometry("323x75")
def insert_one():
user_output.insert("end", "1")
def insert_two():
user_output.insert("end", "2")
def insert_three():
user_output.insert("end", "3")
user_output = Entry(width=53, justify=LEFT)
one = Button(text="1", height=3, width=2, justify=LEFT, command=insert_one)
two = Button(text="2", height=3, width=2, justify=LEFT, command=insert_two)
three = Button(text="3", height=3, width=2, justify=LEFT, command=insert_three)
user_output.grid(row=0, columnspan=3)
one.grid(row=1, column=0, sticky=N+S+E+W)
two.grid(row=1, column=1, sticky=N+S+E+W)
three.grid(row=1, column=2, sticky=N+S+E+W)
window.mainloop()
我目前正在 Python 中开发一个计算器应用程序,当您单击一个按钮时,它会将该按钮上的符号输入到显示在 window 顶部的输入框中。但是,当我单击一个数字按钮时 - 假设我单击上面带有“4”的按钮,然后单击上面带有“6”的按钮,输入框显示数字 64,而不是数字 46。
下面是单击“1”按钮时将数字 1 插入输入框(名为 user_output)的代码:
def insert_one():
user_output.insert(0, "1")
如果您在计算器中输入 563,您会得到以下结果:http://prntscr.com/k7zugb
有人知道如何解决这个问题吗?
编辑: 一位名叫 Bryan Oakley 的用户要求提供插图以显示部分代码。这是演示,因此您可以看到将数字输入 user_output:
时代码如何工作try:
from Tkinter import *
except ImportError:
from tkinter import *
window = Tk()
window.title("Demo")
window.geometry("323x75")
def insert_one():
user_output.insert(0, "1")
def insert_two():
user_output.insert(0, "2")
def insert_three():
user_output.insert(0, "3")
user_output = Entry(width=53, justify=LEFT)
one = Button(text="1", height=3, width=2, justify=LEFT, command=insert_one)
two = Button(text="2", height=3, width=2, justify=LEFT, command=insert_two)
three = Button(text="3", height=3, width=2, justify=LEFT, command=insert_three)
user_output.grid(row=0, columnspan=3)
one.grid(row=1, column=0, sticky=N+S+E+W)
two.grid(row=1, column=1, sticky=N+S+E+W)
three.grid(row=1, column=2, sticky=N+S+E+W)
window.mainloop()
简单的解决方案是使用索引 "end" 而不是零。
你的问题是你如何使用插入。 user_output.insert(0, "1")
。在这里,您在零索引处进行估算,换句话说,每次您按下按钮时,它总是会在前面插入数字。而只是使用 "end" 作为您的索引。
try:
from Tkinter import *
except ImportError:
from tkinter import *
window = Tk()
window.title("Demo")
window.geometry("323x75")
def insert_one():
user_output.insert("end", "1")
def insert_two():
user_output.insert("end", "2")
def insert_three():
user_output.insert("end", "3")
user_output = Entry(width=53, justify=LEFT)
one = Button(text="1", height=3, width=2, justify=LEFT, command=insert_one)
two = Button(text="2", height=3, width=2, justify=LEFT, command=insert_two)
three = Button(text="3", height=3, width=2, justify=LEFT, command=insert_three)
user_output.grid(row=0, columnspan=3)
one.grid(row=1, column=0, sticky=N+S+E+W)
two.grid(row=1, column=1, sticky=N+S+E+W)
three.grid(row=1, column=2, sticky=N+S+E+W)
window.mainloop()