python - 如何在python(tkinter)中计算条目中的输入
问题描述
我想知道如何计算条目中的表达式?假设条目中的输入是 2+2。我想要一个变量来存储该输入,而不是作为字符串,而是作为表达式。我不能将每个数字单独转换为浮点数,所以我不知道如何。这是我尝试使用的代码:
import pyautogui, math
from tkinter import *
def arithmetic_calc_press():
global root
root.destroy()
arith_calc = Tk()
bindings = {
'<FocusIn>': {'default': 'active'},
'<FocusOut>': {'default': 'active'}
}
for k, v in bindings.items():
arith_calc.bind_class('Button', k, lambda e, kwarg=v: e.widget.config(**kwarg))
arith_calc.geometry('302x415')
arith_calc.config(bg='black')
enter_number = Entry(arith_calc, width=70, bg='black', fg='lime', bd=0)
enter_number.grid(row=0, column=0, columnspan=100)
def click(num):
current = enter_number.get()
enter_number.delete(0, END)
enter_number.insert(0, str(current) + str(num))
def back():
arith_calc.destroy()
def clear():
enter_number.delete(0, END)
def equal():
global equal, f_num
second_number = enter_number.get()
enter_number.delete(0, END)
if equal == 'plus':
enter_number.insert(0, f_num + float(second_number))
if equal == 'minus':
enter_number.insert(0, f_num - float(second_number))
if equal == 'mul':
enter_number.insert(0, f_num * float(second_number))
if equal == 'div':
enter_number.insert(0, f_num / float(second_number))
def plus():
first_number = enter_number.get()
global f_num, equal
equal = 'plus'
f_num = float(first_number)
enter_number.delete(0, END)
def minus():
first_number = enter_number.get()
global f_num, equal
equal = 'minus'
f_num = float(first_number)
enter_number.delete(0, END)
def mul():
first_number = enter_number.get()
global f_num, equal
equal = 'mul'
f_num = float(first_number)
enter_number.delete(0, END)
def div():
first_number = enter_number.get()
global f_num, equal
equal = 'div'
f_num = float(first_number)
enter_number.delete(0, END)
enter_number.delete(0, END)
b7 = Button(arith_calc, text='7', bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime", highlightthickness=2, relief=SOLID, default='active', padx=41, pady=20, command=lambda: click(7)).grid(row=1, column=0)
b8 = Button(arith_calc, text='8', bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime",
highlightthickness=2, relief=SOLID, default='active', padx=41, pady=20, command=lambda: click(8)).grid(row=1, column=1)
b9 = Button(arith_calc, text='9', bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime",
highlightthickness=2, relief=SOLID, default='active', padx=41, pady=20, command=lambda: click(9)).grid(row=1, column=2)
b4 = Button(arith_calc, text='4', bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime",
highlightthickness=2, relief=SOLID, default='active', padx=41, pady=20, command=lambda: click(4)).grid(row=2, column=0)
b5 = Button(arith_calc, text='5', bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime",
highlightthickness=2, relief=SOLID, default='active', padx=41, pady=20, command=lambda: click(5)).grid(row=2, column=1)
b6 = Button(arith_calc, text='6', bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime",
highlightthickness=2, relief=SOLID, default='active', padx=41, pady=20, command=lambda: click(6)).grid(row=2, column=2)
b1 = Button(arith_calc, text='1', bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime",
highlightthickness=2, relief=SOLID, default='active', padx=41, pady=20, command=lambda: click(1)).grid(row=3, column=0)
b2 = Button(arith_calc, text='2', bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime",
highlightthickness=2, relief=SOLID, default='active', padx=41, pady=20, command=lambda: click(2)).grid(row=3, column=1)
b3 = Button(arith_calc, text='3', bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime",
highlightthickness=2, relief=SOLID, default='active', padx=41, pady=20, command=lambda: click(3)).grid(row=3, column=2)
b0 = Button(arith_calc, text='0', bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime",
highlightthickness=2, relief=SOLID, default='active', padx=41, pady=20, command=lambda: click(0)).grid(row=4, column=0)
b_clear = Button(arith_calc, text='Clear', bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime",
highlightthickness=2, relief=SOLID, default='active', padx=31, pady=20, command=clear).grid(row=4, column=2)
b_plus = Button(arith_calc, text='+', bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime",
highlightthickness=2, relief=SOLID, default='active', padx=40, pady=20, command=plus).grid(
row=6, column=0)
b_minus = Button(arith_calc, text='-', bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime",
highlightthickness=2, relief=SOLID, default='active', padx=41, pady=20, command=minus).grid(
row=6, column=1)
b_mul = Button(arith_calc, text='x', bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime",
highlightthickness=2, relief=SOLID, default='active', padx=41, pady=20, command=mul).grid(
row=5, column=1)
b_div = Button(arith_calc, text=':', bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime",
highlightthickness=2, relief=SOLID, default='active', padx=43, pady=20, command=div).grid(row=5,
column=0)
b_equal = Button(arith_calc, text='=', bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime",
highlightthickness=2, relief=SOLID, default='active', padx=40, pady=20, command=equal).grid(row=4, column=1)
b_decimal = Button(arith_calc, text='.', bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime",
highlightthickness=2, relief=SOLID, default='active', padx=43, pady=52, command=lambda: click('.')).grid(row=5, column=2, rowspan=2)
def geometric_calc_press():
pass
def quadratic_equation():
global root
root.destroy()
quad_eq = Tk()
bindings = {
'<FocusIn>': {'default': 'active'},
'<FocusOut>': {'default': 'active'}
}
for k, v in bindings.items():
quad_eq.bind_class('Button', k, lambda e, kwarg=v: e.widget.config(**kwarg))
quad_eq.geometry('300x400')
quad_eq.config(bg='black')
def confirm():
x = entry.get()
print(float(x))
label = Label(quad_eq, text='Enter a quadratic equation of the ', bg='black', fg='lime').pack()
label2 = Label(quad_eq, text='form a*2x + bx + c or x*2 + px = q.', fg='lime', bg='black').pack()
entry = Entry(quad_eq, bg='white', fg='black')
entry.pack()
confirm = Button(quad_eq, text='Confirm!', bg='black', fg='lime', highlightcolor="lime", highlightbackground="lime",
highlightthickness=2, relief=SOLID, default='active', padx=34, pady=12, command=confirm).pack()
root = Tk()
root.geometry('1000x600')
bindings = {
'<FocusIn>': {'default': 'active'},
'<FocusOut>': {'default': 'active'}
}
for k, v in bindings.items():
root.bind_class('Button', k, lambda e, kwarg=v: e.widget.config(**kwarg))
root.title('Calculator')
root.config(bg='black')
Button(root, text='Arithmetic Calculator', padx=106, bg='black', fg='lime',highlightcolor="lime", highlightbackground="lime", highlightthickness=2, relief=SOLID, default='active', command=arithmetic_calc_press).grid(row=0, column=0)
Button(root, text='Geometric Calculator', padx=106, bg='black', fg='lime',highlightcolor="lime", highlightbackground="lime", highlightthickness=2, relief=SOLID, default='active', command=geometric_calc_press).grid(row=0, column=1)
Button(root, text='Quadratic Equation', padx=106, bg='black', fg='lime',highlightcolor="lime", highlightbackground="lime", highlightthickness=2, relief=SOLID, default='active', command=quadratic_equation).grid(row=0, column=2)
root.mainloop()
有什么特别的事情我需要做的。我不知道,因为我只在 python 中编程了 3-4 个月。
解决方案
推荐阅读
- java - 在 jsp 页面中的 location.href 中传递当前用户名
- macos - VSCode如何折叠/展开所有子区域
- leakcanary - 此泄漏是否是由 Android 11 DP2 引起的
- r - dplyr::mutate_at 遍历函数中的列
- r - 来自不同data.frame的样本
- string - 字符串比较时间复杂度下限
- c# - Linq 查询处理结果的时间太长
- android - onPolygonClick 检测不同的多边形
- spring-data-jpa - 如何在 Spring Data JPA 的 @Query 中使用 LIMIT 25
- python - 尝试清除文本时出现 SettingWithCopyWarning 错误