英文:
Tkinter - text inserted into text box appears backwards
问题
我目前正在尝试使用Tkinter编写一个计算器。到目前为止,我已经成功放置了所有按钮,并且为一些按钮(数字0-9、"+"按钮和"清除"按钮)添加了功能,但我遇到了困难,因为当我输入一个简单的方程式到我的计算器时,例如,11-1 - 窗口中显示的文本是颠倒的,而我的程序会反向读取它,并计算出1-11。
以下是代码。如果有人能指点我正确的方向,我将不胜感激。
```python
from tkinter import *
from tkinter import ttk
root = Tk()
window_width = 200
window_height = 300
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
center_x = int(screen_width/2 - window_width / 2)
center_y = int(screen_height/2 - window_height / 2)
root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
def clear_text():
entry_box.delete(0, END)
def print_one():
entry_box.insert(0, "1")
def print_two():
entry_box.insert(0, "2")
def print_three():
entry_box.insert(0, "3")
def print_four():
entry_box.insert(0, "4")
def print_five():
entry_box.insert(0, "5")
def print_six():
entry_box.insert(0, "6")
def print_seven():
entry_box.insert(0, "7")
def print_eight():
entry_box.insert(0, "8")
def print_nine():
entry_box.insert(0, "9")
def print_zero():
entry_box.insert(0, "0")
def print_plus():
entry_box.insert(0, "+")
def print_minus():
entry_box.insert(0, "-")
def print_divide():
entry_box.insert(0, "/")
def print_multiply():
entry_box.insert(0, "*")
def do_maths():
equation = entry_box.get()
evaluate_equation = eval(equation)
entry_box.delete(0, END)
entry_box.insert(0, evaluate_equation)
simple_calculator = Label(root,text="Simple Calculator").place(x=30, y=10)
entry_text = StringVar()
entry_box = ttk.Entry(root, textvariable=entry_text)
one = Button(root, text="1", command=print_one).place(x=30,y=50)
two = Button(root, text="2", command=print_two).place(x=60, y=50)
three = Button(root, text="3", command=print_three).place(x=90, y=50)
four = Button(root, text="4", command=print_four).place(x=30, y=80)
five = Button(root, text="5", command=print_five).place(x=60, y=80)
six = Button(root, text="6", command=print_six).place(x=90, y=80)
seven = Button(root, text="7", command=print_seven).place(x=30, y=110)
eight = Button(root, text="8", command=print_eight).place(x=60, y=110)
nine = Button(root, text="9", command=print_nine).place(x=90, y=110)
zero = Button(root, text="0", command=print_zero).place(x=30, y=140)
plus = Button(root, text="+", command=print_plus).place(x=60, y=140)
minus = Button(root, text="-", command=print_minus).place(x=90, y=140)
divide = Button(root, text="/", command=print_divide).place(x=30, y=170)
multiply = Button(root, text="*", command=print_multiply).place(x=60, y=170)
equals = Button(root, text="=", command=do_maths).place(x=90, y=170)
clear = Button(root, text="Clear", command=clear_text).place(x=30, y=200)
entry_box.place(x=30, y=230)
root.mainloop()
英文:
I'm currently attempting to write a calculator in Tkinter. So far, I have managed to place all the buttons and add functionality to some buttons (numbers 0-9, the "+" button and the "clear" button) but I am struggling because when a simple equation is input into my calculator. For example, 11-1 - the text that appears in the window is backwards, and my program reads it backwards, and will evaluate 1-11.
Here's the code. If anyone could point me in the right direction, that would be much appreciated.
from tkinter import *
from tkinter import ttk
root = Tk()
window_width = 200
window_height = 300
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
center_x = int(screen_width/2 - window_width / 2)
center_y = int(screen_height/2 - window_height / 2)
root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
def clear_text():
entry_box.delete(0, END)
def print_one():
entry_box.insert(0, "1")
def print_two():
entry_box.insert(0, "2")
def print_three():
entry_box.insert(0, "3")
def print_four():
entry_box.insert(0, "4")
def print_five():
entry_box.insert(0, "5")
def print_six():
entry_box.insert(0, "6")
def print_seven():
entry_box.insert(0, "7")
def print_eight():
entry_box.insert(0, "8")
def print_nine():
entry_box.insert(0, "9")
def print_zero():
entry_box.insert(0, "0")
def print_plus():
entry_box.insert(0, "+")
def print_minus():
entry_box.insert(0, "-")
def print_divide():
entry_box.insert(0, "/")
def print_multiply():
entry_box.insert(0, "*")
def do_maths():
equation = entry_box.get()
evaluate_equation = eval(equation)
entry_box.delete(0, END)
entry_box.insert(0, evaluate_equation)
simple_calculator = Label(root,text="Simple Calculator").place(x=30, y=10)
entry_text = StringVar()
entry_box = ttk.Entry(root, textvariable=entry_text)
one = Button(root, text="1", command=print_one).place(x=30,y=50)
two = Button(root, text="2", command=print_two).place(x=60, y=50)
three = Button(root, text="3", command=print_three).place(x=90, y=50)
four = Button(root, text="4", command=print_four).place(x=30, y=80)
five = Button(root, text="5", command=print_five).place(x=60, y=80)
six = Button(root, text="6", command=print_six).place(x=90, y=80)
seven = Button(root, text="7", command=print_seven).place(x=30, y=110)
eight = Button(root, text="8", command=print_eight).place(x=60, y=110)
nine = Button(root, text="9", command=print_nine).place(x=90, y=110)
zero = Button(root, text="0", command=print_zero).place(x=30, y=140)
plus = Button(root, text="+", command=print_plus).place(x=60, y=140)
minus = Button(root, text="-", command=print_minus).place(x=90, y=140)
divide = Button(root, text="/", command=print_divide).place(x=30, y=170)
multiply = Button(root, text="*", command=print_multiply).place(x=60, y=170)
equals = Button(root, text="=", command=do_maths).place(x=90, y=170)
clear = Button(root, text="Clear", command=clear_text).place(x=30, y=200)
entry_box.place(x=30, y=230)
root.mainloop()
答案1
得分: 3
在你的程序中,你正在将每个值(数字和方程式)插入到索引0的位置,这意味着你正在将它们插入到entry_box的开头,你需要做的是将每个值插入到entry_box的**'END'**位置。
from tkinter import *
from tkinter import ttk
root = Tk()
window_width = 200
window_height = 300
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
center_x = int(screen_width/2 - window_width / 2)
center_y = int(screen_height/2 - window_height / 2)
root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
def clear_text():
entry_box.delete(0, END)
def print_one():
entry_box.insert(END, "1")
def print_two():
entry_box.insert(END, "2")
def print_three():
entry_box.insert(END, "3")
def print_four():
entry_box.insert(END, "4")
def print_five():
entry_box.insert(END, "5")
def print_six():
entry_box.insert(END, "6")
def print_seven():
entry_box.insert(END, "7")
def print_eight():
entry_box.insert(END, "8")
def print_nine():
entry_box.insert(END, "9")
def print_zero():
entry_box.insert(END, "0")
def print_plus():
entry_box.insert(END, "+")
def print_minus():
entry_box.insert(END, "-")
def print_divide():
entry_box.insert(END, "/")
def print_multiply():
entry_box.insert(END, "*")
def do_maths():
equation = entry_box.get()
evaluate_equation = eval(equation)
entry_box.delete(0, END)
entry_box.insert(0, evaluate_equation)
simple_calculator = Label(root, text="Simple Calculator").place(x=30, y=10)
entry_text = StringVar()
entry_box = ttk.Entry(root, textvariable=entry_text)
one = Button(root, text="1", command=print_one).place(x=30,y=50)
two = Button(root, text="2", command=print_two).place(x=60, y=50)
three = Button(root, text="3", command=print_three).place(x=90, y=50)
four = Button(root, text="4", command=print_four).place(x=30, y=80)
five = Button(root, text="5", command=print_five).place(x=60, y=80)
six = Button(root, text="6", command=print_six).place(x=90, y=80)
seven = Button(root, text="7", command=print_seven).place(x=30, y=110)
eight = Button(root, text="8", command=print_eight).place(x=60, y=110)
nine = Button(root, text="9", command=print_nine).place(x=90, y=110)
zero = Button(root, text="0", command=print_zero).place(x=30, y=140)
plus = Button(root, text="+", command=print_plus).place(x=60, y=140)
minus = Button(root, text="-", command=print_minus).place(x=90, y=140)
divide = Button(root, text="/", command=print_divide).place(x=30, y=170)
multiply = Button(root, text="*", command=print_multiply).place(x=60, y=170)
equals = Button(root, text="=", command=do_maths).place(x=90, y=170)
clear = Button(root, text="Clear", command=clear_text).place(x=30, y=200)
entry_box.place(x=30, y=230)
root.mainloop()
在这里,我只是将每个插入的索引从0更改为END。
END 表示用户输入的最后一个字符之后的位置。
英文:
so in your program , you're inserting each value(numbers and equations) at the index of 0 which means you are inserting them at the start of the entry_box , what you need to do is to insert each value at the 'END' of the entry_box
from tkinter import *
from tkinter import ttk
root = Tk()
window_width = 200
window_height = 300
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
center_x = int(screen_width/2 - window_width / 2)
center_y = int(screen_height/2 - window_height / 2)
root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
def clear_text():
entry_box.delete(0, END)
def print_one():
entry_box.insert(END, "1")
def print_two():
entry_box.insert(END, "2")
def print_three():
entry_box.insert(END, "3")
def print_four():
entry_box.insert(END, "4")
def print_five():
entry_box.insert(END, "5")
def print_six():
entry_box.insert(END, "6")
def print_seven():
entry_box.insert(END, "7")
def print_eight():
entry_box.insert(END, "8")
def print_nine():
entry_box.insert(END, "9")
def print_zero():
entry_box.insert(END, "0")
def print_plus():
entry_box.insert(END, "+")
def print_minus():
entry_box.insert(END, "-")
def print_divide():
entry_box.insert(END, "/")
def print_multiply():
entry_box.insert(END, "*")
def do_maths():
equation = entry_box.get()
evaluate_equation = eval(equation)
entry_box.delete(0, END)
entry_box.insert(0, evaluate_equation)
simple_calculator = Label(root,text="Simple Calculator").place(x=30, y=10)
entry_text = StringVar()
entry_box = ttk.Entry(root, textvariable=entry_text)
one = Button(root, text="1", command=print_one).place(x=30,y=50)
two = Button(root, text="2", command=print_two).place(x=60, y=50)
three = Button(root, text="3", command=print_three).place(x=90, y=50)
four = Button(root, text="4", command=print_four).place(x=30, y=80)
five = Button(root, text="5", command=print_five).place(x=60, y=80)
six = Button(root, text="6", command=print_six).place(x=90, y=80)
seven = Button(root, text="7", command=print_seven).place(x=30, y=110)
eight = Button(root, text="8", command=print_eight).place(x=60, y=110)
nine = Button(root, text="9", command=print_nine).place(x=90, y=110)
zero = Button(root, text="0", command=print_zero).place(x=30, y=140)
plus = Button(root, text="+", command=print_plus).place(x=60, y=140)
minus = Button(root, text="-", command=print_minus).place(x=90, y=140)
divide = Button(root, text="/", command=print_divide).place(x=30, y=170)
multiply = Button(root, text="*", command=print_multiply).place(x=60, y=170)
equals = Button(root, text="=", command=do_maths).place(x=90, y=170)
clear = Button(root, text="Clear", command=clear_text).place(x=30, y=200)
entry_box.place(x=30, y=230)
root.mainloop()
here I just changed the indexes in each insert from 0 to END
> END represents the point immediately after the last character entered
> by the user
答案2
得分: 0
你的 .insert()
语句被设置为索引 0
,所以字符被放置在 entry_box
的开头。你可以根据以下模式更改所有的函数。
def print_one():
entry_box.insert(len(entry_text.get()), "1")
英文:
Your .insert()
statements are set to index 0
so the characters are placed at the beginning of entry_box
. You can change all of your functions according to the pattern below.
def print_one():
entry_box.insert(len(entry_text.get()), "1")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论