简体   繁体   English

如何将两个标签放在一起?

[英]How do I put two label together?

I am doing a UI and using tkinter to establish it. 我正在做一个UI,并使用tkinter建立它。 As seen in the picture below I want the two labels above equally. 如下图所示,我希望上面的两个标签均等。

图片

This is the code that I have written for creating this. 这是我为创建此代码而编写的代码。

Player1 = "Terry"  
Player2 = "David"
player1 = tk.Label(top,
                   text=Player1,
                   font="Times 45",
                   fg="white",
                   bg="#0000FF")
player1.pack(side=TOP, padx=10,pady=10,  anchor=NW)
player2 = tk.Label(top,
                   text=Player2,
                   font="Times 45",
                   fg="white",
                   bg="#FF0000")
player2.pack(side=TOP, padx=10,pady=10,anchor=N)

I tried to adjust it using anchor and side but form reason I cant make the other label go up. 我尝试使用锚点和侧面进行调整,但是由于某种原因,我无法使其他标签升起。

grid is an alternative to pack that might be easier in this case. 在这种情况下, gridpack的替代方法,可能会更容易。 Essentially, your interface is divided into a grid of rows and columns, and locations of objects are specified by their row and column location. 本质上,您的界面分为行和列的网格,对象的位置由其行和列的位置指定。 Your code might look like this: 您的代码可能如下所示:

player1.grid(row=0, column=0)
player2.grid(row=0, column=1)

If you'd rather stick with pack , you just need to specify side as 'left' / 'right' : 如果您想坚持使用pack ,则只需将side指定为'left' / 'right'

player1.pack(side='left')
player2.pack(side='left`)

在此处输入图片说明

If you are expecting the output as below, then here is your code. 如果期望输出如下,那么这是您的代码。 在此处输入图片说明

Player1 = "Terrry"  
Player2 = "David"
player1 = tk.Label(top,
                   text=Player1,
                   font="Times 45",
                   fg="white",
                   bg="#0000FF")
player1.pack(side=tk.LEFT, padx=10,pady=10, anchor=tk.NW)
player2 = tk.Label(top,
                   text=Player2,
                   font="Times 45",
                   fg="white",
                   bg="#FF0000")
player2.pack(side=tk.LEFT, padx=10,pady=10, anchor=tk.N)

Instead of passing 'TOP' to the 'side' attribute you could pass 'LEFT' to achieve this. 可以将“ LEFT”传递给“ side”属性,而不是将“ TOP”传递给“ side”属性。 Also, you could either use 'NW' or 'N' to the 'anchor' attribute. 同样,您可以对'anchor'属性使用'NW'或'N'。

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

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