【问题标题】:Tkinter Label is give me random tabbing of text when using \nTkinter Label 在使用 \n 时给我随机的文本标签
【发布时间】:2021-05-17 04:49:58
【问题描述】:

我正在尝试从 python 的 Tkinter 创建一个标签,最终将存储信息。现在我只是在使用一个占位符,但是当我用 \n 向下输入文本时,它会在文本中跳动。

当前代码中的标签是什么样子的: Tkinter Label without the text correctly aligned

这是用于制作标签的代码:

StoryFrame = LabelFrame(root, width = 1000, heigh = 300, bg="DarkGrey", highlightbackground='black', text='Current News in Technology', font="Helvetica 15 bold")

StoryFrame.place(relx=0.03, rely= 0.65)

info = Label(StoryFrame, anchor=NW, text="Dateline: Date and/or time\n\nNews source: Name\n\nHostname: Domain\n\nURL: Address", width=40, height=17)

info.place(relx=0.685, rely= 0.01)

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    这是因为justify 选项的默认值是“center”。如果要对齐左边的行,在Label()中添加justify="left"

    info = Label(StoryFrame, anchor=NW,
                 text="Dateline: Date and/or time\n\nNews source: Name\n\nHostname: Domain\n\nURL: Address",
                 width=40, height=17, justify="left")
    

    【讨论】:

      【解决方案2】:
      from tkinter import *
      
      root = Tk()
      
      StoryFrame = LabelFrame(root, width = 1000, heigh = 300, bg="DarkGrey", highlightbackground='black', text='Current News in Technology', font="Helvetica 15 bold")
      
      StoryFrame.place(relx=0.03, rely= 0.65)
      
      info = Label(StoryFrame, anchor=NW, text="Dateline: Date and/or time\n\nNews source: Name\n\nHostname: Domain\n\nURL: Address", width=40, height=17, justify='left')
      
      info.place(relx=0.685, rely= 0.01)
      
      root.mainloop()
      
      

      这段代码给出了你想要的输出。如果添加了justify='left' 左对齐。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-15
        • 2014-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-01
        相关资源
        最近更新 更多