[英]Why isn't my frame showing in a tkinter window?
我正在 python 中编写一个名为 eTasks 的图形用户界面。 我需要三个框架来在主屏幕上显示不同的任务文件夹。 但是我的框架没有显示。 我已经尝试过 pack_propagate 方法并更改框架的背景颜色,但这没有用。 这是我的代码:
import tkinter as tk
from tkinter import ttk
from tkinter import*
from time import strftime
from datetime import date
import tkinter as tk
todohome=tk.Tk()
todohome.geometry('500x500+100+100')
todohome.attributes('-fullscreen', True)
todohome.overrideredirect(True)
todohome.configure(bg='white')
ctrlfrme=tk.Frame(todohome, relief='groove', highlightbackground='black', highlightthickness=1)
ctrlfrme.pack(anchor='n', side=tk.TOP, fill=X)
Clsewindowbutton=ttk.Button(ctrlfrme, text='X',
command=todohome.destroy, width=10)
Clsewindowbutton.pack(side=tk.LEFT, anchor=tk.NE)
def minimise():
todohome.overrideredirect(False)
todohome.state(newstate='iconic')
MinButton=ttk.Button(ctrlfrme, text='-', width=10, command=minimise)
MinButton.pack(side=tk.RIGHT, anchor=tk.SW)
quickbar=tk.Frame(todohome, relief="groove", highlightbackground="black"
,highlightthickness=1)
quickbar.grid_columnconfigure(5, weight=0)
quickbar.pack(anchor='n', side=tk.TOP, pady=5)
Welcome=Label(quickbar, font=('calibri', 20), text="Welcome to eTasks")
Welcome.pack(side=LEFT, anchor="w")
def time():
ctime=strftime('Time: %H:%M:%S')
clock.config(text=ctime)
clock.after(1000, time)
clock=Label(quickbar, font=('calibri', 20),
foreground='black')
clock.pack(anchor='n', side=tk.RIGHT, padx=0)
def getdate():
today = date.today()
cdate = today.strftime("%d/%m/%Y")
datecal=Label(quickbar, font=('calibri', 20), foreground='black', text=cdate)
datecal.pack(anchor='n', side=tk.LEFT, padx=100)
taskframe=tk.Frame(todohome, relief="groove", highlightbackground="black"
,highlightthickness=1, bg='red')
taskframe.pack_propagate(False)
taskframe.pack(fill=BOTH)
getdate()
time()
todohome.mainloop()
问题是你没有给taskframe
一个大小,所以它默认为一个像素的宽度和高度。 然后,您将它添加到 pack 中而不指定一侧,或者它应该扩展以填充任何剩余的未使用空间。
当您调用taskframe.pack(fill=BOTH)
时,打包器默认会将小部件对齐到剩余未使用空间的顶部。 由于打包程序分配了一整面,它是小部件的整个宽度,但只为帧高度分配一个像素,即请求的帧大小。
当您指定fill=BOTH
时,打包器将扩展框架以在分配的空间中同时填充 x 和 y 方向。 在 x 方向上,它由于被打包到顶部而分配了整个宽度,但在 y 方向上它只分配了 1 个像素。 这导致帧只有一个像素高,但宽度为 window。
解决方案可能是*在调用pack
时添加expand=True
。 这将指示加壳器扩展分配的空间,不仅适合框架,而且还包括 window 中所有剩余未使用的空间。
taskframe.pack(fill=BOTH, expand=True)
* 我说可能是因为我不确定你想要完成什么。 通常在使用pack
时,如果您希望一个小部件扩展并填充剩余空间,则该小部件将同时包含expand
和fill
选项。 不过,将多个小部件的expand
设置为True
也很常见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.