当用户按TKINTER中的新号码时,如何清除文本小部件内部的内容?
text = ""
e = Text(window, width = 15, height = 2, font = ("Times New Roman", 24))
e.grid(column = 0, row = 0, columnspan=5)
def show_value(val): # Displays the text when user clicks buttons
global text
text+=str(val)
e.insert(END, val)
def clear(): # Clears what's inside the text frame
global text
e.delete("1.0", "end")
text = ""
def delete(): # Deletes the last value/character
e.delete("end-2c")
def equal(): # Computes
global text
try:
text = str(eval(text))
e.delete("1.0", "end")
e.insert("1.0", text)
except:
clear()
e.insert("1.0", "Error")
#Buttons
# button_negative = Button(window, text = "+/-", width = 6, font = ("Arial", 10)) # ADD THIS SOON
button_zero = Button(window, text = "0", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(0))
button_one = Button(window, text = "1", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(1))
button_two = Button(window, text = "2", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(2))
button_three = Button(window, text = "3", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(3))
button_four = Button(window, text = "4", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(4))
button_five = Button(window, text = "5", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(5))
button_six = Button(window, text = "6", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(6))
button_seven = Button(window, text = "7", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(7))
button_eight = Button(window, text = "8", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(8))
button_nine = Button(window, text = "9", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(9))
button_equals = Button(window, text = "=", width = 6, font = ("Arial", 10), bg = "#7C3D00", fg = "white", command = equal)
button_decimal = Button(window, text = ".", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value("."))
button_add = Button(window, text = "+", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value("+"))
button_subtract = Button(window, text = "-", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value("-"))
button_divide = Button(window, text = "/", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value("/"))
button_multiply = Button(window, text = "*", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value("*"))
button_clear = Button(window, text = "AC", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = clear)
button_delete = Button(window, text = "DEL", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = delete)
button_left_bracket = Button(window, text = "(", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value("("))
button_right_bracket = Button(window, text = ")", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(")"))
#button_negative.grid(row = 7, column = 0)
button_zero.grid(row = 7, column = 0)
button_decimal.grid(row = 7, column = 1)
button_equals.grid(row = 7, column = 3)
button_delete.grid(row = 7, column = 2)
button_one.grid(row = 5, column = 0)
button_two.grid(row = 5, column = 1)
button_three.grid(row = 5, column = 2)
button_add.grid(row = 5, column = 3)
button_four.grid(row = 4, column = 0)
button_five.grid(row = 4, column = 1)
button_six.grid(row = 4, column = 2)
button_subtract.grid(row = 4, column = 3)
button_seven.grid(row = 3, column = 0)
button_eight.grid(row = 3, column = 1)
button_nine.grid(row = 3, column = 2)
button_multiply.grid(row = 3, column = 3)
button_clear.grid(row = 2, column = 0)
button_left_bracket.grid(row = 2, column = 1)
button_right_bracket.grid(row = 2, column = 2)
button_divide.grid(row = 2, column = 3)
window.mainloop()
我仍在学习Python,并想创建一个计算器作为我的第一个项目。我的问题是,当用户要计算8 + 8并单击“等价”按钮时,它将显示16。但是,我希望将其清除,而无需按所有清除按钮时,当用户想要计算一些新事物时,EG 56 + 6而是显示。我该怎么做?
text = ""
e = Text(window, width = 15, height = 2, font = ("Times New Roman", 24))
e.grid(column = 0, row = 0, columnspan=5)
def show_value(val): # Displays the text when user clicks buttons
global text
text+=str(val)
e.insert(END, val)
def clear(): # Clears what's inside the text frame
global text
e.delete("1.0", "end")
text = ""
def delete(): # Deletes the last value/character
e.delete("end-2c")
def equal(): # Computes
global text
try:
text = str(eval(text))
e.delete("1.0", "end")
e.insert("1.0", text)
except:
clear()
e.insert("1.0", "Error")
#Buttons
# button_negative = Button(window, text = "+/-", width = 6, font = ("Arial", 10)) # ADD THIS SOON
button_zero = Button(window, text = "0", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(0))
button_one = Button(window, text = "1", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(1))
button_two = Button(window, text = "2", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(2))
button_three = Button(window, text = "3", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(3))
button_four = Button(window, text = "4", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(4))
button_five = Button(window, text = "5", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(5))
button_six = Button(window, text = "6", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(6))
button_seven = Button(window, text = "7", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(7))
button_eight = Button(window, text = "8", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(8))
button_nine = Button(window, text = "9", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(9))
button_equals = Button(window, text = "=", width = 6, font = ("Arial", 10), bg = "#7C3D00", fg = "white", command = equal)
button_decimal = Button(window, text = ".", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value("."))
button_add = Button(window, text = "+", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value("+"))
button_subtract = Button(window, text = "-", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value("-"))
button_divide = Button(window, text = "/", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value("/"))
button_multiply = Button(window, text = "*", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value("*"))
button_clear = Button(window, text = "AC", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = clear)
button_delete = Button(window, text = "DEL", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = delete)
button_left_bracket = Button(window, text = "(", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value("("))
button_right_bracket = Button(window, text = ")", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(")"))
#button_negative.grid(row = 7, column = 0)
button_zero.grid(row = 7, column = 0)
button_decimal.grid(row = 7, column = 1)
button_equals.grid(row = 7, column = 3)
button_delete.grid(row = 7, column = 2)
button_one.grid(row = 5, column = 0)
button_two.grid(row = 5, column = 1)
button_three.grid(row = 5, column = 2)
button_add.grid(row = 5, column = 3)
button_four.grid(row = 4, column = 0)
button_five.grid(row = 4, column = 1)
button_six.grid(row = 4, column = 2)
button_subtract.grid(row = 4, column = 3)
button_seven.grid(row = 3, column = 0)
button_eight.grid(row = 3, column = 1)
button_nine.grid(row = 3, column = 2)
button_multiply.grid(row = 3, column = 3)
button_clear.grid(row = 2, column = 0)
button_left_bracket.grid(row = 2, column = 1)
button_right_bracket.grid(row = 2, column = 2)
button_divide.grid(row = 2, column = 3)
window.mainloop()
I'm still learning Python and wanted to create a Calculator as my first project. My issue is, when the user wants to compute 8 + 8 and clicks on the equals button, it displays 16. However, I want it to be cleared without pressing the all clear button when the user wants to calculate something new e.g 56 + 6 and displays that instead. How would I go about doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在像TKINTER一样操纵GUI时,建议您使用Python类,因为操纵元素会更容易。
至于您的问题,类似的东西是否适合您的需求?
我添加了一个布尔值(calcul_done)在false上初始化的,因此当您单击“+”,“” - “,...它将其值更改为true,然后再次单击一个数字时,调用了funtion clear()。
When manipulating GUI like tkinter, I recommend you to use Python classes, as it will be easier to manipulate elements.
As for your problem, does something like this suits your needs ?
I added a boolean (CALCUL_DONE) initialized at False, so when you click on "+","-",... it changes its value to True, and when clicking on a number again, the funtion clear() is called.
@sramazoth 答案是正确的基本想法使用键盘输入值(而不仅仅是显示的计算器的按钮)。问题是由于使用该全局
文本
正在使用。您的尝试使其内容与文本
中的内容相同,仅用于单击显示的button
s,但用户也可以直接输入通过键盘的文本
窗口小部件的值,您的代码不会检测到。我通过执行两件事(除了添加
global
flag之外,我都建议@sramazoth建议):全局文本
string acter clibal,这真的没有必要由于您需要的文本已经存储在text
小部件本身中。text
窗口小部件,该小部件将设置全局标志,就像单击一个按钮输入值一样。您还需要了解,将
eRAT()
应用于不受信任的用户输入是严重的安全危害,因为它们可以输入可以做坏事的任意代码。鉴于此警告,这是修改的代码:
@sramazoth answer is the right basic idea, but unfortunately the way your code is written it won't work if the user is using the keyboard to enter values (and not just the displayed calculator's buttons). The problem is due to the use of that global
text
variable being used. Your attempt to make its contents be the same as what is in theText
only works for clicking on theButton
s being displayed, but it's also possible for a user to directly enter values into theText
widget via the keyboard, which will not be detected by your code.I corrected the issue by doing two things (in addition to adding a
global
flag like @sramazoth suggested):global text
string variable completely — there's really no need for it since the text you need is already being stored in theText
widget itself.Text
widget which will set the global flag just like clicking on one of the buttons to enter a value would.You also need to understand that applying
eval()
to untrusted user input is a severe security hazard because they can enter arbitrary code that can do bad things.Given that caveat, here's the modified code: