[英]How can I make two widgets on the same column have different widths in Tkinter?
I'm trying to find a way to format this tkinter GUI so that I can have widgets in the same column that are different widths.我正在尝试找到一种方法来格式化此 tkinter GUI,以便我可以在同一列中拥有不同宽度的小部件。 I want to do this so that each label doesn't have an unnecessary amount of white space on either side and can be directly next to its entry box.
我想这样做,以便每个 label 的两侧都没有不必要的空白,并且可以直接位于其输入框旁边。 I've tried shrinking the column, but that obscures the prompt.
我试过缩小列,但这会掩盖提示。 There might also be another way that I'm not aware of that would format this in a visually appealing way.
可能还有另一种我不知道的方式会以视觉上吸引人的方式格式化。 I am open to any suggestions.
我愿意接受任何建议。
This is what it currently looks like.这就是它目前的样子。
import tkinter as tk
responses = ['running', 'reading', 'programming', 'fishing']
prompt_2 = "At what time do you want to do each activity?"
root = tk.Tk()
root.geometry("335x200")
root.title("Schedule Maker")
prompt = tk.Label(
root,
text=prompt_2
)
button = tk.Button(
root,
text="ENTER",
width=10,
height=2
)
button.grid(row=len(responses)+1, column=0)
prompt.grid(row=0, column=0)
prompt['text'] = prompt_2
label0_list = []
label1_list = []
entry0_list = []
entry1_list = []
for w in range(len(responses)):
label0 = tk.Label(root, text=responses[w] + ":")
label0.grid(row=w+1, column=0)
label0_list.append(label0)
label1 = tk.Label(root, text='to')
label1.grid(row=w+1, column=2)
label1_list.append(label1)
entry0 = tk.Entry(root, width=6)
entry0.grid(row=w+1, column=1)
entry0_list.append(entry0)
entry1 = tk.Entry(root, width=6)
entry1.grid(row=w+1, column=3)
entry1_list.append(entry1)
root.mainloop()
Think of tk.Tk()
as a rectangle of space where you can put in your content.将
tk.Tk()
视为可以放入内容的矩形空间。 The content is managed by the geometry manager of your choice, like pack
or grid
.内容由您选择的几何管理器管理,例如
pack
或grid
。 Sometimes people call these rectangle of space container.有时人们称这些矩形空间容器。
Frames are used to have a seperated area where you can organize the content, with different settings.框架用于有一个单独的区域,您可以在其中组织内容,具有不同的设置。 So frame are containers in your window.
所以框架是 window 中的容器。 You could use
pack
in your root and use grid
in a frame, without getting any error.您可以在根目录中使用
pack
并在框架中使用grid
,而不会出现任何错误。
So what I usally do is to imagin the root as filled area of space and I seperate it in my head to some sort of chunks where I place my frames.所以我通常做的是将根想象成空间的填充区域,然后在我的脑海中将它分成一些我放置框架的块。 Like this:
像这样:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
header = tk.Frame(root)
body = tk.Frame(root)
bottom_line = tk.Frame(root)
header.pack(side='top')
body.pack()
bottom_line.pack(side='bottom')
After this I fill the containers/frames with the content it belongs to like:在此之后,我用它所属的内容填充容器/框架,例如:
#header content
headline = tk.Label(header, text='At what time do you want to do each?')
headline.pack(fill='x')
#body content
first_activity = tk.Label(body,text='running:')
first_from = tk.Entry(body)
first_sep = tk.Label(body, text='to')
first_to = tk.Entry(body)
first_activity.pack(side='left')
first_from.pack(side='left')
first_sep.pack(side='left')
first_to.pack(side='left')
#bottom content
my_button = tk.Button(bottom_line, text='Enter')
my_button.pack(fill='x')
For more details [click]更多详情【点击】
PS: If you really want to use grid and your set of envoirment, you could also use prompt.grid(row=0, column=0,columnspan=4)
. PS:如果你真的想使用网格和你的一组环境,你也可以使用
prompt.grid(row=0, column=0,columnspan=4)
。 But it looks a little bit different.但它看起来有点不同。
You can use sticky="e"
on label0.grid(...)
:您可以在
label0.grid(...)
上使用sticky="e"
:
label0.grid(row=w+1, column=0, sticky="e")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.