如何将在计算器中按下的数字正确插入到最后一个索引的文本小部件中? [英] How do I properly insert the numbers that I pressed on in my calculator into the text widget at the last index?

查看:58
本文介绍了如何将在计算器中按下的数字正确插入到最后一个索引的文本小部件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的分数为"100"时,例如,在文本框中,它输出"001".我尝试在索引中使用-1,但仍然发生相同的事情,我也尝试执行.insert(0.end,num),但是会引发错误.如何将数字始终输入到输出的末尾.另外,这是使用tkinter输出数字的最佳方法还是还有其他方法?

When I pres "100" for example, in the text box it outputs "001". I try using -1 in the index but the same thing still happens, and I tried doing .insert(0. end, num) as well but it throws an error. How can I have the numbers always input at the end of the output. Also, is this the best way to output numbers with tkinter or are there other ways if any?

from tkinter import *
import operator

window = Tk()
window.title('Calculator')

def click(num):
    output.insert(0.0, num) #numbers not properly inputted (bug)

#output for calculator
output = Text(window, font = 'none 12 bold', height = 4, width = 25, wrap = 'word')
output.grid(row = 0, column = 0, columnspan = 4, pady = 10)

###buttons
#clear and operators
b_clear = Button(window, text = 'C', width = 7, height = 3)
b_clear.grid(row = 1, column = 2, padx = (10, 0))

b_div = Button(window, text = '/', width = 7, height = 3)
b_div.grid(row = 1, column = 3, padx = 10)

b_mult = Button(window, text = '*', width = 7, height = 3)
b_mult.grid(row = 2, column = 3)

b_subt = Button(window, text = '-', width = 7, height = 3)
b_subt.grid(row = 3, column = 3)

b_add = Button(window, text = '+', width = 7, height = 3)
b_add.grid(row = 4, column = 3)

b_equal = Button(window, text = '=', width = 7, height = 3)
b_equal.grid(row = 5, column = 3, pady = (0, 10))

#numbers
b_9 = Button(window, text = '9', width = 7, height = 3, command = lambda: click(9))
b_9.grid(row = 2, column = 2, padx = (10, 0), pady = 10)

b_8 = Button(window, text = '8', width = 7, height = 3, command = lambda: click(8))
b_8.grid(row = 2, column = 1)

b_7 = Button(window, text = '7', width = 7, height = 3, command = lambda: click(7))
b_7.grid(row = 2, column = 0, padx = 10)

b_6 = Button(window, text = '6', width = 7, height = 3, command = lambda: click(6))
b_6.grid(row = 3, column = 2, padx = (10, 0))

b_5 = Button(window, text = '5', width = 7, height = 3, command = lambda: click(5))
b_5.grid(row = 3, column = 1)

b_4 = Button(window, text = '4', width = 7, height = 3, command = lambda: click(4))
b_4.grid(row = 3, column = 0)

b_3 = Button(window, text = '3', width = 7, height = 3, command = lambda: click(3))
b_3.grid(row = 4, column = 2, padx = (10, 0), pady = 10)

b_2 = Button(window, text = '2', width = 7, height = 3, command = lambda: click(2))
b_2.grid(row = 4, column = 1)

b_1 = Button(window, text = '1', width = 7, height = 3, command = lambda: click(1))
b_1.grid(row = 4, column = 0)

b_0 = Button(window, text = '0', width = 7, height = 3, command = lambda: click(0))
b_0.grid(row = 5, column = 0, pady = (0, 10))

b_decimal = Button(window, text = '.', width = 7, height = 3)
b_decimal.grid(row = 5, column = 1, pady = (0, 10))

b_negative = Button(window, text = '-', width = 7, height = 3)
b_negative.grid(row = 5, column = 2, padx = (10, 0), pady = (0, 10))

#run calculator
window.mainloop()

推荐答案

索引"end"表示在小部件中最后一个字符之后的位置.

The index "end" represents the position just after the last character in the widget.

output.insert("end", num) 

摘自官方文件:

结尾-表示条目字符串中最后一个字符之后的字符.这等效于指定一个等于条目字符串长度的数字索引.

end - Indicates the character just after the last one in the entry's string. This is equivalent to specifying a numerical index equal to the length of the entry's string.

这篇关于如何将在计算器中按下的数字正确插入到最后一个索引的文本小部件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