I am doing a UI and using tkinter to establish it. 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. 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'
:
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. Also, you could either use 'NW' or 'N' to the 'anchor' attribute.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.