0

I am receiving an error message that says that the application has been destroyed. I already got a response on an earlier incomplete version of the code that didn't fix the issue so I am getting a little concerned. I am very new to tkinter so I'm not too good at this.

from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from PIL import ImageTk, Image
import numpy as np
import matplotlib.pyplot as plt
import os
import glob

root = Tk()

e = Entry(root, width = 50)
e.pack()
root.title("Insert GUI Title Here")
root.geometry('600x400+50+50')
root.resizable(False, False)

nameDirections = []
directions = []



def openSmFile():
    folPath = filedialog.askdirectory()
    return folPath


root.mainloop()


def checkDirections():
    folPath = openSmFile()
    for fpath in glob.iglob(f'{folPath}/*'):
        if (fpath.endswith('.sm')):
            file = open(fpath,"r")
            lines = []
            lines = file.readlines()

            left = 0
            down = 0
            up = 0
            right = 0
            beats = 0

            for line in lines:  
                i = 0
                if not ("," in  line or "." in line or "#" in line or ";" in line or "-" in line or line == ""):
                    for alpha in line:
                        if i == 0 and alpha != "0":
                            left += 1
                        if i == 1 and alpha != "0":
                            down += 1
                        if i == 2 and alpha != "0":
                            up += 1
                        if i == 3 and alpha != "0":
                            right += 1
                        i += 1
                        beats += 1
                
            print ("There are " + str(left) + " lefts in this song.")
            print ("There are " + str(down) + " downs in this song.")
            print ("There are " + str(up) + " ups in this song.")
            print ("There are " + str(right) + " rights in this song.")
            print ("There are " + str(beats) + " beats.")
            nameDirections = ["left", "down", "up", "right"]
            directions = [left, down, up, right] 


       

runThrough = Button(
    root,
    padx=50, 
    pady=50, 
    text="Click to print number of each arrow", 
    command=checkDirections
)
runThrough.pack()
runThrough.grid(row = 0, column = 0)

def barGraph():
    LabelBar = Label(root, text = "Bar Activated")
    LabelBar.pack()
    fig = plt.figure()
    plt.title("Directions")
    ax = fig.add_axes([0,0,1,1])
    ypos = np.arange(len(nameDirections))
    plt.bar(nameDirections, directions)
    plt.show()

def ILovePie():
    LabelPie = Label(root, text = "Pie Activated")
    LabelPie.pack()
    plt.title("Directions")
    fig = plt.figure()
    ax = fig.add_axes([0,0,1,1])
    ypos = np.arange(len(nameDirections))
    plt.pie(directions, labels = nameDirections,autopct='%1.1f%%',
    shadow=True, startangle=90)
    plt.show()

barGraph = Button(root, text = "Click to show a bar graph", padx = 50, pady = 50, command = barGraph())
barGraph.grid(row = 1, column = 5)

runThrough.pack()



    
acw1668
  • 40,144
  • 5
  • 22
  • 34

1 Answers1

0

Correct me if this isn't the answer you're looking for.

The error I recive when running this code is _tkinter.TclError: can't invoke "button" command: application has been destroyed. This error only appears when closing the application. The button doesn't appear at all. According to this stack overflow question the error occurs because you call root.mainloop() before creating the button.

After I fixed that I had to rearrange the function checkDirections().

Then you get the error _tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack. According to this stack overflow question:

The error is telling you exactly what is wrong: you can't use both pack and grid with widgets that share a common parent

At this point I can't go furthur as I never used pack, numpy and matplotlib and I don't know how you want you window layed out.

This is where I got the _tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack error and I hope you can continue from here:

from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from PIL import ImageTk, Image
import numpy as np
import matplotlib.pyplot as plt
import os
import glob

root = Tk()

e = Entry(root, width=50)
e.pack()
root.title("Insert GUI Title Here")
root.geometry('600x400+50+50')
root.resizable(False, False)

nameDirections = []
directions = []


def openSmFile():
    folPath = filedialog.askdirectory()
    return folPath

def checkDirections():
    folPath = openSmFile()
    for fpath in glob.iglob(f'{folPath}/*'):
        if (fpath.endswith('.sm')):
            file = open(fpath, "r")
            lines = []
            lines = file.readlines()

            left = 0
            down = 0
            up = 0
            right = 0
            beats = 0

            for line in lines:
                i = 0
                if not ("," in line or "." in line or "#" in line or ";" in line or "-" in line or line == ""):
                    for alpha in line:
                        if i == 0 and alpha != "0":
                            left += 1
                        if i == 1 and alpha != "0":
                            down += 1
                        if i == 2 and alpha != "0":
                            up += 1
                        if i == 3 and alpha != "0":
                            right += 1
                        i += 1
                        beats += 1

            print("There are " + str(left) + " lefts in this song.")
            print("There are " + str(down) + " downs in this song.")
            print("There are " + str(up) + " ups in this song.")
            print("There are " + str(right) + " rights in this song.")
            print("There are " + str(beats) + " beats.")
            nameDirections = ["left", "down", "up", "right"]
            directions = [left, down, up, right]



runThrough = Button(
    root,
    padx=50,
    pady=50,
    text="Click to print number of each arrow",
    command=checkDirections
)
runThrough.grid(row=1, column=1)
runThrough.pack()
root.mainloop()






def barGraph():
    LabelBar = Label(root, text="Bar Activated")
    LabelBar.pack()
    fig = plt.figure()
    plt.title("Directions")
    ax = fig.add_axes([0, 0, 1, 1])
    ypos = np.arange(len(nameDirections))
    plt.bar(nameDirections, directions)
    plt.show()


def ILovePie():
    LabelPie = Label(root, text="Pie Activated")
    LabelPie.pack()
    plt.title("Directions")
    fig = plt.figure()
    ax = fig.add_axes([0, 0, 1, 1])
    ypos = np.arange(len(nameDirections))
    plt.pie(directions, labels=nameDirections, autopct='%1.1f%%',
            shadow=True, startangle=90)
    plt.show()


barGraph = Button(root, text="Click to show a bar graph", padx=50, pady=50, command=barGraph())
barGraph.grid(row=1, column=5)

If this wasn't the probelm then comment on this answer with the actual issue. Hope this helps!

Thomas
  • 1,214
  • 4
  • 18
  • 45