Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Tkinter中更改帧_Python_Tkinter_Frames - Fatal编程技术网

Python 在Tkinter中更改帧

Python 在Tkinter中更改帧,python,tkinter,frames,Python,Tkinter,Frames,我一直在教一个家庭朋友的女儿一些基本的编程。我们用Python创建了一个很好的基本文本冒险游戏,它的工作原理和它应该的一样。然而,她真的很想以图形化的方式看到它,所以我尝试使用我对Tkinter极其基本的知识来实现这一点。每一章都是从上一章复制粘贴的,按钮标签和链接、文本框内容和图像引用都发生了变化。我已经将每个单独的屏幕放置到一个单独的函数中,该函数通过单击上一屏幕上的按钮来调用。然而,我的问题是,它们不是用新的屏幕替换屏幕,而是相互重叠。所以第二章的图像、按钮和文本就放在第一章的下面 我想不

我一直在教一个家庭朋友的女儿一些基本的编程。我们用Python创建了一个很好的基本文本冒险游戏,它的工作原理和它应该的一样。然而,她真的很想以图形化的方式看到它,所以我尝试使用我对Tkinter极其基本的知识来实现这一点。每一章都是从上一章复制粘贴的,按钮标签和链接、文本框内容和图像引用都发生了变化。我已经将每个单独的屏幕放置到一个单独的函数中,该函数通过单击上一屏幕上的按钮来调用。然而,我的问题是,它们不是用新的屏幕替换屏幕,而是相互重叠。所以第二章的图像、按钮和文本就放在第一章的下面

我想不出一种方法来删除以前的框架和小部件,并用新的替换它们。我试着用几种方法杀死他们,但要么什么也不做,要么杀死整个程序。我也尝试过在每个函数中使用相同的按钮名称,但因为它们是本地的,所以没有什么区别

虽然我意识到这是低效的,但如果可能的话,我想尽量避免使用对象,因为向她解释会非常困难

有没有办法通过调整我创建的结构来做到这一点,或者只能通过使用复杂的类和对象来做到这一点?请记住,这段代码永远不会被重用,所以不管它在后台有多混乱都无关紧要

from tkinter import *


root = Tk()

def intro():


  topFrame = Frame(root)
  topFrame.pack()
  bottomFrame = Frame(root)
  bottomFrame.pack(side = BOTTOM)

  photo = PhotoImage(file="hh.gif")
  label = Label(root, image=photo)
  label.pack(side = TOP)

  t = Text(wrap=WORD, height = 10)
  t.insert(INSERT, "This is the first screen")
  t.insert(END, "")
  t.pack()

  # button 1 here should take us to the second screen
  button1 = Button(bottomFrame, text="Option 1", fg="black", command=chapter2)
  button2 = Button(bottomFrame, text="Option 2", fg="black")
  button3 = Button(bottomFrame, text="Option 3", fg="black")

  button1.pack(side = LEFT)
  button2.pack(side = LEFT)
  button3.pack(side = LEFT)

  root.mainloop()


def chapter2():


  topFrame = Frame(root)
  topFrame.pack()
  bottomFrame = Frame(root)
  bottomFrame.pack(side = BOTTOM)

  photo1 = PhotoImage(file="hh2.gif")
  label1 = Label(root, image=photo1)
  label1.pack(side = TOP)

  t1 = Text(wrap=WORD)
  t1.insert(INSERT, "This is the second screen")
  t1.insert(END, "")
  t1.pack()

  button4 = Button(bottomFrame, text="this", fg="black")
  button5 = Button(bottomFrame, text="that", fg="black")
  button6 = Button(bottomFrame, text="the other", fg="black")

  button4.pack(side = LEFT)
  button5.pack(side = LEFT)
  button6.pack(side = LEFT)

  root.mainloop()

  intro()

代码中出现的情况是,您正在创建新的文本字段和标签,而没有删除旧的文本字段和标签

