Как вернуться в начало?
Хочу сделать так, чтобы при нажатии кнопки "Отменить" меня возвращало на стартовую страницу выбора файла, но как это сделать вообще? переструктурировать весь код?
import tkinter as tk
import tkinter.filedialog as fd
from tkinter import *
from PIL import Image,ImageTk
from rembg import remove
import os
import sys
window = Tk()
window.geometry('700x500')
window.title("ext")
window.configure(bg='#212121')
window.resizable (width=False, height=False)
def choose_file():
filetypes = ("Изображение", "*.jpg *.png"),
global filename
global basename
filename = fd.askopenfilename(title="Открыть файл", initialdir="/",
filetypes=filetypes)
input_path = (filename)
basename = os.path.basename(filename)
fixed_width = 450
new_res = Image.open(input_path)
width_percent = (fixed_width / float(new_res.size[0]))
height_size = int((float(new_res.size[1]) * float(width_percent)))
sh_res = new_res.resize((fixed_width, height_size))
global canvas_1
canvas_1 = Canvas(
window,
bg='#212121',
width = 450,
height = 350,
highlightthickness=0
)
canvas_1.pack()
img = ImageTk.PhotoImage(sh_res)
canvas_1.create_image(
0,
0,
anchor=NW,
image=img
)
canvas_1.place(relx=0.20, rely=0.02, anchor=NW)
global btn_obr
btn_obr = tk.Button(window,text="Обработать",
command=backre_file,
width = 20,
height = 3,
anchor=CENTER,
bg='#2F353B',
fg='white',
borderwidth=0,
font='helv39'
)
btn_obr.grid(column=0, row=1)
btn_obr.place(relx=0.35, rely=0.85, anchor=CENTER)
global btn_cancel
btn_cancel = tk.Button(window,text="Отменить",
command=(cancel),
width = 20,
height = 3,
anchor=CENTER,
bg='#2F353B',
fg='white',
borderwidth=0,
font='helv39'
)
btn_cancel.grid(column=0, row=1)
btn_cancel.place(relx=0.70, rely=0.85, anchor=CENTER)
print(btn_cancel)
canvas_1.mainloop()
def backre_file():
input = Image.open(filename)
global removebg
removebg = remove(input)
removebg.save('C:/Users/Nochka/Desktop/Ph/removebg.png', 'PNG')
fixed_width = 450
new_res = Image.open('C:/Users/Nochka/Desktop/Ph/removebg.png')
width_percent = (fixed_width / float(new_res.size[0]))
height_size = int((float(new_res.size[1]) * float(width_percent)))
sh_res = new_res.resize((fixed_width, height_size))
canvas = Canvas(
window,
bg='#212121',
width = 450,
height = 350,
highlightthickness=0
)
canvas.pack()
img = ImageTk.PhotoImage(sh_res)
canvas.create_image(
0,
0,
anchor=NW,
image=img
)
canvas.place(relx=0.20, rely=0.02, anchor=NW)
btn_save = tk.Button(window,text="Сохранить",
command=(save_file),
width = 20,
height = 3,
anchor=CENTER,
bg='#2F353B',
fg='white',
borderwidth=0,
font='helv39'
)
btn_save.grid(column=0, row=1)
btn_save.place(relx=0.35, rely=0.85, anchor=CENTER)
canvas.mainloop()
def save_file():
filetypes = ("Изображение", "*.jpg *.png"),
global s_file
s_file = fd.asksaveasfilename(title="Сохранить Файл",defaultextension="png", initialfile="basename", initialdir="/",
filetypes=filetypes)
print(s_file)
removebg.save(s_file)
btn_file = tk.Button(window,text="Выбрать файл",
command=choose_file,
width = 40,
height =10,
anchor=CENTER)
btn_file.grid(column=0, row=1)
btn_file.place(relx=0.5, rely=0.5, anchor=CENTER)
def cancel():
btn_obr.destroy()
btn_cancel.destroy()
canvas_1.quit()
print(btn_cancel)
btn_file.destroy()
btn_file2 = tk.Button(window,text="Выбрать файл",
command=choose_file,
width = 40,
height =10,
anchor=CENTER)
btn_file2.grid(column=0, row=1)
btn_file2.place(relx=0.5, rely=0.5, anchor=CENTER)
window.mainloop()