Tkinter as a class querries

by: Asma Farhat, 7 years ago


Hi there,
I am a student and have just started to learn the basic syntax of python.I am making a project. It is a kids "app", it will have a main menu and then 3 button to alphabets, colors and number.
in alphabets it will display alphabets from A-Z, then take a test in the form of MCQs whether a lower case letter matches the upper case one. Similarly for numbers, and colors. I have to do the project using tkinter and GUI.
I am facing the problem of  compiling my code of different frames and then shifting between them.  I have searched the web and one solution that i have found is on this website which says to make classes. Now here when i make classes my images doo not appear and the geometery is all messed up unlike when the codes for different alphabet/color/number were different frames. How do i tackle this? how can i adjust using just place geometry method in classes?
Also I please can someone explain the making of classes  a little bit?
Here is my code which i copied from this website or somewhere else idk, its repeitive and long, please suggest what am i missing? The gird and pack here were already in the code which i copied. I want to make my Tk() window of size "1000x624+0+0", how can i do that? and what if i want to add an image, say a picture of apple using PhotoImage in PageA. When i make a an image nd put it in the label, it doesnot show up. PLEASEEEEEE HELLLLLPPPP... My project deadline is VERY VERY CLOSE and i still have done absolutly nothing from apart this.

from tkinter import*              


class SampleApp(Tk):

    def __init__(self):
        Tk.__init__(self)

        self.title = ("FunLearning")

        # the container is where we'll stack frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne, PageTwo, PageThree, PageA, PageB, PageC,
                  PageD, PageE, PageF, PageG, PageH, PageI, PageJ, PageK,
                  PageABCTest,alphaMCQ1, alphaMCQ2):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")
        self.alphaMCQPoints = IntVar()
        alphaMCQPoints.set(0)

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()
        
    def add_point_MCQalpha(self):
        self.alphaMCQPoints.set(self.alphaMCQPoints.get()+1)


class StartPage(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller
        label = Label(self, text="tWelcome to FUNLEARNINGttnnnMAIN MENUnnnnnnn", font=("Ravie", 20, "bold italic"), bg='pink', fg = "purple")
        label.pack(side="top", fill="x")

        label1 = Label(self, text="  ", font=("Ravie", 20, "bold italic"), bg='pink')
        label1.pack(side="bottom", fill="both", pady=10, anchor ="s")
        button3 = Button(label1, text="COLORS",
                            command=lambda: controller.show_frame("PageThree"),bg = "orange", fg = "yellow", font = ("Ravie", 20,"bold"))
        button2 = Button(label1, text="123",
                            command=lambda: controller.show_frame("PageTwo"),bg = "pink", fg = "green", font = ("Ravie", 20,"bold"))
        button1 = Button(label1, text="ABC",
                            command=lambda: controller.show_frame("PageOne"), bg = "yellow", fg = "Purple", font = ("Ravie", 20,"bold"))
                
        button1.pack(side = "top")
        button2.pack(side = "top")
        button3.pack(side = "top")
        


class PageOne(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller
        label = Label(self, text="This is Alphabet Menu", font=("Comic Sans", 50, "italic"), bg="Yellow", fg = "Purple" )
        label.pack(side="top", fill="x", pady=10)
        label1 = Label(self,  bg="light blue" )
        label1.pack(side="bottom", fill="x", pady=10)
        button = Button(label1, text="Go to the Main Page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack(side = "bottom")
        button1 = Button(label1, text="Go to A",
                           command=lambda: controller.show_frame("PageA"))
        button1.pack(side = "bottom")



class PageTwo(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller
        label = Label(self, text="This is Numbers", font=("Helvetica", 20, "bold italic"))
        label.pack(side="top", fill="x", pady=10)
        button = Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()

class PageThree(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller
        label = Label(self, text="This is Colors", font=("Cmic Sans", 20, "italic"))
        label.pack(side="top", fill="x", pady=10)
        button = Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()

class PageA(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller
        apple = PhotoImage(file="C:UsersDELLDownloadsProject FCP CodeColorsbluesky.gif")
        
        label = Label(self,  bg = "pink")
        label.pack( fill="both", pady=10)
        label2 = Label(label, image = apple,  bg = "light blue")
        label2.pack()
        label3 = Label(label, text = "Aa", font =("Comic Sans", 50, "italic") , bg = "light blue")
        label3.pack(side="left",  pady=10)

        
        button1 = Button(label, text="Go to the start page",command=lambda: controller.show_frame("StartPage"))
        button1.pack(side = "left")
        button2 = Button(label, text="Next",command=lambda: controller.show_frame("PageB"))
        button2.pack(side = "left")


class PageB(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller
        
        label = Label(self,  bg = "pink")
        label.pack( fill="both", pady=10)
        label2 = Label(label, text = "insert image here",  bg = "light blue")
        label2.pack(side="right",  pady=10)
        label3 = Label(label, text = "Bb", font =("Comic Sans", 50, "italic") , bg = "light blue")
        label3.pack(side="left",  pady=10)

        button2 = Button(label, text="Back",command=lambda: controller.show_frame("PageA"))
        button2.pack(side = "bottom")
        button = Button(label, text="Go to the start page",command=lambda: controller.show_frame("StartPage"))
        button.pack(side = "bottom")
        button1 = Button(label, text="Next",command=lambda: controller.show_frame("PageC"))
        button1.pack(side = "bottom")
if __name__ == "__main__":

        app = SampleApp()
        app.mainloop()

]




You must be logged in to post. Please login or register an account.