Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python tkinter在标签小部件上创建项目符号列表_Python_Tkinter - Fatal编程技术网

使用python tkinter在标签小部件上创建项目符号列表

使用python tkinter在标签小部件上创建项目符号列表,python,tkinter,Python,Tkinter,有没有办法使用tkinter标签小部件创建悬挂缩进列表?注:使用propper标准项目符号不是*或- 您可以使用unicode代码点作为粗略的实现: try: import tkinter as tk except ImportError: import Tkinter as tk class BulletLabel(tk.Label): def __init__(self, master, *args, **kwargs): text = kwargs

有没有办法使用tkinter标签小部件创建悬挂缩进列表?注:使用propper标准项目符号不是*或-

您可以使用unicode代码点作为粗略的实现:

try:
    import tkinter as tk
except ImportError:
    import Tkinter as tk

class BulletLabel(tk.Label):
    def __init__(self, master, *args, **kwargs):
        text = kwargs.pop('text', '')
        kwargs['text'] = self.bulletise(text)
        tk.Label.__init__(self, master, *args, **kwargs)

    def bulletise(self, text):
        if len(text) == 0: # no text so no bullets
            return ''
        lines = text.split('\n')
        parts = []
        for line in lines: # for each line
            parts.extend(['\u2022', line, '\n']) # prepend bullet and re append newline removed by split
        return ''.join(parts)

    def configure(self, *args, **kwargs):
        text = kwargs.pop('text', '')
        if text != '':
            kwargs['text'] = self.bulletise(text)
        tk.Label.configure(*args, **kwargs)


root = tk.Tk()

blabel = BulletLabel(root, text='one\ntwo\nthree')
blabel.pack()

root.mainloop()

您可以使用unicode代码点作为粗略实现:

try:
    import tkinter as tk
except ImportError:
    import Tkinter as tk

class BulletLabel(tk.Label):
    def __init__(self, master, *args, **kwargs):
        text = kwargs.pop('text', '')
        kwargs['text'] = self.bulletise(text)
        tk.Label.__init__(self, master, *args, **kwargs)

    def bulletise(self, text):
        if len(text) == 0: # no text so no bullets
            return ''
        lines = text.split('\n')
        parts = []
        for line in lines: # for each line
            parts.extend(['\u2022', line, '\n']) # prepend bullet and re append newline removed by split
        return ''.join(parts)

    def configure(self, *args, **kwargs):
        text = kwargs.pop('text', '')
        if text != '':
            kwargs['text'] = self.bulletise(text)
        tk.Label.configure(*args, **kwargs)


root = tk.Tk()

blabel = BulletLabel(root, text='one\ntwo\nthree')
blabel.pack()

root.mainloop()

最好使用消息小部件,这是一个标签小部件,设计用于显示多行文本。对于项目符号,可以使用unicode字符串。例如:

import tkinter as tk

root = tk.Tk()

point = '\u2022'
msg = tk.Message(root, text='Hello\n%s World.' % point)
msg.pack()

最好使用消息小部件,这是一个标签小部件,设计用于显示多行文本。对于项目符号,可以使用unicode字符串。例如:

import tkinter as tk

root = tk.Tk()

point = '\u2022'
msg = tk.Message(root, text='Hello\n%s World.' % point)
msg.pack()

我制作了一个课程,可以满足您的需求:

class BLabel(object):
    b = "•"
    def __init__(self,master):
        import tkinter as tk
        self.l = tk.Label(master)
    def add_option(self,text):
        if self.l.cget("text") == "":
            self.l.config(text=self.b+" "+text)
        else:
            self.l.config(text=self.l.cget("text") +"\n"+ self.b + " "+text)
您可以像这样使用它:

lbal = BLabel(master=master)    
lbal.add_option("Bullet1")    #<-- adding item
lbal.add_option("Bullet2")    #<-- adding item
lbal.l.pack()     #<-- Packing
以下是上述代码的输出:

这样,您就可以使用pack、place或grid。例如:

网格:

地点:

包装:


我制作了一个课程,可以满足您的需求:

class BLabel(object):
    b = "•"
    def __init__(self,master):
        import tkinter as tk
        self.l = tk.Label(master)
    def add_option(self,text):
        if self.l.cget("text") == "":
            self.l.config(text=self.b+" "+text)
        else:
            self.l.config(text=self.l.cget("text") +"\n"+ self.b + " "+text)
您可以像这样使用它:

lbal = BLabel(master=master)    
lbal.add_option("Bullet1")    #<-- adding item
lbal.add_option("Bullet2")    #<-- adding item
lbal.l.pack()     #<-- Packing
以下是上述代码的输出:

这样,您就可以使用pack、place或grid。例如:

网格:

地点:

包装:


看一看。它使用一个文本小部件而不是标签,以获得更大的灵活性。我建议使用u2022作为要点,而不是u00B7。如果您不想允许编辑,请添加txt.configurestate=DISABLED。@fhdrsdg太好了,谢谢,我不知道您可以禁用编辑。请查看。它使用一个文本小部件而不是标签,以获得更大的灵活性。我建议使用u2022作为要点,而不是u00B7。如果您不想允许编辑,请添加txt.configurestate=DISABLED。@fhdrsdg太好了,谢谢,我不知道您可以禁用编辑。