How to create a new pages using classes in Tkinter?











up vote
1
down vote

favorite
1












I have attempted to create a tkinter GUI which can overlap each page Page 1, Page 2, Page 3. I am struggling in two places:



Using the class rather than the method so I have commented them out, but what I have tried at is putting the class in brackets within the command i.e. command=(mainPage) but this gives the error:



builtins.NameError: name 'mainPage' is not defined


Second place I am struggling is not being able to overlap each class.



My code is as follows:



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()









share|improve this question
























  • "Using the class rather than the method so I have commented them out" - this makes no sense. If they are commented out, you are not using classes. Is the problem you're solving using classes or not? If not, I recommend removing the commented code because it's very confusing as written.
    – Bryan Oakley
    Nov 7 at 21:35












  • I commented them out because if i left it in it would be classed as a non-functional code but i will try re-iterate this in the future.
    – pythonewbie
    Nov 7 at 21:52










  • The problem is, removing the class completely changes the nature of the code under the class statements.
    – Bryan Oakley
    Nov 7 at 22:27















up vote
1
down vote

favorite
1












I have attempted to create a tkinter GUI which can overlap each page Page 1, Page 2, Page 3. I am struggling in two places:



Using the class rather than the method so I have commented them out, but what I have tried at is putting the class in brackets within the command i.e. command=(mainPage) but this gives the error:



builtins.NameError: name 'mainPage' is not defined


Second place I am struggling is not being able to overlap each class.



My code is as follows:



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()









share|improve this question
























  • "Using the class rather than the method so I have commented them out" - this makes no sense. If they are commented out, you are not using classes. Is the problem you're solving using classes or not? If not, I recommend removing the commented code because it's very confusing as written.
    – Bryan Oakley
    Nov 7 at 21:35












  • I commented them out because if i left it in it would be classed as a non-functional code but i will try re-iterate this in the future.
    – pythonewbie
    Nov 7 at 21:52










  • The problem is, removing the class completely changes the nature of the code under the class statements.
    – Bryan Oakley
    Nov 7 at 22:27













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I have attempted to create a tkinter GUI which can overlap each page Page 1, Page 2, Page 3. I am struggling in two places:



Using the class rather than the method so I have commented them out, but what I have tried at is putting the class in brackets within the command i.e. command=(mainPage) but this gives the error:



builtins.NameError: name 'mainPage' is not defined


Second place I am struggling is not being able to overlap each class.



My code is as follows:



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()









share|improve this question















I have attempted to create a tkinter GUI which can overlap each page Page 1, Page 2, Page 3. I am struggling in two places:



Using the class rather than the method so I have commented them out, but what I have tried at is putting the class in brackets within the command i.e. command=(mainPage) but this gives the error:



builtins.NameError: name 'mainPage' is not defined


Second place I am struggling is not being able to overlap each class.



My code is as follows:



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 21:21









martineau

65k987176




65k987176










asked Nov 7 at 21:16









pythonewbie

215




215












  • "Using the class rather than the method so I have commented them out" - this makes no sense. If they are commented out, you are not using classes. Is the problem you're solving using classes or not? If not, I recommend removing the commented code because it's very confusing as written.
    – Bryan Oakley
    Nov 7 at 21:35












  • I commented them out because if i left it in it would be classed as a non-functional code but i will try re-iterate this in the future.
    – pythonewbie
    Nov 7 at 21:52










  • The problem is, removing the class completely changes the nature of the code under the class statements.
    – Bryan Oakley
    Nov 7 at 22:27


















  • "Using the class rather than the method so I have commented them out" - this makes no sense. If they are commented out, you are not using classes. Is the problem you're solving using classes or not? If not, I recommend removing the commented code because it's very confusing as written.
    – Bryan Oakley
    Nov 7 at 21:35












  • I commented them out because if i left it in it would be classed as a non-functional code but i will try re-iterate this in the future.
    – pythonewbie
    Nov 7 at 21:52










  • The problem is, removing the class completely changes the nature of the code under the class statements.
    – Bryan Oakley
    Nov 7 at 22:27
















"Using the class rather than the method so I have commented them out" - this makes no sense. If they are commented out, you are not using classes. Is the problem you're solving using classes or not? If not, I recommend removing the commented code because it's very confusing as written.
– Bryan Oakley
Nov 7 at 21:35






"Using the class rather than the method so I have commented them out" - this makes no sense. If they are commented out, you are not using classes. Is the problem you're solving using classes or not? If not, I recommend removing the commented code because it's very confusing as written.
– Bryan Oakley
Nov 7 at 21:35