要删除旧的文本/标签小部件,请使用
pack\u-forget()
,在您的情况下,
t.pack\u-forget()
label.pack\u-forget()
button1.pack\u-forget()
button2.pack\u-forget()
,以及
button3.pack\u-forget()
pack\u-forget()
所做的是从窗口移动一个小部件,就像
pack()
的反面一样。因此,要将其添加到代码中,只需更改第24行:

button1 = Button(bottomFrame, text="Option 1", fg="black", command=chapter2  

注意在第二章中,您将使用
t1.pack_-forget()
label1.pack_-forget()
按钮4/5/6.pack_-forget()
t/label/button1/2/3.pack_-forget()

您还必须在标签上保留对您照片的引用,您可以通过在
label=label(顶框,image=photo)
label.pack(side=TOP)
之间添加
label.image=photo
来实现这一点。另请参见第页。
整个编辑代码:

from tkinter import *


root = Tk()

def intro():

  topFrame = Frame(root)
  topFrame.pack()
  bottomFrame = Frame(root)
  bottomFrame.pack(side=BOTTOM)

  photo = PhotoImage(file="hh.gif")
  label = Label(root, image=photo)
  label.image = photo # keep a reference!
  label.pack(side=TOP)

  t = Text(wrap=WORD, height=10)
  t.insert(INSERT, "This is the first screen")
  t.insert(END, "")
  t.pack()

  # button 1 here should take us to the second screen
  button1 = Button(bottomFrame, text="Option 1", fg="black", command=lambda: (t.pack_forget(), label.pack_forget(), button1.pack_forget(), button2.pack_forget(), button3.pack_forget(), chapter2()))
  button2 = Button(bottomFrame, text="Option 2", fg="black")
  button3 = Button(bottomFrame, text="Option 3", fg="black")

  button1.pack(side=LEFT)
  button2.pack(side=LEFT)
  button3.pack(side=LEFT)

def chapter2():

  topFrame = Frame(root)
  topFrame.pack()
  bottomFrame = Frame(root)
  bottomFrame.pack(side=BOTTOM)

  photo1 = PhotoImage(file="hh2.gif")
  label1 = Label(root, image=photo1)
  label.image = photo # keep a reference!
  label1.pack(side=TOP)

  t1 = Text(wrap=WORD)
  t1.insert(INSERT, "This is the second screen")
  t1.insert(END, "")
  t1.pack()

  button4 = Button(bottomFrame, text="this", fg="black")
  button5 = Button(bottomFrame, text="that", fg="black")
  button6 = Button(bottomFrame, text="the other", fg="black")

  button4.pack(side=LEFT)
  button5.pack(side=LEFT)
  button6.pack(side=LEFT)

intro()

root.mainloop()
希望这有帮助,如果没有,请发表评论,我将尝试找到另一个解决方案:)


另外,如果您想知道lambda是什么(编辑代码的一部分),只需在谷歌上搜索“python lambda”。

代码中发生的事情是,您正在创建新的文本字段和标签,而没有删除旧的文本字段和标签

要删除旧的文本/标签小部件,请使用
pack\u-forget()
,在您的情况下,
t.pack\u-forget()
label.pack\u-forget()
button1.pack\u-forget()
button2.pack\u-forget()
,以及
button3.pack\u-forget()
pack\u-forget()
所做的是从窗口移动一个小部件,就像
pack()
的反面一样。因此,要将其添加到代码中,只需更改第24行:

