【发布时间】:2019-04-11 09:11:32
【问题描述】:
我试图创建一个可以重叠每个页面Page 1, Page 2, Page 3 的 tkinter GUI。我在两个地方挣扎:
使用类而不是方法,所以我已将它们注释掉,但我尝试将类放在命令中的括号中,即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()
【问题讨论】:
-
“使用类而不是方法,所以我将它们注释掉了” - 这没有任何意义。如果它们被注释掉,你就不在使用类。您正在使用类解决问题吗?如果不是,我建议删除注释的代码,因为它在编写时非常混乱。
-
我把它们注释掉了,因为如果我把它留在里面,它会被归类为非功能代码,但我会在以后尝试重新迭代。
-
问题是,删除类完全改变了类语句下代码的性质。
标签: python python-3.x class user-interface tkinter