2016-03-23 99 views
0

私はPython Tkinterでguiを作っています。今私は結果ラベルで多項式関数を表示する必要があり、それは私が立ち往生するところです。多項式を示すために、私は上付き文字として力を書きます。今私は、テキストボックスなどの上付き文字を書くための正のオフセット値、Tkinterのラベルに上付き文字を使用する

root=Tk() 
l=Text(root) 
l.tag_configure("s", offset=5) 
l.insert(INSERT,"X","","2","s") 
l.grid(row=0) 
root.mainloop() 

を使用することができますしかし、私はn次多項式のためにそれを行う方法を見つけ出すことはできません。私はリストに係数を持っており、多項式をLabel/Textウィジェットに挿入したいと思っています。それを行う簡単な方法はありますか? また、私はmatplotlibのグラフウィンドウで多項式を表示するためにmatplotlibのラテックステキストレンダリングを使用することができますが、ここではそうしたくありません。

+0

タイトルには「ラベル」と書かれていますが、コードではテキストウィジェットが使用されています。これらの2つのウィジェットには、大きく異なる機能があります。あなたは本当に "ラベル"を意味しますか、または "テキスト"を使用した回答は動作しますか? –

+0

はい、私はラベルを使用してそれを行う方法を探していましたが、私が上付き文字の近くに行った唯一の事はテキストボックスを使用していたので、私は自分のコードを投稿したのです。そして今、私はラベルやテキストを使うことができます。 – Eular

+0

上付き文字をサポートするフォントを使用する必要があります。 http://tackoverflow.com/questions/10098100/problems-with-superscript-using-python-tkinter-canvasこのリンクでコードを実行しなかったので、どれくらいうまく動作するか分かりません。 –

答えて

0
import Tkinter as tk 
coeff = [-5,4,-3,2,1] 

root=tk.Tk() 
l=tk.Text(root) 
l.tag_configure("s", offset=5) 

#insert first coefficient manually 
l.insert("insert", str(coeff[0])) 
l.insert("insert","x","",str(len(coeff)-1),"s") 

#use enumerate to track indexes 
for idx, item in enumerate(coeff[1:],2): 
    #put a plus if number is positive. If not minus sign will come from number itself 
    if item >= 0: 
     l.insert("insert","+","") #insert sign 
    l.insert("insert", str(item)) #insert coefficient 
    #insert variable if not last item.  
    if item != coeff[-1]: 
     l.insert("insert","x","",str(len(coeff)-idx),"s") #insert variable and its power 

l.grid(row=0) 
root.mainloop() 

係数のサイズを変更する場合は、tag_add and tag_configureを使用できます。

関連する問題