button1 = Button(bottomFrame, text="Option 1", fg="black", command=chapter2  

注意在第二章中,您将使用
t1.pack_-forget()
label1.pack_-forget()
按钮4/5/6.pack_-forget()
t/label/button1/2/3.pack_-forget()

您还必须在标签上保留对您照片的引用,您可以通过在
label=label(顶框,image=photo)
label.pack(side=TOP)
之间添加
label.image=photo
来实现这一点。另请参见第页。
整个编辑代码:

from tkinter import *


root = Tk()

def intro():

  topFrame = Frame(root)
  topFrame.pack()
  bottomFrame = Frame(root)
  bottomFrame.pack(side=BOTTOM)

  photo = PhotoImage(file="hh.gif")
  label = Label(root, image=photo)
  label.image = photo # keep a reference!
  label.pack(side=TOP)

  t = Text(wrap=WORD, height=10)
  t.insert(INSERT, "This is the first screen")
  t.insert(END, "")
  t.pack()

  # button 1 here should take us to the second screen
  button1 = Button(bottomFrame, text="Option 1", fg="black", command=lambda: (t.pack_forget(), label.pack_forget(), button1.pack_forget(), button2.pack_forget(), button3.pack_forget(), chapter2()))
  button2 = Button(bottomFrame, text="Option 2", fg="black")
  button3 = Button(bottomFrame, text="Option 3", fg="black")

  button1.pack(side=LEFT)
  button2.pack(side=LEFT)
  button3.pack(side=LEFT)

def chapter2():

  topFrame = Frame(root)
  topFrame.pack()
  bottomFrame = Frame(root)
  bottomFrame.pack(side=BOTTOM)

  photo1 = PhotoImage(file="hh2.gif")
  label1 = Label(root, image=photo1)
  label.image = photo # keep a reference!
  label1.pack(side=TOP)

  t1 = Text(wrap=WORD)
  t1.insert(INSERT, "This is the second screen")
  t1.insert(END, "")
  t1.pack()

  button4 = Button(bottomFrame, text="this", fg="black")
  button5 = Button(bottomFrame, text="that", fg="black")
  button6 = Button(bottomFrame, text="the other", fg="black")

  button4.pack(side=LEFT)
  button5.pack(side=LEFT)
  button6.pack(side=LEFT)

intro()

root.mainloop()
希望这有帮助,如果没有,请发表评论,我将尝试找到另一个解决方案:)


此外,如果您想知道lambda是什么(编辑代码的一部分),只需在谷歌上搜索“python lambda”。

以mocqoro的答案为基础,我建议您将每一章放在一个框架中。这样,当你想进入下一章时,你只需
pack\u-forget
destroy
pack\u-forget
有助于你以后重新打包小部件,
destroy
完全删除小部件)章节的框架,而不是每个小部件:

from tkinter import *

def intro():
    chapterFrame = Frame(root) 
    chapterFrame.pack()

    topFrame = Frame(chapterFrame) # top and bottom frames are children of chapterFrame
    topFrame.pack()

    photo = PhotoImage(file="hh.gif")
    label = Label(topFrame, image=photo)
    label.image = photo
    label.pack(side=TOP)

    t = Text(topFrame, wrap=WORD, height=10)
    t.insert(INSERT, "This is the first screen")
    t.pack()

    bottomFrame = Frame(chapterFrame)
    bottomFrame.pack(side=BOTTOM)

    # button 1 here should take us to the second screen
    # button1 deletes the entire chapter and calls chapter2
    button1 = Button(bottomFrame, text="Option 1", fg="black", command=lambda: (chapterFrame.destroy(), chapter2()))
    button2 = Button(bottomFrame, text="Option 2", fg="black")
    button3 = Button(bottomFrame, text="Option 3", fg="black")

    button1.pack(side=LEFT)
    button2.pack(side=LEFT)
    button3.pack(side=LEFT)

