我试图在中间的窗口顶部放置一个大标签,然后在它下方均匀分布3个选项菜单。我无法让菜单跨越整个窗口。我不确定我做错了什么
root.geometry("800x600")
在底部。一切都是均匀分布的,但它们都被推到窗户的左侧,而不是填满整个东西。我还没有进入下一部分,我将绑定函数,在每个菜单下面显示文本段落,这就是为什么我希望窗口如此之大。
这是我的代码:
from tkinter import *
from tkinter import messagebox
root=Tk()
topFrame=Frame(root)
bottomFrame=Frame(root)
#The first label
lbl=Label(root,text="Pick a Decade",bg="turquoise",fg="hot pink",font= ("Times","40","bold italic"))
lbl.grid(row=1,column=1)
#Functions
def fifties(s):
if s=="Intro":
lbl=Label(root,text="1950's intro",bg="turquoise")
lbl.grid(column=1)
if s=="Political":
lbl=Label(root,text="1950's politcal",bg="turquoise")
lbl.grid(column=1)
if s=="Economic":
lbl=Label(root,text="1950's economic",bg="turquoise")
lbl.grid(column=1)
if s=="Social":
lbl=Label(root,text="1950's social",bg="turquoise")
lbl.grid(column=1)
if s=="Technological":
lbl=Label(root,text="1950's technological",bg="turquoise")
lbl.grid(column=1)
if s=="Aesthetic":
lbl=Label(root,text="1950's aesthetic",bg="turquoise")
lbl.grid(column=1)
def sixties(s):
if s=="Intro":
lbl=Label(root,text="1960's intro")
lbl.grid(column=1,row=3)
if s=="Political":
lbl=Label(root,text="1960's politcal")
lbl.grid(column=1,row=3)
if s=="Economic":
lbl=Label(root,text="1960's economic")
lbl.grid(column=1,row=3)
if s=="Social":
lbl=Label(root,text="1960's social")
lbl.grid(column=1,row=3)
if s=="Technological":
lbl=Label(root,text="1960's technological")
lbl.grid(column=1,row=3)
if s=="Aesthetic":
lbl=Label(root,text="1960's aesthetic")
lbl.grid(column=1,row=3)
def seventies(s):
if s=="Intro":
lbl=Label(root,text="1970's intro")
lbl.grid(column=2,row=3)
if s=="Political":
lbl=Label(root,text="1970's political")
lbl.grid(column=2,row=3)
if s=="Economic":
lbl=Label(root,text="1970's economic")
lbl.grid(column=2,row=3)
if s=="Social":
lbl=Label(root,text="1970's social")
lbl.grid(column=2,row=3)
if s=="Technological":
lbl=Label(root,text="1970's technological")
lbl.grid(column=2,row=3)
if s=="Aesthetic":
lbl=Label(root,text="1970's aesthetic")
lbl.grid(column=2,row=3)
#Menus
v=StringVar(root)
v.set("1950's")
a=OptionMenu(root,v,"Intro","Political","Economic","Social","Technological","Aesthetic",command=fifties)
a.grid(column=0,row=2)
v=StringVar(root)
v.set("1960's")
a=OptionMenu(root,v,"Intro","Political","Economic","Social","Technological","Aesthetic",command=sixties)
a.grid(column=1,row=2)
v=StringVar(root)
v.set("1970's")
a=OptionMenu(root,v,"Intro","Political","Economic","Social","Technological","Aesthetic",command=seventies)
a.grid(column=2,row=2)
#Root
root.configure(background="turquoise")
root.geometry("800x600")
root.mainloop()
在" 1950' social"等等将成为大段落。
答案 0 :(得分:0)
由于网格中的默认单元格没有大小。您只能使用
设置最小尺寸root.columnconfigure(0, minsize=300)
root.columnconfigure(1, minsize=300)
root.columnconfigure(2, minsize=300)
要创建大标题,您可以连接3个单元格
lbl.grid(..., columnspan=3)
您还可以使用sticky='we'
网格(...,粘性='我们')
w
= west/left
,e
= east/right
工作示例:
import tkinter as tk
# --- functions ---
def fifties(selection):
data = {
"Intro": "1950's intro",
"Political": "1950's politcal",
"Economic": "1950's economic",
"Social": "1950's social",
"Technological": "1950's technological",
"Aesthetic": "1950's aesthetic",
}
if selection in data:
text_1950['text'] = data[selection]
else:
text_1950['text'] = "Unknow selection: " + selection
def sixties(selection):
data = {
"Intro": "1960's intro",
"Political": "1960's politcal",
"Economic": "1960's economic",
"Social": "1960's social",
"Technological": "1960's technological",
"Aesthetic": "1960's aesthetic",
}
if selection in data:
text_1960['text'] = data[selection]
else:
text_1960['text'] = "Unknow selection: " + selection
def seventies(selection):
data = {
"Intro": "1970's intro",
"Political": "1970's politcal",
"Economic": "1970's economic",
"Social": "1970's social",
"Technological": "1970's technological",
"Aesthetic": "1970's aesthetic",
}
if selection in data:
text_1970['text'] = data[selection]
else:
text_1970['text'] = "Unknow selection: " + selection
# --- main ---
# - init -
root = tk.Tk()
root.configure(bg="turquoise")
root.geometry("900x600")
# - set columns minimal size -
root.columnconfigure(0, minsize=300)
root.columnconfigure(1, minsize=300)
root.columnconfigure(2, minsize=300)
# - header -
lbl = tk.Label(root, text="Pick a Decade", bg="turquoise", fg="hot pink", font=("Times", 40, "bold italic"))
lbl.grid(column=0, row=0, columnspan=3, sticky='we')
# - menus -
options = ["Intro", "Political", "Economic", "Social", "Technological", "Aesthetic"]
var = tk.StringVar(value="1950's")
a = tk.OptionMenu(root, var, *options, command=fifties)
a.grid(column=0, row=2, sticky='we')
var = tk.StringVar(value="1960's")
a = tk.OptionMenu(root, var, *options, command=sixties)
a.grid(column=1, row=2, sticky='we')
var = tk.StringVar(value="1970's")
a = tk.OptionMenu(root, var, *options, command=seventies)
a.grid(column=2, row=2, sticky='we')
# - empty labels for text -
text_1950 = tk.Label(root, bg="turquoise")
text_1950.grid(column=0, row=3)
text_1960 = tk.Label(root, bg="turquoise")
text_1960.grid(column=1, row=3)
text_1970 = tk.Label(root, bg="turquoise")
text_1970.grid(column=2, row=3)
# - start -
root.mainloop()
答案 1 :(得分:0)
如果有足够的空间来显示小部件,grid
几何管理器需要知道该怎么做。它通过相对于每行和每列的“权重”分配额外空间来实现。例如,如果一列的权重为3而另一列的权重为1,则第一列的额外空间量将是另一列的3倍。
默认情况下,行和列的权重为零。这意味着如果有额外的空间 - 如你的例子那样 - 它就没用了。
对您的问题绝对最简单的解决方案是为每列提供相等的非零权重,以便均匀分配额外的空间。您可以使用columnconfigure
(或grid_columnconfigure
)方法执行此操作:
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=1)
root.grid_columnconfigure(2, weight=1)
由于您说您希望标签跨越所有三列,因此您需要告诉grid
您可以通过将标签放在第0列并给它{3}的columnspan
来做到这一点:
lbl.grid(row=1,column=0, columnspan=3)