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. 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. 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.
The content is managed by the geometry manager of your choice, like pack
or 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. You could use pack
in your root and use grid
in a frame, without getting any error.
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)
. But it looks a little bit different.