I commented them out because if i left it in it would be classed as a non-functional code but i will try re-iterate this in the future.
– pythonewbie
Nov 7 at 21:52




I commented them out because if i left it in it would be classed as a non-functional code but i will try re-iterate this in the future.
– pythonewbie
Nov 7 at 21:52












The problem is, removing the class completely changes the nature of the code under the class statements.
– Bryan Oakley
Nov 7 at 22:27




The problem is, removing the class completely changes the nature of the code under the class statements.
– Bryan Oakley
Nov 7 at 22:27












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Creating a Frame (page) and raising it (bringing it to the top to view it) are 2 completely different operations. To create it you need to use a layout like grid on the class instance, and to raise it you need to use the tkraise method of the instance. We usually do this by dedicating a class to do nothing but handle all the pages. Try this:



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()


Note also I removed your use of place. I recommend you stay away from place if you can, use grid or pack instead. Using place requires specifying the exact size in a lot of other places, and grid or pack can handle that for you.






share|improve this answer























  • when would you suggest i could use place, could i read on these bugs if you have a link? I am new to python, but this was great help i shall study this code now and compare it to my question.
    – pythonewbie
    Nov 7 at 21:54










  • I don't think it's fair to say place causes weird bugs. place is entirely deterministic and isn't buggy. The only problems it causes are that it requires you to do a lot more work if you want your UI to be responsive and easy to maintain.
    – Bryan Oakley
    Nov 7 at 22:02










  • place should only be used when a floating widget is needed that partially covers up other widgets. It requires that the user defines ALL dimensions, so forgetting one makes the whole thing not work (which is what happens if you replace grid with place in the code above) or it makes things appear off screen with new fonts / versions of python / computers. For now forget that place exists and let tkinter do the hard work.
    – Novel
    Nov 7 at 22:03










  • No, that's not necessarily true that place is only for floating widgets. place is actually quite good when you want to stack widgets on top of each other. it has other uses as well. place is actually quite powerful and flexible. I don't recommend it for most work, and I especially don't recommend it for beginners, but it's definitely good for more things than just floating widgets.
    – Bryan Oakley
    Nov 7 at 22:04












  • @BryanOakley I edited out the buggy comment, I agree that implied something wrong with tkinter not the user. But I disagree with your other comment; I have yet to find any other use for place that can't be solved more easily and neater with grid. (Mind floating widgets is a very important role; I'm not advocating for never using place.)
    – Novel
    Nov 7 at 22:16













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53197938%2fhow-to-create-a-new-pages-using-classes-in-tkinter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Creating a Frame (page) and raising it (bringing it to the top to view it) are 2 completely different operations. To create it you need to use a layout like grid on the class instance, and to raise it you need to use the tkraise method of the instance. We usually do this by dedicating a class to do nothing but handle all the pages. Try this:



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()


Note also I removed your use of place. I recommend you stay away from place if you can, use grid or pack instead. Using place requires specifying the exact size in a lot of other places, and grid or pack can handle that for you.






share|improve this answer























  • when would you suggest i could use place, could i read on these bugs if you have a link? I am new to python, but this was great help i shall study this code now and compare it to my question.
    – pythonewbie
    Nov 7 at 21:54










  • I don't think it's fair to say place causes weird bugs. place is entirely deterministic and isn't buggy. The only problems it causes are that it requires you to do a lot more work if you want your UI to be responsive and easy to maintain.
    – Bryan Oakley
    Nov 7 at 22:02










  • place should only be used when a floating widget is needed that partially covers up other widgets. It requires that the user defines ALL dimensions, so forgetting one makes the whole thing not work (which is what happens if you replace grid with place in the code above) or it makes things appear off screen with new fonts / versions of python / computers. For now forget that place exists and let tkinter do the hard work.
    – Novel
    Nov 7 at 22:03










  • No, that's not necessarily true that place is only for floating widgets. place is actually quite good when you want to stack widgets on top of each other. it has other uses as well. place is actually quite powerful and flexible. I don't recommend it for most work, and I especially don't recommend it for beginners, but it's definitely good for more things than just floating widgets.
    – Bryan Oakley
    Nov 7 at 22:04












  • @BryanOakley I edited out the buggy comment, I agree that implied something wrong with tkinter not the user. But I disagree with your other comment; I have yet to find any other use for place that can't be solved more easily and neater with grid. (Mind floating widgets is a very important role; I'm not advocating for never using place.)
    – Novel
    Nov 7 at 22:16

















