python3.4tkinter:框架和命令问题

2024-04-24 16:53:40 发布

您现在位置:Python中文网/ 问答频道 /正文

所以在我的代码中,我试图在第一个框架(Logo页面)上创建一个按钮,这样当点击时,它会关闭该框架,然后打开另一个框架(Intro-Fight)。然而,当前按下按钮时,第一帧会被删除,而下一帧则不会出现,留下一个空白窗口。我认为下一帧出现了,但是由于它的所有小部件都在帧打包之前打包好了,所以它们不会出现。我怎样才能让下一帧中的所有小部件都显示出来呢?这是密码。在

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


Tags: textimageself框架youdefplaceroot
2条回答

如果我正确地理解了这个问题,你就必须这么做。在

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)

然后,这样称呼它:

^{pr2}$

免责声明

@Marcin似乎对Tkinter很熟悉。我从来没有用过它,所以我不知道它的特殊性或涉及的具体方法。如果我的回答在上下文中没有意义,请参考他的答案。在

这似乎是因为您使用了“放置几何图形管理器”。我把它换成了packone,它看起来像预期的那样工作。我还从下面的代码中删除了图像,因为我没有你的图像文件,无法运行它。在

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

相关问题 更多 >