2 回答
TA贡献1801条经验 获得超8个赞
原来你的问题是一个简单的大小问题。该LabelFrame基本上坐在零大小,因为你还没有添加任何东西的框架。因此,要纠正此添加宽度和高度以调整大小LabelFrame并解决吊臂问题。
一旦您开始LabelFrame使用小部件填充那些's ,您将不再需要大小格式。
import tkinter as tk
from tkinter import ttk
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
notebook = ttk.Notebook(parent)
notebook.add(Typ1(notebook), text='TAB1')
notebook.add(Typ2(notebook), text='TAB2')
notebook.pack()
class Typ1(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
shell_frame=tk.LabelFrame(self, text="Sample Label Frame", padx=5, pady=5, width=200, height=200)
shell_frame.grid(row=0,column=0,padx=5,pady=5)
class Typ2(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
shell_frame=tk.LabelFrame(self, text="Sample Label Frame", padx=5,pady=5, width=200, height=200)
shell_frame.grid(row=0,column=0,padx=5,pady=5)
if __name__ == "__main__":
root = tk.Tk()
MainApplication(root).pack(side="top", fill="both", expand=True)
root.mainloop()
结果:
TA贡献1862条经验 获得超6个赞
您也可以简单地继承 fromLabelFrame而不是Frame在您的班级中,以更少的费用获得相同的结果。
例子:
import tkinter as tk
from tkinter import ttk
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
notebook = ttk.Notebook(parent)
notebook.add(Typ1(notebook), text='TAB1')
notebook.add(Typ2(notebook), text='TAB2')
notebook.pack()
class Typ1(tk.LabelFrame):
def __init__(self, parent, *args, **kwargs):
tk.LabelFrame.__init__(self, parent, text="Sample Label Frame", padx=5, pady=5, width=200, height=200)
self.grid(row=0,column=0,padx=5,pady=5)
class Typ2(tk.LabelFrame):
def __init__(self, parent, *args, **kwargs):
tk.LabelFrame.__init__(self, parent, text="Sample Label Frame", padx=5, pady=5, width=200, height=200)
self.grid(row=0,column=0,padx=5,pady=5)
if __name__ == "__main__":
root = tk.Tk()
MainApplication(root).pack(side="top", fill="both", expand=True)
root.mainloop()
要在下面回答您的评论,您将如何将框架交换与登录页面集成在一起。
import tkinter as tk
from tkinter import ttk
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self._frame = None
self.switch_frame(LoginPage)
def switch_frame(self, frame_class):
"""Destroys current frame and replaces it with a new one."""
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.grid(row=0, column=0)
class LoginPage(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.master = master
tk.Label(self, text="Username: ").grid(row=0, column=0)
tk.Label(self, text="Password: ").grid(row=1, column=0)
self.un_entry = tk.Entry(self)
self.un_entry.grid(row=0, column=1)
self.pw_entry = tk.Entry(self)
self.pw_entry.grid(row=1, column=1)
self.pw_entry.bind("<Return>", self.check_login)
tk.Button(self, text="Login", command=self.check_login).grid(row=2, column=0)
def check_login(self, event=None):
if self.un_entry.get() == "Mike" and self.pw_entry.get() == "pass":
self.master.switch_frame(MainApplication)
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
notebook = ttk.Notebook(parent)
notebook.add(Typ1(notebook), text='TAB1')
notebook.add(Typ2(notebook), text='TAB2')
notebook.grid(row=0, column=0)
class Typ1(tk.LabelFrame):
def __init__(self, parent, *args, **kwargs):
tk.LabelFrame.__init__(self, parent, text="Sample Label Frame", padx=5, pady=5, width=200, height=200)
self.grid(row=0,column=0,padx=5,pady=5)
class Typ2(tk.LabelFrame):
def __init__(self, parent, *args, **kwargs):
tk.LabelFrame.__init__(self, parent, text="Sample Label Frame", padx=5, pady=5, width=200, height=200)
self.grid(row=0,column=0,padx=5,pady=5)
if __name__ == "__main__":
App().mainloop()
添加回答
举报