我收到一条错误消息,说应用程序已被破坏。我已经收到了关于早期不完整版本的代码的回复,该版本没有解决问题,所以我有点担心。我是国际米兰的新手,所以我不太擅长这个。
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()
如果这不是你想要的答案,请纠正我。
运行此代码时收到的错误为_tkinter.TclError: can't invoke "button" command: application has been destroyed
。此错误仅在关闭应用程序时出现。按钮根本没有出现。根据这个堆栈溢出问题,出现错误是因为您在创建按钮之前调用了root.mainloop()
。
在我解决了这个问题之后,我不得不重新安排函数checkDirections()
。
然后得到错误_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack
。根据这个堆栈溢出问题:
错误会告诉你到底出了什么问题:你不能将pack和grid都与共享公共父的小部件一起使用
在这一点上,我不能再往下说了,因为我从未使用过pack、numpy和matplotlib,我不知道你希望你的窗口布局如何。
这就是我得到_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack
错误的地方,我希望你能从这里继续:
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)
如果这不是问题所在,那么就用实际问题来评论这个答案。希望这能有所帮助!