[英]_tkinter.TclError: bad window path name ".!button2"
I'm trying to run this code but it's giving "_tkinter.TclError: bad window path name ".!button2" " this error.我正在尝试运行此代码,但它给出了 "_tkinter.TclError: bad window path name ".!button2" " 这个错误。 But If I run the same code without ebook function, it's working fine.
但是如果我在没有电子书功能的情况下运行相同的代码,它就可以正常工作。 As I'm new to coding, any help will be appreciated.
由于我是编码新手,任何帮助将不胜感激。
from tkinter import *
root = Tk()
root.title("Application")
root.resizable(width=False, height=False)
mylabel0= Label(root, text=" ")
mylabel0.grid(row=0, column=0)
def ebook():
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from gtts import gTTS
import pdfplumber
book = tk.Tk()
book.minsize(width=150, height=200)
book.maxsize(width=300, height=420)
canvas1 = tk.Canvas(book, width=300, height=420, bg='azure3', relief='raised')
canvas1.grid()
label1 = tk.Label(book, text="PDF Audio Store", bg='azure3')
label1.config(font=('helvetica', 16))
canvas1.create_window(150, 20, window=label1)
final = ""
def get_pdf():
global final
global pdf_checker
try:
import_file_path = filedialog.askopenfilename()
if (str(import_file_path).endswith(".pdf")):
pdf = pdfplumber.open(import_file_path)
pdf_checker = pdf
n = len(pdf.pages)
for page in range(n):
data = pdf.pages[page].extract_text()
final = final + "\n" + data
messagebox.showinfo("Information", "PDF is imported")
else:
raise Exception("Provide .pdf file only")
except Exception as e:
messagebox.showerror("Error", "{}".format(e))
else:
print("Continue to conversion")
brows_pdf = tk.Button(text="PDF entry", command=get_pdf, bg='royalblue', fg="white", font=('helvetica', 12, 'bold'),
border=0,
activebackground="green")
It's giving error in this(👇) area specifically, create_window.它在这个(👇)区域中给出了错误,特别是 create_window。 Error Image
错误图像
canvas1.create_window(150, 60, window=brows_pdf)
def convert_audio():
global final
global pdf_checker
try:
print("File Information")
print(pdf_checker)
export_file_path = filedialog.asksaveasfilename(defaultextension=".mp3")
final_file = gTTS(text=final, lang="en")
final_file.save(export_file_path)
messagebox.showinfo("Information", "Audio file generated")
except NameError:
messagebox.showwarning("Warning", "Import PDF first")
audio_button = tk.Button(text="Convert to Audio", command=convert_audio, bg='royalblue', fg="white",
font=('helvetica', 12, 'bold'),
border=0,
activebackground="green")
canvas1.create_window(150, 100, window=audio_button)
book.mainloop()
button_ebook= Button(root, text="E-Book📖", font=30, padx=25, pady=25, command=ebook, fg="black", bg="white", borderwidth=1)
button_ebook.grid(row=3, column=2, padx=10)
root.mainloop()
The root of the problem is that you can't use a button in one window that is a descendant of another window.问题的根源在于您不能在作为另一个窗口的后代的一个窗口中使用按钮。
The first problem is that you are creating two instances of Tk
.第一个问题是您正在创建
Tk
两个实例。 Buttons created in one instance are not available in the other.在一个实例中创建的按钮在另一个实例中不可用。 In this case, your button is in the original instance of
Tk
but your canvas is in the other.在这种情况下,您的按钮在
Tk
的原始实例中,而您的画布在另一个实例中。
The second problem is similar.第二个问题类似。 Even when replacing the second instance of
Tk
, the button is in the root window and thus can't be used as a child of the canvas which is in the second window.即使在替换
Tk
的第二个实例时,该按钮也位于根窗口中,因此不能用作位于第二个窗口中的画布的子窗口。
The solution is to use Toplevel
for the second window, and to make sure the button is a descendant of the second window and/or the canvas.解决方案是将
Toplevel
用于第二个窗口,并确保按钮是第二个窗口和/或画布的后代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.