在 Tkinter 中将方程表示为画布对象 [英] Representing an equation as a canvas object in Tkinter

查看:27
本文介绍了在 Tkinter 中将方程表示为画布对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序来处理在 Tkinter 中的画布对象上呈现的符号方程.程序需要能够绘制方程,然后当用户点击方程时,我需要知道他们点击的是哪个符号.

I'm trying to make a program which deals with symbolic equations rendered on a canvas object in Tkinter. The program needs to be able to draw an equation, and then when users clicks on the equation, I need to know which symbol they clicked on.

我考虑了几种方法:

我可以手动编写一个数学渲染程序,它告诉 Canvas 对象要在那里放置哪些符号,然后它们只是文本对象,我可以以正常方式检测到其单击事件.然而,这似乎是很多多余的努力,我无法让它变得非常漂亮.

I can manually write a math rendering program which tells the Canvas object what symbols to put there, and then they're just text objects whose click events I can detect the normal way. However, this seems like a lot of redundant effort, and I won't be able to make it very pretty.

我可以使用库将方程渲染为图像,然后编写自己的代码来确定不同符号的大致位置,然后检测点击并通过点击位置确定用户点击的符号.这是 hacky 并且听起来不可靠.另外这意味着我需要编写代码来检测符号的位置.

I could render the equation as an image using a library, and then write my own code to figure out approximately where the different symbols would be, then detect clicks and decide what symbol the user clicked by the position of the click. This is hacky and sounds unreliable. Plus it means I need to write the code to detect where the symbols are.

还有其他想法吗?

推荐答案

从字符串中获取方程可能是最好的主意.

It is probably the best idea to get the equation from a string.

这可能不是最好的方法,但它仍然是一个有效的选择,您可以按原样获取字符串,然后使用 eval() 函数.

This may not be the best way to do it, but it's still a valid option, you can get the string as it is and then replace the x value with what what ever x is using the eval() function.

equation = "2^x"

for x in range(length of graph):
    x2 = x-1 #to draw a line from this point to the previous one. 
    canvas.create_line(x, eval(equation), x2, eval(equation.replace('x', "x2"))

这篇关于在 Tkinter 中将方程表示为画布对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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