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。但是,當用戶想要計算新的東西時,我希望它被清除而不按全部清除按鈕,例如 56 6并改為顯示。我該怎么做呢?
uj5u.com熱心網友回復:
在像 tkinter 這樣操作 GUI 時,我建議您使用 Python 類,因為它會更容易操作元素。
至于您的問題,這樣的事情是否適合您的需求?
#!/usr/bin/python3
import tkinter as tk
text = ""
CALCUL_DONE=False
window=tk.Tk()
e = tk.Text(window, width = 15, height = 2, font = ("Times New Roman", 24))
e.grid(column = 0, row = 0, columnspan=5)
def clear(): # Clears what's inside the text frame
global text
e.delete("1.0", "end")
text = ""
def show_value(val): # Displays the text when user clicks buttons
global text, CALCUL_DONE
if CALCUL_DONE:
clear()
CALCUL_DONE=False
text =str(val)
e.insert(tk.END, val)
def delete(): # Deletes the last value/character
e.delete("end-2c")
def equal(): # Computes
global text, CALCUL_DONE
try:
text = str(eval(text))
e.delete("1.0", "end")
e.insert("1.0", text)
CALCUL_DONE=True
except:
clear()
e.insert("1.0", "Error")
#Buttons
# button_negative = tk.Button(window, text = " /-", width = 6, font = ("Arial", 10)) # ADD THIS SOON
button_zero = tk.Button(window, text = "0", width = 6, font = ("Arial", 10), bg = "black", fg = "white", command = lambda: show_value(0))
[...]
button_right_bracket = tk.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_divide.grid(row = 2, column = 3)
window.mainloop()
我添加了一個布林值 (CALCUL_DONE),初始化為 False,因此當您單擊“ ”、“-”、...時,它會將其值更改為 True,并且當再次單擊一個數字時,會呼叫 clear() 函式。
uj5u.com熱心網友回復:
@sramazoth答案是正確的基本思想,但不幸的是,如果用戶使用鍵盤輸入值(而不僅僅是顯示的計算器的按鈕),您的代碼撰寫方式將不起作用。問題是由于使用了text正在使用的全域變數。您嘗試使其內容與Text單擊Button正在顯示的 s 唯一有效的內容相同,但用戶也可以Text通過鍵盤直接將值輸入到小部件中,您的代碼不會檢測到.
我通過做兩件事糾正了這個問題(除了添加global像@sramazoth 建議的標志):
- 完全消除
global text字串變數——實際上不需要它,因為您需要的文本已經存盤在Text小部件本身中。 - 添加了一個按鍵事件處理程式并將其系結到
Text將設定全域標志的小部件,就像單擊其中一個按鈕輸入值一樣。
您還需要了解,應用于eval()不受信任的用戶輸入是一種嚴重的安全隱患,因為他們可以輸入可以做壞事的任意代碼。
鑒于此警告,這是修改后的代碼:
import tkinter as tk
from tkinter.constants import *
clear_flag = False # Indicates when Text widget should be cleared.
window = tk.Tk()
e = tk.Text(window, width=15, height=2, font=("Times New Roman", 24))
e.grid(column=0, row=0, columnspan=5)
def on_keypress(event):
global clear_flag
if clear_flag:
clear()
clear_flag = False
e.bind('<KeyPress>', on_keypress)
def show_value(val): # Displays the expression when user clicks buttons
global clear_flag
if clear_flag:
clear()
clear_flag = False
e.insert(END, val)
def clear(): # Clears what's inside the expression frame
e.delete("1.0", "end")
def delete(): # Deletes the last value/character
e.delete("end-2c")
def equal(): # Computes
global clear_flag
try:
result = eval(e.get("1.0", END))
e.delete("1.0", "end")
e.insert("1.0", result)
except Exception as exc:
clear()
e.insert("1.0", "Error")
clear_flag = True
#Buttons
# button_negative = tk.Button(window, text=" /-", width=6, font=("Arial", 10)) # ADD THIS SOON
button_zero = tk.Button(window, text="0", width=6, font=("Arial", 10), bg="black", fg="white", command=lambda: show_value(0))
button_one = tk.Button(window, text="1", width=6, font=("Arial", 10), bg="black", fg="white", command=lambda: show_value(1))
button_two = tk.Button(window, text="2", width=6, font=("Arial", 10), bg="black", fg="white", command=lambda: show_value(2))
button_three = tk.Button(window, text="3", width=6, font=("Arial", 10), bg="black", fg="white", command=lambda: show_value(3))
button_four = tk.Button(window, text="4", width=6, font=("Arial", 10), bg="black", fg="white", command=lambda: show_value(4))
button_five = tk.Button(window, text="5", width=6, font=("Arial", 10), bg="black", fg="white", command=lambda: show_value(5))
button_six = tk.Button(window, text="6", width=6, font=("Arial", 10), bg="black", fg="white", command=lambda: show_value(6))
button_seven = tk.Button(window, text="7", width=6, font=("Arial", 10), bg="black", fg="white", command=lambda: show_value(7))
button_eight = tk.Button(window, text="8", width=6, font=("Arial", 10), bg="black", fg="white", command=lambda: show_value(8))
button_nine = tk.Button(window, text="9", width=6, font=("Arial", 10), bg="black", fg="white", command=lambda: show_value(9))
button_equals = tk.Button(window, text="=", width=6, font=("Arial", 10), bg="#7C3D00", fg="white", command=equal)
button_decimal = tk.Button(window, text=".", width=6, font=("Arial", 10), bg="black", fg="white", command=lambda: show_value("."))
button_add = tk.Button(window, text=" ", width=6, font=("Arial", 10), bg="black", fg="white", command=lambda: show_value(" "))
button_subtract = tk.Button(window, text="-", width=6, font=("Arial", 10), bg="black", fg="white", command=lambda: show_value("-"))
button_divide = tk.Button(window, text="/", width=6, font=("Arial", 10), bg="black", fg="white", command=lambda: show_value("/"))
button_multiply = tk.Button(window, text="*", width=6, font=("Arial", 10), bg="black", fg="white", command=lambda: show_value("*"))
button_clear = tk.Button(window, text="AC", width=6, font=("Arial", 10), bg="black", fg="white", command=clear)
button_delete = tk.Button(window, text="DEL", width=6, font=("Arial", 10), bg="black", fg="white", command=delete)
button_left_bracket = tk.Button(window, text="(", width=6, font=("Arial", 10), bg="black", fg="white", command=lambda: show_value("("))
button_right_bracket=tk.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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/492995.html
