_tkinter.TclError:无法调用“按钮”命令:应用程序已被破坏

时间:2018-10-15 13:39:50

标签: python-3.x tkinter

我正在尝试创建一个菜单窗口,该窗口选择所需的计算器类型。当我单击该按钮时,它应该会打开一个新的窗口进行计算。但是当我尝试在该窗口中放置一个按钮时,它将给出标题中给出的错误。有人可以帮我解决此问题。非常感谢

from tkinter import *
from tkinter import messagebox
import sys

cca=0

def cc():
    global cca
    cca=1
    menu.destroy()

class Cal:
    def __init__(self,cal):
        self.root=Tk()
        self.root.geometry("400x400")
        self.root.title("Currency Converter")

        self.us=StringVar()
        self.usd=Entry(self.root,textvariable=self.us)
        self.usd.grid(row=3,column=1)
        self.us.set("0")

        self.rate=StringVar()
        self.raten=Entry(self.root,textvariable=self.rate)
        self.raten.grid(row=4,column=1)
        self.rate.set("0")

        self.buttona=Button(self.root,text="-->",command=self.convert1).grid(row=4,column=2,rowspan=2)
        self.buttonb=Button(self.root,text="<--",command=self.convert2).grid(row=6,column=2,rowspan=2)
        self.buttonc=Button(self.root,text="Quit",command=self.root.quit).grid(row=8,column=2)

        self.china=StringVar()
        self.china.set("0")
        self.chn=Entry(self.root,textvariable=self.china)
        self.chn.grid(row=3, column=2)
        self.root.mainloop()

    def convert1(self):
        cal=float(self.rate.get())
        u=eval(self.us.get())
        c=cal*u
        cc=round(c,4)
        self.china.set(cc)

    def convert2(self):
        cal=float(self.rate.get())
        s=eval(self.china.get())
        ss=s/cal
        sss=round(ss,2)
        self.us.set(sss)

menu=Toplevel()
menu.geometry("400x400")
menu.title("UniCal 1.0")
menu.mainloop()

buttoncc=Button(menu,text="Currency Converter",command=cc).grid(row=3,column=4)
if cca==1:
    lol=Cal(5)

2 个答案:

答案 0 :(得分:4)

您的错误是您在创建buttoncc之前先调用menu.mainloop(),然后两次调用mainloop()。

最好是这样创建一个顶级窗口:

from tkinter import *
from tkinter import messagebox
import sys

class Cal:
    def __init__(self, master, cal):
        self.master = master
        self.root = Toplevel(self.master)
        self.root.title("Currency Converter")
        self.root.geometry("400x400")

        self.us=StringVar()
        self.usd=Entry(self.root,textvariable=self.us)
        self.usd.grid(row=3,column=1)
        self.us.set("0")
        self.rate=StringVar()
        self.raten=Entry(self.root,textvariable=self.rate)
        self.raten.grid(row=4,column=1)
        self.rate.set("0")
        self.buttona=Button(self.root,text="-->",command=self.convert1).grid(row=4,column=2,rowspan=2)
        self.buttonb=Button(self.root,text="<--",command=self.convert2).grid(row=6,column=2,rowspan=2)
        self.buttonc=Button(self.root,text="Quit",command=self.root.quit).grid(row=8,column=2)
        self.china=StringVar()
        self.china.set("0")
        self.chn=Entry(self.root,textvariable=self.china)
        self.chn.grid(row=3, column=2)

    def convert1(self):
        cal=float(self.rate.get())
        u=eval(self.us.get())
        c=cal*u
        cc=round(c,4)
        self.china.set(cc)

    def convert2(self):
        cal=float(self.rate.get())
        s=eval(self.china.get())
        ss=s/cal
        sss=round(ss,2)
        self.us.set(sss)

class My_Root(Frame):

    def __init__(self, master):
        self.master = master
        Frame.__init__(self, self.master)
        self.grid()
        self.buttoncc=Button(self,text="Currency Converter",command=self.new_toplevel)
        self.buttoncc.grid(row=0,column=0)

    def new_toplevel(self):
        self.myToplevel = Cal(self, 5)

if __name__ == "__main__":

    root=Tk()
    root.geometry("400x400")
    root.title("UniCal 1.0")   
    app = My_Root(root)
    root.mainloop()

答案 1 :(得分:0)

这是当前代码

import os
import pickle
import tkinter as tk
from tkinter import filedialog
from tkinter import PhotoImage
from pygame import mixer

class Player(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.master = master
self.pack()

self.playlist = []

self.create_frames()

def create_frames(self):
self.track = tk.LabelFrame(self, text = 'Song Track',
    font = ("times new roman",15,"bold"),
    bg="grey", fg="white", bd=5, relief = tk.GROOVE)
self.track.configure(width=410, height=300)
self.track.grid(row=0, column =0)

self.tracklist = tk.LabelFrame(self, text = 'Song Track',
    font = ("times new roman",15,"bold"),
    bg="grey", fg="white", bd=5, relief=tk.GROOVE)
self.tracklist.configure(width=190, height=400)
self.tracklist.grid(row=0, column=1, rowspan=3)
    
self.controls = tk.LabelFrame(self, text = 'Song Track',
    font=("times new roman",15,"bold"),
    bg="grey", fg="white", bd=5, relief=tk.GROOVE)
self.track.configure(width=410, height=300)
self.track.grid(row=0, column=0)
    
    
root = tk.Tk()
root.geometry('600x400')
root.wm_title('Music Player')
root.mainloop()


app = Player(master=root)
app.mainloop()

错误消息:

File "h:/MusicPlayer/musicplayer.py", line 44, in <module>
app = Player(master=root)
File "h:/MusicPlayer/musicplayer.py", line 10, in __init__
super().__init__(master)
File "C:\Users\--\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", 
line 
3119, in __init__
Widget.__init__(self, master, 'frame', cnf, {}, extra)
File "C:\Users\--\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", 
line 
2567, in __init__
self.tk.call(
_tkinter.TclError: can't invoke "frame" command: application has been destroyed