1pokepie
1pokepie

Reputation: 72

Python 3.4 Tkinter: Frames and Command issues

So in my code, I'm trying to make a button on the first frame(Logo Page) so that when clicked, it closes that frame and then opens another frame(Intro Fight). However, currently when the button is pressed, the the first frame gets deleted, however the the next frame doesn't show up, leaving a blank window. I think what's happening is that the next frame appears, but since all of it's widgets were packed before the frame was packed, they don't show up. How would I be able to have all the widgets that are supposed to be in the next frame show up? Here's the code.

##############################     GUI GAME       #############################
from tkinter import *


##############################    PROGRAM      #############################
class MyApp:


    def __init__(self,parent): 
################################## LOGO PAGE   ###############################
        self.myParent=parent
        Pic1 = PhotoImage(file="../Python STUFF/RestLogo.gif") #logo
        Pic2 = PhotoImage(file="../Python STUFF/IntroBattle.gif") #Intro Battle
        self.LogoFrame=Frame(parent)
        self.LogoFrame.pack()

        self.b1=Button(self.LogoFrame, text=('Continue'),command = self.showIntro)
        self.b1.pack(side='bottom')
        self.w1 = Label(self.LogoFrame, image = Pic1)
        self.w1.image=Pic1
        self.w1.pack(side='top')
        
        
################################### INTRO FIGHT    #############################        
        



        self.IntroFrame=Frame(parent)
        
        self.IntroBG=Label(self.IntroFrame, image=Pic2)
        self.IntroBG.image=Pic2
        self.IntroBG.place(x=0,y=0)
        
        self.text=Message(self.IntroFrame, fg='black',text="""Flaming Arrows whizz over your hair, War rages around you. Suddenly, it charges at you. A 8 foot tall mechanical beast the enemy have been training for war. You have no chance but to fight it. You swing your sword as hard as you can...Only to leave a minor dent on it's armor. With one blow from its club, you fall unconscious."""
        , font='Times 15 bold')
        self.text.place(x=70, y=50)
        self.FinishSlide1=Button(self.IntroFrame, text='Next', command = self.WakingUp, width=10)
        self.FinishSlide1.place(x=500, y=700)
        
    def WakingUp(self): #Closes game for now
        root.destroy()
    def showIntro(self): #Transition into Intro Fight frame.
        self.LogoFrame.destroy()
        self.IntroFrame.place(x=0, y=0)

 
    
       

print ('\n'*100) #Clear Screen
root = Tk()
myapp=MyApp(root)
root.title('GUI GAME')
root.mainloop()

Upvotes: 1

Views: 236

Answers (2)

Eithos
Eithos

Reputation: 2491

If I'm understanding the problem correctly, you'd simply have to do this.

def createIntroFightFrame(self):

    self.IntroFrame=Frame(self.myParent) # Notice I'm using self.myParent here

    self.IntroFrame.place(x=0, y=0) # Place this here instead of in `showIntro()`

    self.IntroBG=Label(self.IntroFrame, image=Pic2)
    self.IntroBG.image=Pic2
    self.IntroBG.place(x=0,y=0)

    self.text=Message(self.IntroFrame, fg='black',text="""Flaming Arrows whizz over your hair, War rages around you. Suddenly, it charges at you. A 8 foot tall mechanical beast the enemy have been training for war. You have no chance but to fight it. You swing your sword as hard as you can...Only to leave a minor dent on it's armor. With one blow from its club, you fall unconscious."""
    , font='Times 15 bold')
    self.text.place(x=70, y=50)
    self.FinishSlide1=Button(self.IntroFrame, text='Next', command = self.WakingUp, width=10)
    self.FinishSlide1.place(x=500, y=700)

And then, call it this way:

def showIntro(self): #Transition into Intro Fight frame.
    self.LogoFrame.destroy()
    self.createIntroFightFrame()

Disclaimer

@Marcin seems to be familiar with Tkinter. I've never used it, so I don't know about its peculiarities or the specific methods involved. Refer to his answer if mine doesn't make sense in the context.

Upvotes: 2

Marcin
Marcin

Reputation: 238887

This seem to be because you use place geometry manager. I changed it to pack one, and it seems to be working as expected. I also removed images from the code below, as I dont have your image files and could not run it.

##############################     GUI GAME       #############################
from tkinter import *


##############################    PROGRAM      #############################
class MyApp:


    def __init__(self,parent): 
################################## LOGO PAGE   ###############################
        self.myParent=parent
        #Pic1 = PhotoImage(file="../Python STUFF/RestLogo.gif") #logo
        #Pic2 = PhotoImage(file="../Python STUFF/IntroBattle.gif") #Intro Battle
        self.LogoFrame=Frame(parent)
        self.LogoFrame.pack()

        self.b1=Button(self.LogoFrame, text=('Continue'),command = self.showIntro)
        self.b1.pack(side='bottom')
        self.w1 = Label(self.LogoFrame)
        #self.w1.image=Pic1
        self.w1.pack(side='top')


################################### INTRO FIGHT    #############################        




        self.IntroFrame=Frame(parent)

        self.IntroBG=Label(self.IntroFrame)
        #self.IntroBG.image=Pic2
        #self.IntroBG.place(x=0,y=0)
        self.IntroBG.pack()

        self.text=Message(self.IntroFrame, fg='black',text="""Flaming Arrows whizz over your hair, War rages around you. Suddenly, it charges at you. A 8 foot tall mechanical beast the enemy have been training for war. You have no chance but to fight it. You swing your sword as hard as you can...Only to leave a minor dent on it's armor. With one blow from its club, you fall unconscious."""
                                ,font='Times 15 bold')
        #self.text.place(x=70, y=50)
        self.text.pack()
        self.FinishSlide1=Button(self.IntroFrame, text='Next', command = self.WakingUp, width=10)
        #self.FinishSlide1.place(x=500, y=700)
        self.FinishSlide1.pack()

    def WakingUp(self): #Closes game for now
        root.destroy()

    def showIntro(self): #Transition into Intro Fight frame.
        self.LogoFrame.destroy()
        #self.IntroFrame.place(x=0, y=0)
        self.IntroFrame.pack()





print ('\n'*100) #Clear Screen
root = Tk()
myapp=MyApp(root)
root.title('GUI GAME')
root.mainloop()

Upvotes: 1

Related Questions