调整 ttk 笔记本的大小 ttk.frame,在笔记本之外的另一个 class 中定义的框架
Resizing ttk.frame of a ttk notebook, frame defined in a other class than the notebook
我的 gui 有一个主 class,我在标签后添加了一个 ttk.NoteBook:
class MainApplication(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('1000x500')
self.configure(background='#F0F8FF')
#TOP LABEL
load = Image.open("my_image")
load = load.resize((200, 67), Image.ANTIALIAS)
self.render = ImageTk.PhotoImage(load)
self.Label_top = tk.Label(self, image=self.render, compound=tk.LEFT, text="TOOL")
self.Label_top.pack()
#--Notebook---------
self.notebook = ttk.Notebook(self)
self.Page1 = Page1(self.notebook)
self.Page2 = Page2(self.notebook)
self.Page3 = Page3(self.notebook)
self.Page4 = Page4(self.notebook)
self.notebook.add(self.Page1, text='PAGE1')
self.notebook.add(self.Page2, text='PAGE2')
self.notebook.add(self.Page3, text='PAGE3')
self.notebook.add(self.Page4, text='PAGE4')
self.notebook.pack(fill='x', side=TOP)
#expand=True create empty space between my top label and my notebook, even with side=TOP
我在 class 中定义了每一帧,如下所示:
class Page1(ttk.Frame):
def __init__(self, container):
super().__init__()
self.(width=400, height=280) #Error message
self.pack(expand=True) #Doesn't work
你知道我怎样才能扩展我的框架,让它填满我的页面,并在我的顶部标签之后打包笔记本
我看到三个问题。
首先,每个“页面”都需要是笔记本的子页面。您可以通过确保将笔记本传递到框架的 __init__
来做到这一点:
class Page1(ttk.Frame):
def __init__(self, container):
super().__init__(container)
其次,您需要不在页面上调用pack
。 self.notebook.add
已经在将框架添加到笔记本中。因此,从每个页面中删除行 self.pack(expand=True)
。
三、self.(width=400, height=280)
需要self.configure(width=400, height=280)
我想这会如你所愿。除了我还添加了一个 BasePage
class 并从中导出所有其他 Page
class 之外,我已经合并了@Bryan Oakley 在他的回答中提到的大部分内容。这样做是为了提供一个放置代码的地方,否则需要重复每个子 classes.
我还更改了您的一些变量名称以符合 PEP 8 Naming Conventions。
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *
class BasePage(ttk.Frame):
def __init__(self, container):
super().__init__(container, width=400, height=280)
classname = type(self).__name__
tk.Label(self, text=f'Welcome to {classname}').place(relx=0.5, rely=0.25,
anchor=CENTER)
class Page1(BasePage):
def __init__(self, container):
super().__init__(container)
class Page2(BasePage):
def __init__(self, container):
super().__init__(container)
class Page3(BasePage):
def __init__(self, container):
super().__init__(container)
class Page4(BasePage):
def __init__(self, container):
super().__init__(container)
class MainApplication(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('1000x500')
self.configure(background='#F0F8FF')
#--Notebook---------
self.notebook = ttk.Notebook(self)
self.page1 = Page1(self.notebook)
self.page2 = Page2(self.notebook)
self.page3 = Page3(self.notebook)
self.page4 = Page4(self.notebook)
self.notebook.add(self.page1, text='Page1')
self.notebook.add(self.page2, text='Page2')
self.notebook.add(self.page3, text='Page3')
self.notebook.add(self.page4, text='Page4')
self.notebook.pack(expand=True, fill=BOTH)
app = MainApplication()
app.mainloop()
我的 gui 有一个主 class,我在标签后添加了一个 ttk.NoteBook:
class MainApplication(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('1000x500')
self.configure(background='#F0F8FF')
#TOP LABEL
load = Image.open("my_image")
load = load.resize((200, 67), Image.ANTIALIAS)
self.render = ImageTk.PhotoImage(load)
self.Label_top = tk.Label(self, image=self.render, compound=tk.LEFT, text="TOOL")
self.Label_top.pack()
#--Notebook---------
self.notebook = ttk.Notebook(self)
self.Page1 = Page1(self.notebook)
self.Page2 = Page2(self.notebook)
self.Page3 = Page3(self.notebook)
self.Page4 = Page4(self.notebook)
self.notebook.add(self.Page1, text='PAGE1')
self.notebook.add(self.Page2, text='PAGE2')
self.notebook.add(self.Page3, text='PAGE3')
self.notebook.add(self.Page4, text='PAGE4')
self.notebook.pack(fill='x', side=TOP)
#expand=True create empty space between my top label and my notebook, even with side=TOP
我在 class 中定义了每一帧,如下所示:
class Page1(ttk.Frame):
def __init__(self, container):
super().__init__()
self.(width=400, height=280) #Error message
self.pack(expand=True) #Doesn't work
你知道我怎样才能扩展我的框架,让它填满我的页面,并在我的顶部标签之后打包笔记本
我看到三个问题。
首先,每个“页面”都需要是笔记本的子页面。您可以通过确保将笔记本传递到框架的 __init__
来做到这一点:
class Page1(ttk.Frame):
def __init__(self, container):
super().__init__(container)
其次,您需要不在页面上调用pack
。 self.notebook.add
已经在将框架添加到笔记本中。因此,从每个页面中删除行 self.pack(expand=True)
。
三、self.(width=400, height=280)
需要self.configure(width=400, height=280)
我想这会如你所愿。除了我还添加了一个 BasePage
class 并从中导出所有其他 Page
class 之外,我已经合并了@Bryan Oakley 在他的回答中提到的大部分内容。这样做是为了提供一个放置代码的地方,否则需要重复每个子 classes.
我还更改了您的一些变量名称以符合 PEP 8 Naming Conventions。
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *
class BasePage(ttk.Frame):
def __init__(self, container):
super().__init__(container, width=400, height=280)
classname = type(self).__name__
tk.Label(self, text=f'Welcome to {classname}').place(relx=0.5, rely=0.25,
anchor=CENTER)
class Page1(BasePage):
def __init__(self, container):
super().__init__(container)
class Page2(BasePage):
def __init__(self, container):
super().__init__(container)
class Page3(BasePage):
def __init__(self, container):
super().__init__(container)
class Page4(BasePage):
def __init__(self, container):
super().__init__(container)
class MainApplication(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('1000x500')
self.configure(background='#F0F8FF')
#--Notebook---------
self.notebook = ttk.Notebook(self)
self.page1 = Page1(self.notebook)
self.page2 = Page2(self.notebook)
self.page3 = Page3(self.notebook)
self.page4 = Page4(self.notebook)
self.notebook.add(self.page1, text='Page1')
self.notebook.add(self.page2, text='Page2')
self.notebook.add(self.page3, text='Page3')
self.notebook.add(self.page4, text='Page4')
self.notebook.pack(expand=True, fill=BOTH)
app = MainApplication()
app.mainloop()