亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

結果:

http://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 關注
  • 275 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號