【问题标题】:How to navigate via pages in tkinter? [duplicate]如何通过 tkinter 中的页面导航? [复制]
【发布时间】:2020-08-21 16:18:44
【问题描述】:

所以对于我的项目,当我单击一个按钮时 - 页面必须导航到另一个。当我每次都这样做时,它会创建一个不同的窗口(因此会产生很多窗口)或保持相互堆叠

目前我有一个管理员/学生选择页面。如果我点击学生页面,它将运行学生登录功能,这同样适用于管理员。

我也想保持窗口的背景一致 - 但我无法做到这一点,最重要的是,我不知道如何通过页面导航 - 我看过一些视频,但我做到了不是很懂。

contains 2 buttons admin and student which should navigate to different "windows" when clicked

def mainPage():
""""Starting Page - which navigates to the approporate login, when user logs it navigates back to this window"""
    root = Tk()
    root.geometry('1024x600')
    load = PIL.Image.open('Image//book.jpg')
    render = ImageTk.PhotoImage(load)
    img =Label(root, image = render)
    img.place(x=0, y=0)
    my_font = font.Font(size = 15)
    buttonFrame = Frame(root, width = 600, height=300, bg="grey")
    buttonFrame.grid(row = 0, column = 0, padx= 10, pady=2, sticky="")

    label = Label(buttonFrame, text = "Pick a Log-in", bg = "white", font=my_font).grid(row = 1, column=1, sticky="", padx=10, pady=10)
    button = Button(buttonFrame, text = "Admin",command = adminLogin, width=30, height = 5, font=my_font).grid(row=2, column=1, sticky = "", padx= 10, pady=10)
    button2 = Button(buttonFrame, text = "Student", command = studentLogin, width = 30, height=5, font=my_font).grid(row = 3, column=1, sticky="", padx = 10, pady=10)


    root.mainloop()
def studentLogin():
    root = Tk()
    
    root.geometry('1024x600')
    load = PIL.Image.open('Image//book.jpg')
    render = ImageTk.PhotoImage(load)
    img =Label(root, image = render)
    img.place(x=0, y=0)

    my_font = font.Font(size = 15)
    buttonFrame = Frame(root, width = 600, height=600, bg="grey")
    buttonFrame.grid(row = 0, column = 0, padx= 10, pady=2, sticky="")
    root.grid_rowconfigure(0, weight=1)
    root.grid_columnconfigure(0, weight=1)
    label = Label (buttonFrame, text="Welcome", font=('Helvetica', 18, 'bold'), bg="white").grid(row = 1, column = 1,sticky="", padx=10, pady=10)
    label = Label(buttonFrame, text="Username", font = my_font).grid(row=2, column=1, padx=10, pady=10)
    usernameEntry = Entry(buttonFrame).grid(row=2, column=2, padx=10, pady=10)
    
   
    root.mainloop()

【问题讨论】:

  • 如果您想要简单的答案:如果您使用grid,则在您要删除的页面上使用grid_forget,并在您要显示的页面上调用grid。如果您使用pack 或place,除了分别调用pack_forget 或place_forget 之外,它是相同的。
  • 请修正您的代码缩进。

标签: python xcode user-interface tkinter computer-science


【解决方案1】:

也许可以试试 PySimpleGUI :)

对于这些情况,它有一个方便的 window.Hide() 或 window.UnHide()。它也是一个用于开发 GUI、良好文档、强大支持且易于上手的优秀库。

对于 tkinter,你可以试试

root.withdraw() 

和

root.update()
root.deiconify()

【讨论】:

  • 哦,好的,谢谢!使用 tkinter 选项 - 你会在哪里输入 - 我已经更新了帖子:D
  • 这甚至还没有开始回答这个问题。 root.withdraw() 和 root.deiconify 影响整个窗口。 OP 想要在单个窗口内切换页面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-12
  • 1970-01-01
  • 1970-01-01
  • 2015-11-22
  • 1970-01-01
  • 2019-10-27
相关资源
最近更新 更多