Canvas ttk.带画布的笔记本选项卡,展开以填充窗口

Canvas ttk.带画布的笔记本选项卡,展开以填充窗口,canvas,tkinter,resize,ttk,Canvas,Tkinter,Resize,Ttk,我有一个简单的GUI,它有三个框架。其中一个框架是包含画布的笔记本框架。问题是笔记本框架无法展开以适应窗口。我尝试了grid()和pack()的所有不同组合。对我来说似乎什么都不管用。所以我终于来了。我希望这里有人能告诉我我做错了什么。包括我的代码的精简版本: from tkinter import * import ttk window = Tk() window.geometry("640x480") window.title("Notebook Test") options_l

我有一个简单的GUI,它有三个框架。其中一个框架是包含画布的笔记本框架。问题是笔记本框架无法展开以适应窗口。我尝试了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()
从tkinter导入*
导入ttk
window=Tk()
窗口几何(“640x480”)
窗口标题(“笔记本测试”)
选项列表=[“一”、“二”、“三”]
def doNothing():
通过
leftFrame=框架(窗口)
tabFrame=框架(窗口)
statusFrame=帧(窗口)
leftFrame.grid(行=0,列=0,sticky=“n”)
tabFrame.grid(行=0,列=1,sticky=“nw”)
statusFrame.grid(行=2,列=0,列span=2,sticky=“ew”)
Xlabel=Label(leftFrame,text=“X函数”,anchor=N)
Xlabel.pack()
x_var=StringVar(左帧)
x_菜单=ttk.Combobox(leftFrame,textvariable=x_变量)
x_菜单[“值”]=选项列表
x_菜单。当前(0)
x_menu.pack()
tab_control=ttk.笔记本电脑(tabFrame)
tab1=ttk.Frame(制表符控件)
tab2=ttk.Frame(制表符控件)
tab_control.add(tab1,text=“Default One”)
tab_control.add(tab2,text=“Default Two”)
tab_control.grid()
canvas1=画布(tab1,bg=“蓝色”)
canvas2=画布(tab2,bg=“绿色”)
canvas1.grid()
canvas2.grid()
tab_control.bind(“,doNothing,True)
tab_control.bind(“,doNothing,True)
tab_control.bind(“,doNothing,True)
状态=标签(statusFrame,text=“status:”,bd=1,浮雕=凹陷,锚定=W)
status.pack(fill=X,expand=True)
window.grid_row配置(1,权重=1)
window.grid\u column配置(1,权重=1)
window.mainloop()
以下是我目前拥有的照片:

正如您所看到的,“蓝色”部分应该占据更多的窗口


谢谢

只需将
高度
宽度
传递到笔记本小部件:

tab_control = ttk.Notebook(tabFrame,height=480,width=495)
要缩放选项卡,请将
事件绑定到根窗口:

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

window.bind("<Configure>",conf)
def conf(事件):
tab_control.config(height=window.winfo_height(),width=window.winfo_width()-145)
window.bind(“,conf)

要解决您的问题,请将一些
.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()
从tkinter导入*
将tkinter.ttk导入为ttk
window=Tk()
窗口几何(“640x480”)
窗口标题(“笔记本测试”)
选项列表=[“一”、“二”、“三”]
def doNothing(事件):#添加了事件参数
通过
leftFrame=框架(窗口)
tabFrame=框架(窗口)
statusFrame=帧(窗口)
leftFrame.grid(行=0,列=0,sticky=“n”)
tabFrame.grid(行=0,列=1,sticky=“nsew”)#将“nw”更改为“nsew”
statusFrame.grid(行=2,列=0,列span=2,sticky=“ew”)
Xlabel=Label(leftFrame,text=“X函数”,anchor=N)
Xlabel.pack()
x_var=StringVar(左帧)
x_菜单=ttk.Combobox(leftFrame,textvariable=x_变量)
x_菜单[“值”]=选项列表
x_菜单。当前(0)
x_menu.pack()
tab_control=ttk.笔记本电脑(tabFrame)
tab1=ttk.Frame(制表符控件)
tab2=ttk.Frame(制表符控件)
tab_control.add(tab1,text=“Default One”)
tab_control.add(tab2,text=“Default Two”)
tab_control.pack(expand=1,fill='both')#从grid()更改为pack()
canvas1=画布(tab1,bg=“蓝色”)
canvas2=画布(tab2,bg=“绿色”)
canvas1.pack(fill='both',expand=1)#从grid()更改为pack()
canvas2.pack(fill='both',expand=1)#从grid()更改为pack()
tab_control.bind(“,doNothing,True)
tab_control.bind(“,doNothing,True)
tab_control.bind(“,doNothing,True)
状态=标签(statusFrame,text=“status:”,bd=1,浮雕=凹陷,锚定=W)
status.pack(fill=X,expand=True)
window.grid_rowconfigure(0,权重=1)#将第1行更改为0
window.grid\u column配置(1,权重=1)
window.mainloop()

谢谢!宽度有帮助,但高度没有调整。奇怪!它在我的Windows7中工作。您是否在
窗口中进行了更改。grid_rowconfigure(…)
?@acw1668我在不可调整大小的ttk.notebook和trusted pack()中也遇到了同样的问题,并且它仅对水平缩放有帮助,至少在Win10+python2上是这样。谢谢!这很有效。现在我只需要设定一些限制。但你是个救生员!