【问题标题】:python Tkinter() how to hide the UIpython Tkinter() 如何隐藏用户界面
【发布时间】:2013-10-16 13:13:45
【问题描述】:

嗨,我正在使用 Tkinter() 在 python 中开发单个登录,我只希望当用户正确登录时,登录 UI 将被隐藏并且内容 UI 将显示......所以我在想 ui可以隐藏还是隐藏可见性? 例如我有这个代码......

def httpGETCheck(username, password):
    .... 
    # assumed that the user is correct
    if registeredUser:
        # how can I hide or set visible is false to all the UI in the showLogin() Module
        # and how can I show the showContent() UI or replaced the showContent() UI from showLogin() UI

class ContentUI():
    def showContent(self, frame):
        button1 = Button(frame, text="+", width=15, command=counterPlus)
        button1.grid()

    def showLogin(self, frame):
        self.contentUI = ContentUI()    

        L1 = Label(frame, text="User Name")
        L1.pack( side = LEFT)
        L1.grid()

        E1 = Entry(frame, bd =5)
        E1.pack(side = RIGHT)
        E1.grid()

        L2 = Label(frame, text="Password")
        L2.pack( side = LEFT)
        L2.grid()       

        E2 = Entry(frame, bd =5, show="*")
        E2.pack(side = RIGHT)
        E2.grid()

        submit = Button(frame, text="Login", width=15, command=lambda: httpGETCheck(E1.get(), E2.get())) 
        submit.grid()

class UIDisplay():
    def play(self):
        root = Tk()

        root.title(title)
        root.geometry(dimension)

        app = Frame(root)

        contentUI = ContentUI()
        contentUI.showLogin(app)

        app.grid()


        root.mainloop()

ad = UIDisplay()
ad.play()

【问题讨论】:

标签: python user-interface tkinter


【解决方案1】:

这里有一个替代方法:在 Frame 中定义每个“页面”。然后使用对lift 的调用将适当的框架提升到其他框架之上。 (以下代码大量借鉴了 Bryan Oakley 的代码 here。)

import tkinter as tk
class Page(tk.Frame):
    def __init__(self, master, text, height, width, *args, **kwargs):
        tk.Frame.__init__(self, *args, borderwidth=20, **kwargs)
        self.height = height
        self.width = width
        button = tk.Button(self, text=text, font=('Comic Sans MS', 20),
                           command=lambda: self.callback())
        button.pack(side="top", fill="both", expand=True)
    def onlift(self):
        root.geometry('{}x{}'.format(self.width, self.height))
        self.lift()

class App(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)

        p1 = Page(self, 'Login 1', height=200, width=300)
        p2 = Page(self, 'Next page is 2', height=400, width=300)
        p3 = Page(self, 'We love number 3', height=400, width=600)
        p1.callback = p2.onlift
        p2.callback = p3.onlift
        p3.callback = p1.onlift

        p1.place(x=0, y=0, relwidth=1, relheight=1)
        p2.place(x=0, y=0, relwidth=1, relheight=1)
        p3.place(x=0, y=0, relwidth=1, relheight=1)

        p1.onlift()

root = tk.Tk()
app = App(root)
root.mainloop()

上面我只定义了一种Frame,称为Page。对于您的情况,您需要一个 Login 框架,然后是一个后续框架:

    p1 = Login(self, ...)
    p2 = Registered(self, ...)

【讨论】:

  • 仅用于其他编程语言用户的文档(但在答案中简要提及):lift() 调用 tk 命令raise 以更改小部件窗口的 z 顺序。请参阅tk doc for raise
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-15
  • 2023-03-09
相关资源
最近更新 更多