How to print / show rational number expression in python

I am developing a Tkinter application, and on some shortcut I need to put a formula that should look like a rational number (expression1 / expression2, as the numerator and denominator, and the strip between them). I did some digging and could not find anything related to it. Any suggestions on how to do this?

I couldn’t even find anything by printing a fragment in rational number format on the console. I generally care about the appearance, and there will be no calculations with him, it's just a shortcut

+4
source share
3 answers

A high-tech solution would be to use matplotlib in Tkinter. It provides a smaller version Latexthat it calls mathtext . This route does not look easy, but it seems possible. See this for more details . If this method seems too involved, a similar, but perhaps simpler idea is to use MathML (officially part of HTML5) for markup. Tkinter may be easier to embed.

A relatively low-tech approach would be to use two labels (with invisible borders) for the numerator and denominator, respectively, with a thin rectangle between them. This will require some tweaking, but you should be able to tweak the fonts and alignment so that the result looks like a single expression.

+1
print('\n\033[4m'+'3' + '\033[0m'+'\n2')

\033[4m

\033[0m

, ( 3):

3
2
+2
print '%s'%Fraction(1.5)
>>> '3/2'

: https://pymotw.com/2/fractions/

0
source

Source: https://habr.com/ru/post/1653957/


All Articles