4

I've been developing a Tkinter app and at some label i need to put formula that should look like a rational number(expression1/expression2, like a numerator and denominator and a bar between them). I did some digging and couldnt find anything related to it. Any suggestions on how this can be done ?

I even couldnt find anything on printing a fraction in a rational number format on the console. I oonly care about the looks and no calculation will be made with it, its just a label

Firat.Berk.Cakar
  • 185
  • 1
  • 12
  • 1
    Low tech: the numerator in one label, the denominator in another, and a thin solid rectangle between the labels. Tweaking fonts and alignment could get it to look nice. Hi tech: find out how to render LaTex or some other math markup in Tkinter. – John Coleman Sep 08 '16 at 01:51
  • @JohnColeman if you post your comment as an answer i will accept it. I just played around with labels, took some time but worked pretty well. – Firat.Berk.Cakar Sep 13 '16 at 12:57

3 Answers3

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

\033[4m enables underline

\033[0m resets it

It will basically display something that looks like (with an underline under the 3):

3
2
jrobichaud
  • 1,272
  • 1
  • 11
  • 23
1

A high-tech solution would be to use matplotlib in Tkinter. It provides a scaled-down version of Latex which it calls mathtext. This route doesn't look easy but seems to be possible. See this question for details. If this way seems too involved, a similar but perhaps easier idea would be to use MathML (officially part of HTML5) for the markup. It might be easier to embed in Tkinter.

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

Community
  • 1
  • 1
John Coleman
  • 51,337
  • 7
  • 54
  • 119
0
print '%s'%Fraction(1.5)
>>> '3/2'

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

jrobichaud
  • 1,272
  • 1
  • 11
  • 23
  • http://blogs.diariodepernambuco.com.br/espacodaprevidencia/wp-content/uploads/2013/04/img-thing.jpg This the format i am trying to display, rather than plain one line 3/2 – Firat.Berk.Cakar Sep 08 '16 at 01:40