Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

python - Grouping Tkinter Labels

I have a simple code,

            Monday = Label(master, text="Subject")
            Monday.grid(row=1, column=0)
            Monday = Label(master, text="Monday")
            Monday.grid(row=1, column=2)
            Tuesday = Label(master, text="Tuesday")
            Tuesday.grid(row=1, column=3)
            Wednesday = Label(master, text="Wednesday")
            Wednesday.grid(row=1, column=4)
            Thursday = Label(master, text="Thursday")
            Thursday.grid(row=1, column=5)
            Friday = Label(master, text="Friday")
            Friday.grid(row=1, column=6)

enter image description here

Is there a quick way I can select all of these and move all the days into the middle of the window? I understand I could simply work out the column number for each individual day but surely there is an easier way while also keeping them perfectly spaced out like they are now.

question from:https://stackoverflow.com/questions/65878213/grouping-tkinter-labels

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just like Bryan Oakley said, you might do something like this:

import tkinter as tk

master = tk.Tk()

frame = tk.Frame(master)

subject = tk.Label(frame, text="Subject")
monday = tk.Label(frame, text="Monday")
tuesday = tk.Label(frame, text="Tuesday")
wednesday = tk.Label(frame, text="Wednesday")
thursday = tk.Label(frame, text="Thursday")
friday = tk.Label(frame, text="Friday")

subject.grid(row=0, column=0)
monday.grid(row=0, column=1)
tuesday.grid(row=0, column=2)
wednesday.grid(row=0, column=3)
thursday.grid(row=0, column=4)
friday.grid(row=0, column=5)

frame.pack(expand=True)

master.mainloop()

Check this out for more info on why the place or pack method can be used to center things.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...