如何解决无论窗口的大小如何,如何使元素在框架中向右对齐,同时使整个框架着色?
我是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,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',width=10)
# topFrame Layout
rlLabel.grid(row=0,columnspan=3)
# center Widgets
# center Layout
# btmFrame Widgets
powered = Label(btmFrame,text='Powered by: ',fg='#FFF204',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)
将powered
和sLogoLabel
推到框架的右侧。
或者您可以在pack()
和powered
上使用sLogoLabel
:
sLogoLabel.pack(side='right')
powered.pack(side='right')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。