2017-07-25 60 views
1

我是python编程的新手,我在用Tkinter开发GUI的特定部分时遇到了一些问题。如何在Tkinter Python中创建表达式

我想要做的是,一个空间,用户可以输入(键入)他的数学公式,软件使用先前计算的变量进行计算。

我发现很多Tkinter的计算器,但没有一个是我正在寻找的。我没有太多的类定义经验。

我做了这个简单的布局,以更好地解释什么,我想做的事:

import tkinter as tk 

root = tk.Tk() 

Iflabel = tk.Label(root, text = "If...") 
Iflabel.pack() 
IfEntry = tk.Entry(root) 
IfEntry.pack() 

thenlabel = tk.Label(root, text = "Then...") 
thenEntry = tk.Entry(root) 
thenlabel.pack() 
thenEntry.pack() 

elselabel = tk.Label(root, text = "else..") 
elseEntry = tk.Entry(root) 
elselabel.pack() 
elseEntry.pack() 

applybutton = tk.Button(root, text = "Calculate") 
applybutton.pack() 

root.mainloop() 

为Python 3这个简单的代码有3个空间

1)如果...

第二然后...

3)否则...

因此,用户将输入他的条件表达和软件将完成这项工作。在我看来,另一个重要的事情是,如果用户将“if”空格留空,他只需在“Then ...”输入内输入他的表达式,然后按下“calculate”按钮或使用语句构建所有表达式。

如果有人能提供关于如何以及怎样做的一些想法....

(无类,如果可能的话)

I'l给出using语句例证 1日一些情况:

var = the variable previously calculated and stored in the script 

out = output 

if var >= 10 

then out = 4 

else out = 2 

第二不使用语句的用户将输入“然后”进入了他想表达的计算,这将是:

Then: Out = (((var)**2) +(2*var))**(1/2) 

再说一遍,这只是例证......我不需要这个特定的布局。如果有人有一个想法如何更好地构建它,是值得欢迎的。

谢谢大家。

+0

你能给你的if-then-else的一些例子表情?当然是 – scotty3785

+0

! I'l给使用statments 变种某些情况下 第一)=先前计算的变量和存储在脚本 OUT =输出 如果VAR> = 10 再出= 4 否则OUT = 2 第二)无使用语句 用户将键入“然后”输入他想要计算的表达式,那将是 Then: Out =(((var)** 2)+(2 * var))**(1/2) 再一次,它只是为了举例说明......我不需要这个特定的布局。 – GabrielBR

+0

那么为什么你需要if-then-else字段呢?据我可以告诉所有复杂的计算器使用一个单一的输入字段。 –

回答

2

下面是你正在尝试做的简单版本。

我们需要使用内置函数eval来评估字符串的数学运算。

我们还应该编写一些错误处理的代码,因为有一个非常好的更改,用户将输入一个错误的公式,并且eval语句将失败。

欲了解更多有关evalexec的信息,请看看这篇文章here。我认为它解释这两个方面做得很好。

这是它会是什么样子:

import tkinter as tk 

root = tk.Tk() 

math_label = tk.Label(root, text = "Type formula and press the Calculate button.") 
math_label.pack() 
math_entry = tk.Entry(root) 
math_entry.pack() 
result_label = tk.Label(root, text = "Results: ") 
result_label.pack(side = "bottom") 

def perform_calc(): 
    global result_label 
    try: 
     result = eval(math_entry.get()) 
     result_label.config(text = "Results: {}".format(result)) 
    except: 
     result_label.config(text = "Bad formula, try again.") 


applybutton = tk.Button(root, text = "Calculate", command = perform_calc) 
applybutton.pack() 

root.mainloop() 
2

第一个答案能够在正确的思想,但它也可以匹配一点更加明确,以你给的例子,如果你想利用这一点更进一步。

基本上你想使用eval语句来测试你的条件,然后使用exec语句来运行你的python代码块。你必须在全局传递()的参数,以确保您的exec函数修改正确的变量在这种情况下

见下文:

import tkinter as tk 
from tkinter import messagebox 

var = 10 
out = 0 

def calculate(): 
    global out 

    try: 
     if eval(IfEntry.get()): 
      exec(thenEntry.get(), globals()) 
     else: 
      exec(elseEntry.get(), globals()) 
     messagebox.showinfo(title="Calculation", message="out: " + str(out)) 
    except: 
     exc_type, exc_value, exc_traceback = sys.exc_info() 
     msg = traceback.format_exception(exc_type, exc_value, exc_traceback) 
     messagebox.showinfo("Bad Entry", message=msg)    

root = tk.Tk() 

Iflabel = tk.Label(root, text = "If...") 
Iflabel.pack() 
IfEntry = tk.Entry(root) 
IfEntry.insert(0, "var >= 10") 
IfEntry.pack() 

thenlabel = tk.Label(root, text = "Then...") 
thenEntry = tk.Entry(root) 
thenlabel.pack() 
thenEntry.insert(0, "out = 4") 
thenEntry.pack() 

elselabel = tk.Label(root, text = "else..") 
elseEntry = tk.Entry(root) 
elselabel.pack() 
elseEntry.insert(0, "out = 2") 
elseEntry.pack() 

applybutton = tk.Button(root, command=calculate, text = "Calculate") 
applybutton.pack() 


applybutton.focus_displayof 

root.mainloop() 
+0

对此很好的工作。我的回答更多地是回应OP的一条评论,但我喜欢你回答更多:) –

+0

谢谢,可能很好,指出应该有一些形式的错误处理来处理这个问题,就像你在例子中一样。 –

+0

雅由于用户错误,输入错误公式的机会很大,所以错误处理在这里是一个好主意。 –