python - 在 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)
选项卡2.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)
现在我想用这个程序做的是有一个类似登录的页面,一旦用户登录,它将更改同一页面上的框架,然后显示上面的程序,如上图所示的选项卡。我曾尝试查看其他具有多个框架的代码并将其放入我的代码中,但每次我遇到网格和包等错误时,空白框或窗口都是分开的。
如果可能的话,登录页面可以是它自己的文件。
我该怎么做,或者你能给我一些关于如何自己解决这个问题的线索吗?
提前致谢。
解决方案
原来你的问题是一个简单的大小问题。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()
结果:
您也可以简单地继承自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()
推荐阅读
- python - 在 Pytest 文档中使用作为 *fruit_bowl 传入参数的目的
- file-permissions - Asp.net 站点:无法访问的日志:状态
- oracle - 不一致的数据类型:预期的 CHAR 得到了 REF
- python - 将 Azure 翻译的 PDF 文件保存为 MS Word docx 文件
- python-3.x - Python 网络日志记录示例 - 如何禁用服务器端控制台打印
- javascript - 让 Bootstrap 日期选择器显示为字符串,但格式为 yyyy-mm-dddd
- java - 我无法让它在 java 中的工资单程序上正确扣除。我得到的错误是我的变量之一尚未初始化
- javascript - Vue.js/NuxtJS - 如何创建具有可通过 JSON 配置文件自定义的默认设计的组件
- docksal - Docksal 和 Git 浅问题
- javascript - React-Redux:操作已分派,但 Reducer 未更新状态