up vote
2
down vote



accepted










Creating a Frame (page) and raising it (bringing it to the top to view it) are 2 completely different operations. To create it you need to use a layout like grid on the class instance, and to raise it you need to use the tkraise method of the instance. We usually do this by dedicating a class to do nothing but handle all the pages. Try this:



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()


Note also I removed your use of place. I recommend you stay away from place if you can, use grid or pack instead. Using place requires specifying the exact size in a lot of other places, and grid or pack can handle that for you.






share|improve this answer























  • when would you suggest i could use place, could i read on these bugs if you have a link? I am new to python, but this was great help i shall study this code now and compare it to my question.
    – pythonewbie
    Nov 7 at 21:54










  • I don't think it's fair to say place causes weird bugs. place is entirely deterministic and isn't buggy. The only problems it causes are that it requires you to do a lot more work if you want your UI to be responsive and easy to maintain.
    – Bryan Oakley
    Nov 7 at 22:02










  • place should only be used when a floating widget is needed that partially covers up other widgets. It requires that the user defines ALL dimensions, so forgetting one makes the whole thing not work (which is what happens if you replace grid with place in the code above) or it makes things appear off screen with new fonts / versions of python / computers. For now forget that place exists and let tkinter do the hard work.
    – Novel
    Nov 7 at 22:03










  • No, that's not necessarily true that place is only for floating widgets. place is actually quite good when you want to stack widgets on top of each other. it has other uses as well. place is actually quite powerful and flexible. I don't recommend it for most work, and I especially don't recommend it for beginners, but it's definitely good for more things than just floating widgets.
    – Bryan Oakley
    Nov 7 at 22:04












  • @BryanOakley I edited out the buggy comment, I agree that implied something wrong with tkinter not the user. But I disagree with your other comment; I have yet to find any other use for place that can't be solved more easily and neater with grid. (Mind floating widgets is a very important role; I'm not advocating for never using place.)
    – Novel
    Nov 7 at 22:16















up vote
2
down vote



accepted







up vote
2
down vote



accepted






Creating a Frame (page) and raising it (bringing it to the top to view it) are 2 completely different operations. To create it you need to use a layout like grid on the class instance, and to raise it you need to use the tkraise method of the instance. We usually do this by dedicating a class to do nothing but handle all the pages. Try this:



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()


Note also I removed your use of place. I recommend you stay away from place if you can, use grid or pack instead. Using place requires specifying the exact size in a lot of other places, and grid or pack can handle that for you.






share|improve this answer














Creating a Frame (page) and raising it (bringing it to the top to view it) are 2 completely different operations. To create it you need to use a layout like grid on the class instance, and to raise it you need to use the tkraise method of the instance. We usually do this by dedicating a class to do nothing but handle all the pages. Try this:



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()


Note also I removed your use of place. I recommend you stay away from place if you can, use grid or pack instead. Using place requires specifying the exact size in a lot of other places, and grid or pack can handle that for you.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 6:45

























answered Nov 7 at 21:44









Novel

7,1801922




