如何在Tkinter Python中进行表达式 [英] How to make expressions in Tkinter Python

查看:149
本文介绍了如何在Tkinter Python中进行表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python编程的新手,在使用Tkinter开发GUI的特定部分时遇到一些问题.

我想做的是,用户可以在其中输入(键入)他的数学方程式,然后软件使用先前计算出的变量进行计算.

我已经找到了很多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个输入空间

1st)如果...

然后第二次...

3rd)其他...

因此,用户将输入他的条件表达式,然后软件将完成工作.在我看来,另一件重要的事情是,如果用户将"if"空格留在空白处,那么他只需在"Then ..."条目中键入表达式,然后按下"calculate"按钮或使用语句构建所有表达式./p>

如果有人可以提供有关如何做事的想法....

(如果可能的话,没有课程)

我会举例说明 第一条using语句:

var = the variable previously calculated and stored in the script 

out = output 

if var >= 10 

then out = 4 

else out = 2 

2nd在不使用语句的情况下,用户将键入"Then",然后输入要计算的表达式,即:

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

同样,这只是为了举例说明...我不需要这种特定的布局.如果有人知道如何更好地构建它,欢迎您.

谢谢.

解决方案

第一个答案得到了正确的思考,但也可以更明确地将其与您给出的示例进行匹配,以防万一.再远一点.

基本上,您想使用eval语句来测试您的条件,然后使用exec语句来运行python代码块.您必须传入globals()参数,以确保您的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()

I'm new in python programming and I'm having some issues in developing a specific part of my GUI with Tkinter.

What I'm trying to do is, a space where the user could enter (type) his math equation and the software make the calculation with the variables previously calculated.

I've found a lot of calculators for Tkinter, but none of then is what I'm looking for. And I don't have much experience with classes definitions.

I made this simple layout to explain better what I want to do:

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()

This simple code for Python 3 have 3 Entry spaces

1st) If...

2nd Then...

3rd) Else...

So, the user will enter with his conditional expression and the software will do the job. In my mind, another important thing is if the user left the "if" space in blank, he will just type his expression inside "Then..." Entry and press the button "calculate" or build all expression with the statements.

If someone could give some ideas about how and what to do....

(without classes, if it is possible)

I'l give some situations for exemplification 1st using statements:

var = the variable previously calculated and stored in the script 

out = output 

if var >= 10 

then out = 4 

else out = 2 

2nd Without using statement the user will type in "Then" Entry the expression that he want to calculate and that would be:

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

Again, it's just for exemplification...I don't need this specific layout. If anyone has an idea how to construct it better, is welcome.

Thanks all.

解决方案

The first answer gets at the right thought, but it can also be matched a little more explicitly to the example you gave, in case you want to take this a little further.

Basically you want to use the eval statement to test your conditional, and then use the exec statement to run your python code blocks. You have to pass in the globals() argument in order to make sure your exec functions modify the correct variables in this case

See below:

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()

这篇关于如何在Tkinter Python中进行表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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