I'm trying to run this code but it's giving "_tkinter.TclError: bad window path name ".!button2" " this error. 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.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()
Tk
. You shouldn't be creating more than one instance ofTk
.