def chapter2():
    chapterFrame = Frame(root)
    chapterFrame.pack()

    topFrame = Frame(chapterFrame)
    topFrame.pack()

    photo1 = PhotoImage(file="hh2.gif")
    label1 = Label(topFrame, image=photo1)
    label1.image = photo1
    label1.pack(side = TOP)

    t1 = Text(topFrame, wrap=WORD)
    t1.insert(INSERT, "This is the second screen")
    t1.pack()

    bottomFrame = Frame(chapterFrame)
    bottomFrame.pack(side=BOTTOM)

    button4 = Button(bottomFrame, text="this", fg="black")
    # button5 loops back to intro
    button5 = Button(bottomFrame, text="that", fg="black", command=lambda: (chapterFrame.destroy(), intro()))
    button6 = Button(bottomFrame, text="the other", fg="black")

    button4.pack(side=LEFT)
    button5.pack(side=LEFT)
    button6.pack(side=LEFT)

root = Tk()
intro()
root.mainloop()

根据莫库罗的回答,我建议你把每一章都放在一个框架中。这样,当你想进入下一章时,你只需
pack\u-forget
destroy
pack\u-forget
有助于你以后重新打包小部件,
destroy
完全删除小部件)章节的框架,而不是每个小部件:

from tkinter import *

def intro():
    chapterFrame = Frame(root) 
    chapterFrame.pack()

    topFrame = Frame(chapterFrame) # top and bottom frames are children of chapterFrame
    topFrame.pack()

    photo = PhotoImage(file="hh.gif")
    label = Label(topFrame, image=photo)
    label.image = photo
    label.pack(side=TOP)

    t = Text(topFrame, wrap=WORD, height=10)
    t.insert(INSERT, "This is the first screen")
    t.pack()

    bottomFrame = Frame(chapterFrame)
    bottomFrame.pack(side=BOTTOM)

    # button 1 here should take us to the second screen
    # button1 deletes the entire chapter and calls chapter2
    button1 = Button(bottomFrame, text="Option 1", fg="black", command=lambda: (chapterFrame.destroy(), chapter2()))
    button2 = Button(bottomFrame, text="Option 2", fg="black")
    button3 = Button(bottomFrame, text="Option 3", fg="black")

    button1.pack(side=LEFT)
    button2.pack(side=LEFT)
    button3.pack(side=LEFT)

def chapter2():
    chapterFrame = Frame(root)
    chapterFrame.pack()

    topFrame = Frame(chapterFrame)
    topFrame.pack()

    photo1 = PhotoImage(file="hh2.gif")
    label1 = Label(topFrame, image=photo1)
    label1.image = photo1
    label1.pack(side = TOP)

    t1 = Text(topFrame, wrap=WORD)
    t1.insert(INSERT, "This is the second screen")
    t1.pack()

    bottomFrame = Frame(chapterFrame)
    bottomFrame.pack(side=BOTTOM)

    button4 = Button(bottomFrame, text="this", fg="black")
    # button5 loops back to intro
    button5 = Button(bottomFrame, text="that", fg="black", command=lambda: (chapterFrame.destroy(), intro()))
    button6 = Button(bottomFrame, text="the other", fg="black")

    button4.pack(side=LEFT)
    button5.pack(side=LEFT)
    button6.pack(side=LEFT)

root = Tk()
intro()
root.mainloop()

您还可以删除每个函数中的
root.mainloop()
,并在
intro()
@PRMoureu之后只添加一个可能有效的函数。。。抱歉,我不太明白你的意思,但它可能会起作用:)tkinter在这种情况下只需要一个主循环,在最后,在
intro()之后。
@PRMoureu噢,明白了。是的,那是真的,我以为你的意思完全不同,谢谢!我应该编辑它还是你应该提出编辑请求?(自从你告诉我以后)@PRMoureu:在这种情况下不是
,而是总是这样。Tkinter只需要一次
Tk()
和一次
mainloop()
您还可以删除每个函数中的
root.mainloop()
,并在
intro()
@PRMoureu之后只添加一个可能有效的函数。。。抱歉,我不太明白你的意思,但它可能会起作用:)tkinter在这种情况下只需要一个主循环,在最后,在
intro()之后。
@PRMoureu噢,明白了。是的,那是真的,我以为你的意思完全不同,谢谢!我应该编辑它还是应该编辑它