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

时间:2018-10-17 10:13:28

标签: python python-3.x tkinter

我有一个程序,其中有多个选项卡,这些选项卡跨多个文件完成,每个文件都有一个文件,它们是我从here获得的,并对其稍加操作,因为它无法起作用:

main.py

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

tab1.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)

现在我要对该程序执行的操作是具有一个类似页面的登录名,一旦用户登录它,它将更改同一页面上的框架,然后使用选项卡显示如上所示的程序。我曾尝试查看其他具有多个框架的代码片段并将其放入我的代码中,但是每次我在网格和包装等错误,空白框或窗口分开时都遇到错误。

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

我该怎么做,或者您能给我一些有关如何自己弄清楚的线索吗?

先谢谢了。

1 个答案:

答案 0 :(得分:0)

证明您的问题很简单。 LabelFrame的大小基本上为零,因为您没有在框架中添加任何内容。因此,要纠正此添加的宽度和高度,以调整LabelFrame和悬臂问题的大小。

一旦您开始用小部件填充这些LabelFrame,您将不再需要大小格式。

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

结果:

enter image description here

您也可以在类中简单地继承LabelFrame而不是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()