在 tkinter python 中的输入框旁边打包标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18550710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pack labels right next to entry box in tkinter python
提问by user2633882
I have a window that prompts users to enter the directory of their log files. However, my label seems to pack on top of my entrybox. Any idea on how to pack them side by side?
我有一个窗口,提示用户输入其日志文件的目录。但是,我的标签似乎包装在我的输入框顶部。关于如何并排包装它们的任何想法?
labelText=StringVar()
labelText.set("Enter directory of log files")
labelDir=Label(app,textvariable=labelText,height=4)
labelDir.pack()
directory=StringVar(None)
dirname=Entry(app,textvariable=directory,width=50)
dirname.pack()
采纳答案by user2633882
Yes, you need to set the side
option to "left". See below:
是的,您需要将side
选项设置为“左”。见下文:
from Tkinter import Tk, Label, Entry, StringVar
app = Tk()
labelText=StringVar()
labelText.set("Enter directory of log files")
labelDir=Label(app, textvariable=labelText, height=4)
labelDir.pack(side="left")
directory=StringVar(None)
dirname=Entry(app,textvariable=directory,width=50)
dirname.pack(side="left")
app.mainloop()
example:
例子:
回答by Octopusghost
You could always switch to using '.grid' instead.
您可以随时切换到使用“.grid”。
With your code:
使用您的代码:
from Tkinter import Tk, Label, Entry, StringVar
app = Tk()
labelText=StringVar()
labelText.set("Enter directory of log files")
labelDir=Label(app, textvariable=labelText, height=4)
labelDir.grid(row=1,column=1)
directory=StringVar(None)
dirname=Entry(app,textvariable=directory,width=50)
dirname.grid(row=1,column=2)
app.mainloop()
Code Running: https://gyazo.com/7c78e6f3d7c8fe9233f150072c44a0d1