Вопрос

in the code below i would like to destroy everything inside the window root when the GameButton button is pressed, however i would like other things to happen so the only way to do this would be for the button to run a function. When i perform self.destroy outside of the main class, nothing is deleted, is there any way around this?

from PIL import Image, ImageTk
from Tkinter import Tk, Label, BOTH,W, N, E, S, Entry, Text, INSERT, Toplevel
from ttk import Frame, Style, Button, Label
import Tkinter
import Callingwordlist
difficulty = ""

class MainMenuUI(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)   

        self.parent = parent

        self.initUI()

    def initUI(self):

        self.parent.title("Type!")
        self.pack(fill=BOTH, expand=1)

        style = Style()
        style.configure("TFrame", background="black")        

        Logo = Image.open("Type!.png")
        TypeLogo = ImageTk.PhotoImage(Logo)
        label1 = Label(self, image=TypeLogo)
        label1.image = TypeLogo
        label1.place(x=342,y=80)
        label1.pack()

        self.pack(fill=BOTH, expand=1)

        GameButton = Button(self, text="Main Game", command=lambda: main2(self.parent,self))
        GameButton.pack()
        GameButton.place(x=344,y=200,height = 80,width = 176)

        TutorialButton = Button(self,text="Tutorial Level")
        TutorialButton.pack()
        TutorialButton.place(x=344, y=300 ,height = 80,width = 176)

        quitbutton = Button(self, text= "Quit",command=self.parent.destroy)
        quitbutton.place(x=344, y=400,height = 80,width = 176)

def main2(root,self):
    self.destroy
    app = MainGameUI(root)
    root.mainloop()

class MainGameUI(root):
    ....

def main():

    root = Tk()
    root.geometry("860x640+300+300")
    app = MainMenuUI(root) 
    root.mainloop()

if __name__ == '__main__':
    main()
Это было полезно?

Решение

You aren't actually destroying anything in your function. Look at this code:

def main2(root,self):
    self.destroy
    app = MainGameUI(root)
    root.mainloop()

Notice the first line in the function where you are trying to destroy everything. Your code is self.destroy - notice the lack of parenthesis. You aren't actually calling the function, you are simply referencing it. Add parentheses to call it: self.destroy().

You have another problem in that you're calling a function that destroys the widget that calls the function. However, this function enters into an endless loop (mainloop()), so the button command will never return. I'm not entirely sure what will happen here, you'll probably get some sort of error. The bottom line is, calling mainloop from a button command is not a good idea.

Since you are structuring your app such that the app is a frame (rather than the root window) you don't need to restart the event loop. When you destroy the MainMenuUI widget, the event loop will continue to run. There's no need to restart it.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top