在 tkinter 中的帧之间切换。一种带标签,一种不带

我有一个程序,其中包含跨多个文件完成的多个选项卡,每个文件都有一个文件,我从这里获得并对其进行了轻微操作,因为它无法用于:


主文件


import tkinter as tk

from tkinter import ttk


from tab1 import *

from tab2 import *    


class MainApplication(tk.Frame):

  def __init__(self, parent, *args, **kwargs):

    tk.Frame.__init__(self, parent, *args, **kwargs)


notebook = ttk.Notebook(parent)


Typ1frame = Typ1(notebook)

Typ2frame = Typ2(notebook)


notebook.add(Typ1frame, text='TAB1')

notebook.add(Typ2frame, text='TAB2')

notebook.pack()


if __name__ == "__main__":

    root = tk.Tk()

    MainApplication(root).pack(side="top", fill="both", expand=True)

    root.mainloop()

表1.py


import tkinter as tk

from tkinter import ttk


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)

    shell_frame.grid(row=0,column=0,padx=5,pady=5)

tab2.py


import tkinter as tk

from tkinter import ttk


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)

    shell_frame.grid(row=0,column=0,padx=5,pady=5)

现在我想用这个程序做一个像页面一样的登录,一旦用户登录它就会改变同一页面上的框架,然后用选项卡显示如上所示的程序。我曾尝试查看其他具有多个框架的代码段并将其放入我的代码中,但是每次出现网格和包装等错误时,空白框或窗口都是分开的。


如果可能,登录页面可以是它自己的文件。


我会怎么做,或者你能给我一些关于如何自己解决这个问题的线索吗?


提前致谢。


一只斗牛犬
浏览 218回答 2
2回答

蝴蝶刀刀

原来你的问题是一个简单的大小问题。该LabelFrame基本上坐在零大小,因为你还没有添加任何东西的框架。因此,要纠正此添加宽度和高度以调整大小LabelFrame并解决吊臂问题。一旦您开始LabelFrame使用小部件填充那些's ,您将不再需要大小格式。import tkinter as tkfrom tkinter import ttkclass 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()结果:

阿波罗的战车

您也可以简单地继承 fromLabelFrame而不是Frame在您的班级中,以更少的费用获得相同的结果。例子:import tkinter as tkfrom tkinter import ttkclass MainApplication(tk.Frame):&nbsp; &nbsp; def __init__(self, parent, *args, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; tk.Frame.__init__(self, parent, *args, **kwargs)&nbsp; &nbsp; &nbsp; &nbsp; notebook = ttk.Notebook(parent)&nbsp; &nbsp; &nbsp; &nbsp; notebook.add(Typ1(notebook), text='TAB1')&nbsp; &nbsp; &nbsp; &nbsp; notebook.add(Typ2(notebook), text='TAB2')&nbsp; &nbsp; &nbsp; &nbsp; notebook.pack()class Typ1(tk.LabelFrame):&nbsp; &nbsp; def __init__(self, parent, *args, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; tk.LabelFrame.__init__(self, parent, text="Sample Label Frame", padx=5, pady=5, width=200, height=200)&nbsp; &nbsp; &nbsp; &nbsp; self.grid(row=0,column=0,padx=5,pady=5)class Typ2(tk.LabelFrame):&nbsp; &nbsp; def __init__(self, parent, *args, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; tk.LabelFrame.__init__(self, parent, text="Sample Label Frame", padx=5, pady=5, width=200, height=200)&nbsp; &nbsp; &nbsp; &nbsp; self.grid(row=0,column=0,padx=5,pady=5)if __name__ == "__main__":&nbsp; &nbsp; root = tk.Tk()&nbsp; &nbsp; MainApplication(root).pack(side="top", fill="both", expand=True)&nbsp; &nbsp; root.mainloop()要在下面回答您的评论,您将如何将框架交换与登录页面集成在一起。import tkinter as tkfrom tkinter import ttkclass App(tk.Tk):&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; tk.Tk.__init__(self)&nbsp; &nbsp; &nbsp; &nbsp; self._frame = None&nbsp; &nbsp; &nbsp; &nbsp; self.switch_frame(LoginPage)&nbsp; &nbsp; def switch_frame(self, frame_class):&nbsp; &nbsp; &nbsp; &nbsp; """Destroys current frame and replaces it with a new one."""&nbsp; &nbsp; &nbsp; &nbsp; new_frame = frame_class(self)&nbsp; &nbsp; &nbsp; &nbsp; if self._frame is not None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self._frame.destroy()&nbsp; &nbsp; &nbsp; &nbsp; self._frame = new_frame&nbsp; &nbsp; &nbsp; &nbsp; self._frame.grid(row=0, column=0)class LoginPage(tk.Frame):&nbsp; &nbsp; def __init__(self, master):&nbsp; &nbsp; &nbsp; &nbsp; tk.Frame.__init__(self, master)&nbsp; &nbsp; &nbsp; &nbsp; self.master = master&nbsp; &nbsp; &nbsp; &nbsp; tk.Label(self, text="Username: ").grid(row=0, column=0)&nbsp; &nbsp; &nbsp; &nbsp; tk.Label(self, text="Password: ").grid(row=1, column=0)&nbsp; &nbsp; &nbsp; &nbsp; self.un_entry = tk.Entry(self)&nbsp; &nbsp; &nbsp; &nbsp; self.un_entry.grid(row=0, column=1)&nbsp; &nbsp; &nbsp; &nbsp; self.pw_entry = tk.Entry(self)&nbsp; &nbsp; &nbsp; &nbsp; self.pw_entry.grid(row=1, column=1)&nbsp; &nbsp; &nbsp; &nbsp; self.pw_entry.bind("<Return>", self.check_login)&nbsp; &nbsp; &nbsp; &nbsp; tk.Button(self, text="Login", command=self.check_login).grid(row=2, column=0)&nbsp; &nbsp; def check_login(self, event=None):&nbsp; &nbsp; &nbsp; &nbsp; if self.un_entry.get() == "Mike" and self.pw_entry.get() == "pass":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.master.switch_frame(MainApplication)class MainApplication(tk.Frame):&nbsp; &nbsp; def __init__(self, parent, *args, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; tk.Frame.__init__(self, parent, *args, **kwargs)&nbsp; &nbsp; &nbsp; &nbsp; notebook = ttk.Notebook(parent)&nbsp; &nbsp; &nbsp; &nbsp; notebook.add(Typ1(notebook), text='TAB1')&nbsp; &nbsp; &nbsp; &nbsp; notebook.add(Typ2(notebook), text='TAB2')&nbsp; &nbsp; &nbsp; &nbsp; notebook.grid(row=0, column=0)class Typ1(tk.LabelFrame):&nbsp; &nbsp; def __init__(self, parent, *args, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; tk.LabelFrame.__init__(self, parent, text="Sample Label Frame", padx=5, pady=5, width=200, height=200)&nbsp; &nbsp; &nbsp; &nbsp; self.grid(row=0,column=0,padx=5,pady=5)class Typ2(tk.LabelFrame):&nbsp; &nbsp; def __init__(self, parent, *args, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; tk.LabelFrame.__init__(self, parent, text="Sample Label Frame", padx=5, pady=5, width=200, height=200)&nbsp; &nbsp; &nbsp; &nbsp; self.grid(row=0,column=0,padx=5,pady=5)if __name__ == "__main__":&nbsp; &nbsp; App().mainloop()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python