简体   繁体   English

Python Tkinter文本对齐

[英]Python Tkinter Text Alignment

I am using Tkinter with Python2 to create a new window by clicking on the button. 我将Tkinter与Python2结合使用,通过单击按钮来创建一个新窗口。 In this new window I want to Display text. 在这个新窗口中,我要显示文本。 Now I have Troubles with the alignment, is it possible to align it to the left? 现在我有麻烦的对齐方式,是否可以将其向左对齐? It is always centered and neither anchor=LEFT nor sticky="NSEW" help. 它始终处于居中状态,anchor = LEFT或sticky =“ NSEW”都没有帮助。

import tkinter as tki

btn3 = tki.Button(self.root, text="HELP", command=self.help, fg="black", bg="white", font=("Courier",22))
btn3.grid(row=1, column=2, padx=10, pady=10, sticky="NSEW" )

def help(self):
    self.text = """ Hello. 
                    You can find help here to the following subjects:
                        - getting started
                        - installation
                        - FAQ."""  
    self.top = tki.Toplevel()
    self.top.title("Help")
    self.label1 = tki.Label(self.top, text = self.text, height = 0, width=80, fg="black", bg="white", font=("Courier",18))
    self.label1.pack()

When you use anchor = n you need to put anchor = "n" ie with the n in quotes. 当使用anchor = n您需要将anchor = "n"放在引号中。 You're getting the global name is not defined error because of this. 因此,您正在获取全局名称未定义错误。 Alternatively, use anchor = tki.N . 或者,使用anchor = tki.N Because you used import tkinter as tki you must prefix tkinter variables with tki. 因为您将import tkinter as tki ,所以必须在tki.变量前加上tki.前缀tki. in order for python to know what you're referring to. 为了让python知道您指的是什么。 However, I think you might want to try justify=tki.LEFT also if that doesn't work. 但是,我认为您可能还想尝试justify=tki.LEFT如果那不起作用。

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

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