带有Canvas的ttk.Notebook选项卡,展开以填充窗口

问题描述 投票:0回答:2

我有一个简单的GUI,有三个框架。其中一个框架是包含画布的Notebook框架。问题是Notebook框架没有扩展以适应窗口。我已经尝试了grid()和pack()的所有不同组合。似乎没有什么对我有用。所以我终于来到这里了。我希望这里有人可以告诉我我做错了什么。包含我的代码的精简版本:

    from tkinter import *
import ttk

window = Tk()
window.geometry("640x480")
window.title("Notebook Test")

options_list = ["one", "two", "three"]

def doNothing():
    pass

leftFrame = Frame(window)
tabFrame = Frame(window)
statusFrame = Frame(window)

leftFrame.grid(row=0, column=0, sticky="n")
tabFrame.grid(row=0, column=1, sticky="nw")
statusFrame.grid(row=2, column=0, columnspan=2, sticky="ew")

Xlabel = Label(leftFrame, text="X Function", anchor=N)
Xlabel.pack()
x_var = StringVar(leftFrame)
x_menu = ttk.Combobox(leftFrame, textvariable=x_var)
x_menu["values"] = options_list
x_menu.current(0)
x_menu.pack()

tab_control = ttk.Notebook(tabFrame)
tab1 = ttk.Frame(tab_control)
tab2 = ttk.Frame(tab_control)
tab_control.add(tab1, text="Default One")
tab_control.add(tab2, text="Default Two")
tab_control.grid()

canvas1 = Canvas(tab1, bg="blue")
canvas2 = Canvas(tab2, bg="green")

canvas1.grid()
canvas2.grid()

tab_control.bind("<ButtonPress-1>", doNothing, True)
tab_control.bind("<ButtonPress-2>", doNothing, True)
tab_control.bind("<ButtonPress-3>", doNothing, True)

status = Label(statusFrame, text = "Status:", bd=1, relief=SUNKEN, anchor=W)
status.pack(fill=X, expand=True)

window.grid_rowconfigure(1, weight=1)
window.grid_columnconfigure(1, weight=1)

window.mainloop()

这是我目前拥有的图片:

Notebook Frame that won't resize

如您所见,“蓝色”部分应该填充窗口的更多内容。

谢谢!

canvas tkinter resize ttk
2个回答
1
投票

只需将heightwidth传递给您的笔记本小部件:

tab_control = ttk.Notebook(tabFrame,height=480,width=495)

要使标签缩放,请将<Configure>事件绑定到根窗口:

def conf(event):
    tab_control.config(height=window.winfo_height(),width=window.winfo_width()-145)

window.bind("<Configure>",conf)

0
投票

要解决您的问题,请将一些.grid()更改为.pack(...),如下所示:

from tkinter import *
import tkinter.ttk as ttk

window = Tk()
window.geometry("640x480")
window.title("Notebook Test")

options_list = ["one", "two", "three"]

def doNothing(event): # added event argument
    pass

leftFrame = Frame(window)
tabFrame = Frame(window)
statusFrame = Frame(window)

leftFrame.grid(row=0, column=0, sticky="n")
tabFrame.grid(row=0, column=1, sticky="nsew") # changed "nw" to "nsew"
statusFrame.grid(row=2, column=0, columnspan=2, sticky="ew")

Xlabel = Label(leftFrame, text="X Function", anchor=N)
Xlabel.pack()
x_var = StringVar(leftFrame)
x_menu = ttk.Combobox(leftFrame, textvariable=x_var)
x_menu["values"] = options_list
x_menu.current(0)
x_menu.pack()

tab_control = ttk.Notebook(tabFrame)
tab1 = ttk.Frame(tab_control)
tab2 = ttk.Frame(tab_control)
tab_control.add(tab1, text="Default One")
tab_control.add(tab2, text="Default Two")
tab_control.pack(expand=1, fill='both') # changed from grid() to pack()

canvas1 = Canvas(tab1, bg="blue")
canvas2 = Canvas(tab2, bg="green")

canvas1.pack(fill='both', expand=1) # changed from grid() to pack()
canvas2.pack(fill='both', expand=1) # changed from grid() to pack()

tab_control.bind("<ButtonPress-1>", doNothing, True)
tab_control.bind("<ButtonPress-2>", doNothing, True)
tab_control.bind("<ButtonPress-3>", doNothing, True)

status = Label(statusFrame, text = "Status:", bd=1, relief=SUNKEN, anchor=W)
status.pack(fill=X, expand=True)

window.grid_rowconfigure(0, weight=1)  # changed row 1 to 0
window.grid_columnconfigure(1, weight=1)

window.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.