为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

一只斗牛犬 2021-07-12 17:35:25
我有一个程序,其中包含跨多个文件完成的多个选项卡,每个文件都有一个文件,我从这里获得并对其进行了轻微操作,因为它无法用于:主文件import tkinter as tkfrom tkinter import ttkfrom 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.pyimport tkinter as tkfrom tkinter import ttkclass 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.pyimport tkinter as tkfrom tkinter import ttkclass 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)现在我想用这个程序做一个像页面一样的登录,一旦用户登录它就会改变同一页面上的框架,然后用选项卡显示如上所示的程序。我曾尝试查看其他具有多个框架的代码段并将其放入我的代码中,但是每次出现网格和包装等错误时,空白框或窗口都是分开的。如果可能,登录页面可以是它自己的文件。我会怎么做,或者你能给我一些关于如何自己解决这个问题的线索吗?提前致谢。
查看完整描述

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()

结果:

//img1.sycdn.imooc.com//60f7cb150001db5602270270.jpg

查看完整回答
反对 回复 2021-07-21
?
阿波罗的战车

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()


查看完整回答
反对 回复 2021-07-21
  • 2 回答
  • 0 关注
  • 199 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信