博客园  :: 首页  :: 管理

关于python中tkinter模块-grid布局的对齐方式问题及设定

Posted on 2025-07-11 15:51  520_1351  阅读(60)  评论(0)    收藏  举报

先看一段代码,部分核心的如下:

root = tkinter.Tk()
root.title("Multi_Vars_Name_To_Strings - Author:qq-5201351")
root.geometry('455x310')

Label_01=tkinter.Label(root,text="Please fill the Multi Vars Name Such as --> a,b,c")
Label_01.grid(row=0,column=0,padx=10,pady=4)

Text_01=tkinter.Text(root,width=60,height=6)
Text_01.grid(row=1,column=0,padx=10,pady=2)

Text_02=tkinter.Text(root,width=60,height=6)
Text_02.grid(row=2,column=0,padx=10,pady=2)

运行后的结果,如下图所示:

可以看到 Label 与 Text 组件没有对齐,如果有时在对Label中加入font=(8) 之类的,对齐的距离更不可控

解决方法,可以对 Label 标签加入 sticky="w" 属性,其中w代表西,对应左对齐

root = tkinter.Tk()
root.title("Multi_Vars_Name_To_Strings - Author:qq-5201351")
root.geometry('455x310')

Label_01=tkinter.Label(root,text="Please fill the Multi Vars Name Such as --> a,b,c",font=(8))
Label_01.grid(row=0,column=0,padx=10,pady=4,sticky="w")

Text_01=tkinter.Text(root,width=60,height=6)
Text_01.grid(row=1,column=0,padx=10,pady=2)

Text_02=tkinter.Text(root,width=60,height=6)
Text_02.grid(row=2,column=0,padx=10,pady=2)

这样再看Label 与 Text 组件就左对齐了,当然对于 Text 组件 ,也可以都加上 sticky="w" 对齐的

 

 

 

尊重别人的劳动成果 转载请务必注明出处:https://www.cnblogs.com/5201351/p/18978960