簡體   English   中英

無論窗口大小如何,如何在保持整個框架着色的同時保持框架中的元素向右對齊?

[英]How do I keep elements in a frame justified to the right while keeping the entire frame coloured, regardless of the size of the window?

我是 tkinter 的新手,所以我在網格布局方面有點迷失。 我想要做的是在窗口的右下角放置一個徽標,並且無論窗口有多大,始終處於該位置。 我已經設法定位徽標沒有問題,但是當我向右對齊時,元素左側的框架變為白色。 我如何將這部分保持為黑色,因為它在徽標下方? 左對齊用黑色填充整個框架,但右對齊僅從徽標/文本開始填充。

這就是我得到的

這是我當前的代碼:

from tkinter import *

root = Tk()
# GUI attributes
root.title('Lantern')
root.geometry('800x600')
root.iconbitmap('iconrl.ico')

# main containers
topFrame = Frame(root, bg='#000000', width=800, height=100, pady=3)
center = Frame(root, bg='#181818', padx=3, pady=3)
btmFrame = Frame(root, bg='#000000', width=800, height=90, padx=10)

# layout all of the main containers
root.grid_rowconfigure(1, weight=1)
root.grid_columnconfigure(0, weight=1)

topFrame.grid(row=0, sticky='ew')
center.grid(row=1, sticky='nsew')
btmFrame.grid(row=2, sticky='e')

# topFrame Widgets
rlLabel = Label(topFrame, text='Lantern ', font=('Verdana', 12), fg='red', bg='#000000', width=10)

# topFrame Layout
rlLabel.grid(row=0, columnspan=3)

# center Widgets
# center Layout
# btmFrame Widgets

powered = Label(btmFrame, text='Powered by: ', font=('Verdana', 12), fg='#FFF204', bg='#000000', width=15)
sLogo = PhotoImage(file='slogo.png')
sLogoLabel = Label(btmFrame, image=sLogo, bg='#000000')

# btmFrame Layout

powered.grid(row=0, column=1, sticky='e')
sLogoLabel.grid(row=0, column=2, sticky='e')


root.mainloop()

首先,您需要將btmFrame.grid(row=2, sticky='e')更改為btmFrame.grid(row=2, sticky='ew') ,以便框架水平填充所有空間。

然后添加btmFrame.columnconfigure(0, weight=1)poweredsLogoLabel推到框架的右側。

或者您可以在poweredsLogoLabel上使用pack()

sLogoLabel.pack(side='right')
powered.pack(side='right')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM