简体   繁体   English

Python Tkinter帮助菜单

[英]Python Tkinter Help Menu

I have a paragraph of help information that I would like to display in a window with an "ok" button at the bottom. 我有一段帮助信息,我想在窗口的底部显示“确定”按钮。 My issue is formatting...I want to be able to simply set the paragraph equal to a variable and then send that variable to a message box widget. 我的问题是格式设置...我希望能够简单地将段落设置为等于变量,然后将该变量发送到消息框小部件。 By default, it formats laterally and in a very ugly manner. 默认情况下,它以非常难看的方式横向格式化。 Any advice? 有什么建议吗?

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()

Use a text widget with word wrapping, and either define your string more accurately or do a little post-processing to remove all that extra whitespace. 使用带有自动换行的文本小部件,或者更精确地定义您的字符串,或者进行一些后期处理以删除所有多余的空格。 Using the code from this answer makes it easy to use multiple colors, fonts, etc. 使用此答案中的代码可以轻松使用多种颜色,字体等。

For example: 例如:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM