在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:01:39  来源:igfitidea点击:

Pack labels right next to entry box in tkinter python

pythontkinterlabelpacktkinter-entry

提问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 sideoption 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:

例子:

sample

样本

回答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

代码运行:https: //gyazo.com/7c78e6f3d7c8fe9233f150072c44a0d1