7,1801922












  • when would you suggest i could use place, could i read on these bugs if you have a link? I am new to python, but this was great help i shall study this code now and compare it to my question.
    – pythonewbie
    Nov 7 at 21:54










  • I don't think it's fair to say place causes weird bugs. place is entirely deterministic and isn't buggy. The only problems it causes are that it requires you to do a lot more work if you want your UI to be responsive and easy to maintain.
    – Bryan Oakley
    Nov 7 at 22:02










  • place should only be used when a floating widget is needed that partially covers up other widgets. It requires that the user defines ALL dimensions, so forgetting one makes the whole thing not work (which is what happens if you replace grid with place in the code above) or it makes things appear off screen with new fonts / versions of python / computers. For now forget that place exists and let tkinter do the hard work.
    – Novel
    Nov 7 at 22:03










  • No, that's not necessarily true that place is only for floating widgets. place is actually quite good when you want to stack widgets on top of each other. it has other uses as well. place is actually quite powerful and flexible. I don't recommend it for most work, and I especially don't recommend it for beginners, but it's definitely good for more things than just floating widgets.
    – Bryan Oakley
    Nov 7 at 22:04












  • @BryanOakley I edited out the buggy comment, I agree that implied something wrong with tkinter not the user. But I disagree with your other comment; I have yet to find any other use for place that can't be solved more easily and neater with grid. (Mind floating widgets is a very important role; I'm not advocating for never using place.)
    – Novel
    Nov 7 at 22:16




















  • when would you suggest i could use place, could i read on these bugs if you have a link? I am new to python, but this was great help i shall study this code now and compare it to my question.
    – pythonewbie
    Nov 7 at 21:54










  • I don't think it's fair to say place causes weird bugs. place is entirely deterministic and isn't buggy. The only problems it causes are that it requires you to do a lot more work if you want your UI to be responsive and easy to maintain.
    – Bryan Oakley
    Nov 7 at 22:02










  • place should only be used when a floating widget is needed that partially covers up other widgets. It requires that the user defines ALL dimensions, so forgetting one makes the whole thing not work (which is what happens if you replace grid with place in the code above) or it makes things appear off screen with new fonts / versions of python / computers. For now forget that place exists and let tkinter do the hard work.
    – Novel
    Nov 7 at 22:03










  • No, that's not necessarily true that place is only for floating widgets. place is actually quite good when you want to stack widgets on top of each other. it has other uses as well. place is actually quite powerful and flexible. I don't recommend it for most work, and I especially don't recommend it for beginners, but it's definitely good for more things than just floating widgets.
    – Bryan Oakley
    Nov 7 at 22:04












  • @BryanOakley I edited out the buggy comment, I agree that implied something wrong with tkinter not the user. But I disagree with your other comment; I have yet to find any other use for place that can't be solved more easily and neater with grid. (Mind floating widgets is a very important role; I'm not advocating for never using place.)
    – Novel
    Nov 7 at 22:16


















when would you suggest i could use place, could i read on these bugs if you have a link? I am new to python, but this was great help i shall study this code now and compare it to my question.
– pythonewbie
Nov 7 at 21:54




when would you suggest i could use place, could i read on these bugs if you have a link? I am new to python, but this was great help i shall study this code now and compare it to my question.
– pythonewbie
Nov 7 at 21:54












I don't think it's fair to say place causes weird bugs. place is entirely deterministic and isn't buggy. The only problems it causes are that it requires you to do a lot more work if you want your UI to be responsive and easy to maintain.
– Bryan Oakley
Nov 7 at 22:02




I don't think it's fair to say place causes weird bugs. place is entirely deterministic and isn't buggy. The only problems it causes are that it requires you to do a lot more work if you want your UI to be responsive and easy to maintain.
– Bryan Oakley
Nov 7 at 22:02












place should only be used when a floating widget is needed that partially covers up other widgets. It requires that the user defines ALL dimensions, so forgetting one makes the whole thing not work (which is what happens if you replace grid with place in the code above) or it makes things appear off screen with new fonts / versions of python / computers. For now forget that place exists and let tkinter do the hard work.
– Novel
Nov 7 at 22:03




place should only be used when a floating widget is needed that partially covers up other widgets. It requires that the user defines ALL dimensions, so forgetting one makes the whole thing not work (which is what happens if you replace grid with place in the code above) or it makes things appear off screen with new fonts / versions of python / computers. For now forget that place exists and let tkinter do the hard work.
– Novel
Nov 7 at 22:03












No, that's not necessarily true that place is only for floating widgets. place is actually quite good when you want to stack widgets on top of each other. it has other uses as well. place is actually quite powerful and flexible. I don't recommend it for most work, and I especially don't recommend it for beginners, but it's definitely good for more things than just floating widgets.
– Bryan Oakley
Nov 7 at 22:04






No, that's not necessarily true that place is only for floating widgets. place is actually quite good when you want to stack widgets on top of each other. it has other uses as well. place is actually quite powerful and flexible. I don't recommend it for most work, and I especially don't recommend it for beginners, but it's definitely good for more things than just floating widgets.
– Bryan Oakley
Nov 7 at 22:04














@BryanOakley I edited out the buggy comment, I agree that implied something wrong with tkinter not the user. But I disagree with your other comment; I have yet to find any other use for place that can't be solved more easily and neater with grid. (Mind floating widgets is a very important role; I'm not advocating for never using place.)
– Novel
Nov 7 at 22:16






@BryanOakley I edited out the buggy comment, I agree that implied something wrong with tkinter not the user. But I disagree with your other comment; I have yet to find any other use for place that can't be solved more easily and neater with grid. (Mind floating widgets is a very important role; I'm not advocating for never using place.)
– Novel
Nov 7 at 22:16




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53197938%2fhow-to-create-a-new-pages-using-classes-in-tkinter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma