gpt4 book ai didi

python - tkinter - 加权 Canvas 不填充空白空间

转载 作者:太空宇宙 更新时间:2023-11-04 05:37:27 27 4
gpt4 key购买 nike

canvas 占据了整个屏幕,从绿色可以看出。canvasFrame 有两行,其中第二行是滚动文本小部件。第二行也被加权但是它没有填满屏幕,因为绿色区域应该是黄色的。如何让第二行填充空白并让滚动的文本小部件垂直展开。

附言innerFrame 在此示例中看起来是不必要的,但它存在是因为为了简单起见,我已经删除了框架中的其他小部件。

Canvas

代码:

import tkinter as tk
from tkinter import ttk
import tkinter.scrolledtext as tks

class Program(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)

tk.Tk.iconbitmap(self, default = "")
tk.Tk.wm_title(self, "")

container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)

self.frames = {}
for F in (Page, Other):

frame = F(container, self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")

self.show_frame(Page)

def show_frame(self,cont):
frame = self.frames[cont]
frame.tkraise()


class Page(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)

innerFrame = tk.Frame(self, bg="red")
innerFrame.place(relx=.5, rely=.5, anchor="c", relwidth=1.0, relheight=1.0)

innerFrame.grid_rowconfigure(1, weight=1)
innerFrame.grid_columnconfigure(0, weight=1)

frameOne = tk.Frame(innerFrame, bg="green")
frameOne.grid(row=1, sticky="NSWE")

self.canvas = tk.Canvas(frameOne, bg="green")

canvasFrame = tk.Frame(self.canvas, bg="yellow")
canvasFrame.pack()

canvasFrame.grid_rowconfigure(1, weight=1)

scrollbar = tk.Scrollbar(frameOne, orient="horizontal", command=self.canvas.xview)
self.canvas.configure(xscrollcommand=scrollbar.set)
scrollbar.pack(side="bottom", fill="x")

self.canvas.pack(fill="both", expand=True)
self.canvas.create_window((0, 0), window=canvasFrame, anchor="nw")

for x in range(5):
pagename = tk.Label(canvasFrame, text = "Unit Name")
pagename.grid(row=0, column=x, sticky="NSEW")
text = tks.ScrolledText(canvasFrame, width=50)
text.grid(row=1, column=x ,sticky="NSEW")

canvasFrame.bind("<Configure>", self.onFrameConfigure)

def onFrameConfigure(self, event):
'''Reset the scroll region to encompass the inner frame'''
self.canvas.configure(scrollregion=self.canvas.bbox("all"))


class Other(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)

app = Program()
app.state('zoomed')
app.mainloop()

最佳答案

你的 canvasFrame没有理由扩大以填满 Canvas 。您已将小部件放入其中,框架将缩小以适合小部件。

最常见的解决方案是添加绑定(bind)到 <Configure> Canvas 事件,获取 Canvas 高度,使用 Canvas itemconfigure调整框架高度与 Canvas 高度相同的方法。

class Page(...):
def __init__(...):
...
self.frame_id = self.canvas.create_window(...)
...
self.canvas.bind("<Configure>", self.onCanvasConfigure)

def onCanvasConfigure(self, event):
# you might need to subtract a little to account for borders,
# padding, etc.
self.canvas.itemconfigure(self.frame_id, height=event.height)

关于python - tkinter - 加权 Canvas 不填充空白空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35166465/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com