2010-10-26 167 views
0

我有一段幫助信息,我想在窗口底部顯示一個「ok」按鈕。我的問題是格式化...我希望能夠簡單地將段落設置爲一個變量,然後將該變量發送到消息框小部件。默認情況下,它以橫向和非常難看的方式進行格式化。有什麼建議?Python Tkinter幫助菜單

def aboutF(): 
    win = Toplevel() 
    win.title("About") 
    about = "Top/bottom 3 - Reports only the top/bottom 3 rows for a param you will  later specify.\ 
     Set noise threshold - Filters results with deltas below the specified noise threshold in ps.\ 
     Sort output - Sorts by test,pre,post,unit,delta,abs(delta).\ 
     Top 2 IDD2P/IDD6 registers - Reports only the top 2 IDD2P/IDD6 registers.\ 
     Only critical registers - Reports only critical registers.\ 
     Use tilda output format - Converts the output file from csv to tilda.\ 
     Use html output format - Converts the output file from csv to html." 
    Label(win, text=about, width=100, height=10).pack() 
    Button(win, text='OK', command=win.destroy).pack() 
+1

我們能看到你的代碼?這可能與您爲段落定義字符串的方式有關,而不是Tkinter本身。 – 2010-10-26 23:07:34

+0

我使用代碼編輯了原始帖子。感謝您的建議。 – thenickname 2010-10-27 16:51:44

回答

1

使用自動換行的文本部件,並且或者更準確地定義你的字符串或做一些後處理以去除所有多餘的空白。從this answer使用代碼可以很容易地使用多種顏色,字體等

例如:

import Tkinter as tk 
import re 

class CustomText(tk.Text): 
    '''A text widget with a new method, HighlightPattern 

    example: 

    text = CustomText() 
    text.tag_configure("red",foreground="#ff0000") 
    text.HighlightPattern("this should be red", "red") 

    The HighlightPattern method is a simplified python 
    version of the tcl code at http://wiki.tcl.tk/3246 
    ''' 
    def __init__(self, *args, **kwargs): 
     tk.Text.__init__(self, *args, **kwargs) 

    def HighlightPattern(self, pattern, tag, start="1.0", end="end", regexp=True): 
     '''Apply the given tag to all text that matches the given pattern''' 

     start = self.index(start) 
     end = self.index(end) 
     self.mark_set("matchStart",start) 
     self.mark_set("matchEnd",end) 
     self.mark_set("searchLimit", end) 

     count = tk.IntVar() 
     while True: 
      index = self.search(pattern, "matchEnd","searchLimit",count=count, regexp=regexp) 
      if index == "": break 
      self.mark_set("matchStart", index) 
      self.mark_set("matchEnd", "%s+%sc" % (index,count.get())) 
      self.tag_add(tag, "matchStart","matchEnd") 

def aboutF(): 
    win = tk.Toplevel() 
    win.title("About") 
    about = '''Top/bottom 3 - Reports only the top/bottom 3 rows for a param you will later specify. 
     Set noise threshold - Filters results with deltas below the specified noise threshold in ps. 
     Sort output - Sorts by test,pre,post,unit,delta,abs(delta). 
     Top 2 IDD2P/IDD6 registers - Reports only the top 2 IDD2P/IDD6 registers. 
     Only critical registers - Reports only critical registers. 
     Use tilda output format - Converts the output file from csv to tilda. 
     Use html output format - Converts the output file from csv to html.''' 
    about = re.sub("\n\s*", "\n", about) # remove leading whitespace from each line 
    t=CustomText(win, wrap="word", width=100, height=10, borderwidth=0) 
    t.tag_configure("blue", foreground="blue") 
    t.pack(sid="top",fill="both",expand=True) 
    t.insert("1.0", about) 
    t.HighlightPattern("^.*? - ", "blue") 
    tk.Button(win, text='OK', command=win.destroy).pack() 

root=tk.Tk() 
aboutF() 
root.mainloop()