[英]Python 3.4 Tkinter: Frames and Command issues
因此,在我的代碼中,我正在嘗試在第一幀(徽標頁面)上創建一個按鈕,以便在單擊時關閉該幀然后打開另一個幀(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()
如果我正確理解問題,你只需要這樣做。
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)
然后,這樣稱呼它:
def showIntro(self): #Transition into Intro Fight frame.
self.LogoFrame.destroy()
self.createIntroFightFrame()
@Marcin似乎熟悉Tkinter。 我從來沒有使用它,所以我不知道它的特點或涉及的具體方法。 如果我在上下文中沒有意義,請參考他的回答。
這似乎是因為您使用場所幾何管理器。 我改變它打包一個,它似乎按預期工作。 我還從下面的代碼中刪除了圖像,因為我沒有你的圖像文件,無法運行它。
############################## 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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.