我尝试创建一个 tkinter GUI,它可以重叠每个页面第 1 页、第 2 页、第 3 页
。我在两个地方苦苦挣扎:
使用类而不是方法,所以我将它们注释掉,但我尝试将类放在命令内的括号中,即 command=(mainPage)
但这给出了错误:
builtins.NameError: name 'mainPage' is not defined
我苦苦挣扎的第二个地方是无法重叠每个类(class)。
我的代码如下:
from tkinter import *
class testOverlap(Frame):
def __init__(self, root):
Frame.__init__(self, root)
self.root = root
self.topButtons()
self.pageOne()
self.pageTwo()
self.pageThree()
def topButtons(self):
self.firstPage = Button(self.root, text="Go to Page 1", background="WHITE", height = 2, width = 16, command= self.pageOne())
self.firstPage.place(x=0, y=0)
self.secondPage = Button(self.root, text="Go to Page 2", background="WHITE",height = 2, width = 16, command= self.pageTwo())
self.secondPage.place(x=121, y=0)
self.thirdPage = Button(self.root, text="Go to Page 3", background="WHITE",height = 2, width = 17, command= self.pageThree())
self.thirdPage.place(x=242, y=0)
#class mainPage(Frame):
def pageOne(self):
self.Label1 = Label(self.root, text=" First Page ", height = 20, width = 52, background="Green")
self.Label1.place(x=0, y=40)
#class middlePage(Frame):
def pageTwo(self):
self.Label2 = Label(self.root, text=" Second Page ", height = 20, width = 52, background="Blue")
self.Label2.place(x=0, y=40)
#class finalPage(Frame):
def pageThree(self):
self.Label3 = Label(self.root, text=" Third Page ", height = 20, width = 52, background="Red")
self.Label3.place(x=0, y=40)
def main():
root = Tk()
root.title("Tk")
root.geometry('370x340')
app = testOverlap(root)
root.mainloop()
if __name__ == '__main__':
main()
最佳答案
创建框架(页面)并将其提升(将其带到顶部查看)是两个完全不同的操作。要创建它,您需要在类实例上使用像 grid
这样的布局,要引发它,您需要使用实例的 tkraise
方法。我们通常通过指定一个类来执行此操作,该类除了处理所有页面外什么都不做。试试这个:
from tkinter import *
class Controller(Frame):
def __init__(self, root):
Frame.__init__(self, root)
self.pageOne = mainPage(self)
self.pageOne.grid(row=0, column=0, sticky='nsew')
self.pageTwo = middlePage(self)
self.pageTwo.grid(row=0, column=0, sticky='nsew')
self.pageThree = finalPage(self)
self.pageThree.grid(row=0, column=0, sticky='nsew')
self.menu = testOverlap(self)
self.menu.grid(row=0, column=0, sticky='nsew')
self.menu.tkraise() # show the testOverlap Frame now
class testOverlap(Frame):
def __init__(self, root):
Frame.__init__(self, root)
self.topButtons()
def topButtons(self):
self.firstPage = Button(self, text="Go to Page 1", background="WHITE", height = 2, width = 16, command= self.master.pageOne.tkraise)
self.firstPage.grid(row=0, column=0)
self.secondPage = Button(self, text="Go to Page 2", background="WHITE",height = 2, width = 16, command= self.master.pageTwo.tkraise)
self.secondPage.grid(row=0, column=1)
self.thirdPage = Button(self, text="Go to Page 3", background="WHITE",height = 2, width = 17, command= self.master.pageThree.tkraise)
self.thirdPage.grid(row=0, column=2)
class mainPage(Frame):
def __init__(self, root):
Frame.__init__(self, root)
self.Label1 = Label(self, text=" First Page ", height = 20, width = 52, background="Green")
self.Label1.grid()
class middlePage(Frame):
def __init__(self, root):
Frame.__init__(self, root)
self.Label2 = Label(self, text=" Second Page ", height = 20, width = 52, background="Blue")
self.Label2.grid()
class finalPage(Frame):
def __init__(self, root):
Frame.__init__(self, root)
self.Label3 = Label(self, text=" Third Page ", height = 20, width = 52, background="Red")
self.Label3.grid()
def main():
root = Tk()
root.title("Tk")
root.geometry('370x340')
app = Controller(root)
app.pack(expand=True, fill=BOTH)
root.mainloop()
if __name__ == '__main__':
main()
另请注意,我删除了您对 place
的使用。我建议您尽可能远离某个地方,改用grid
或pack
。使用 place 需要在许多其他地方指定确切的大小,而 grid 或 pack 可以为您处理这个问题。
关于python - 如何使用 Tkinter 中的类创建新页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53197938/