我试图创建一个tkinter GUI,它可以与每个页面Page 1, Page 2, Page 3
重叠。我在两个地方挣扎:
使用类而不是方法,因此我将它们注释掉,但是我试图将类放在命令command=(mainPage)
中的括号中,但这会产生错误:
builtins.NameError: name 'mainPage' is not defined
我正在努力的第二个地方不能让每个班级重叠。
我的代码如下:
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()
答案 0 :(得分:2)
创建框架(页面)并将其抬起(将其调到顶部以查看)是2个完全不同的操作。要创建它,您需要在类实例上使用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
。使用位置要求在其他许多地方指定确切的尺寸,网格或包装可以为您处理。