-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_widgets.py
93 lines (73 loc) · 3.69 KB
/
common_widgets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import tkinter as tk
from PIL import Image, ImageTk
from tkinter import filedialog as fd
from tkinter.messagebox import showinfo
import os
class TexturePreview(tk.LabelFrame):
def __init__(self, app, text, im_size):
super().__init__(app, text=text)
self.is_valid_file = tk.BooleanVar(value=False)
self.im_size = im_size
self.im = Image.new('RGBA', (self.im_size, self.im_size), (0, 0, 0, 0))
self.im = self.im.resize((self.im_size, self.im_size))
self.imtk = ImageTk.PhotoImage(self.im)
self.path_entry = tk.Entry(self)
self.path_entry.grid(row=0,column=0,sticky='we')
self.path_entry.bind("<KeyRelease>", self.on_path_entry_change)
self.examine_button = tk.Button(self, text='Examine',command=self.load_path)
self.examine_button.grid(row=0, column=1, sticky='we')
self.preview = tk.Label(self,text='Futuro logo', image=self.imtk)
self.preview.grid(row=1,column=0, columnspan=2,sticky='we')
self.prev_button = tk.Button(self, text='Preview',command=self.load_image)
self.prev_button.grid(row=2, column=0, columnspan=2, sticky='we')
def load_image(self):
self.im = Image.open(self.path_entry.get())
self.im = self.im.resize((self.im_size, self.im_size))
self.imtk = ImageTk.PhotoImage(self.im)
self.preview.configure(image=self.imtk)
def blank_image(self):
self.im = Image.new('RGBA', (self.im_size, self.im_size), (0, 0, 0, 0))
self.im = self.im.resize((self.im_size, self.im_size))
self.imtk = ImageTk.PhotoImage(self.im)
self.preview.configure(image=self.imtk)
def load_path(self):
# -defaultextension, -filetypes, -initialdir, -initialfile, -multiple, -parent, -title, or -typevariable
path = fd.askopenfilename(filetypes=[("PNG Files", "*.png"),("Texture Files", "*.tiff"), ("All Files", "*.*")])
if path:
self.path_entry.delete(0, tk.END)
self.path_entry.insert(0, path)
else:
showinfo("Error", "Error al cargar la imagen")
def on_path_entry_change(self, event):
try :
self.load_image()
self.is_valid_file.set(os.path.isfile(self.path_entry.get()))
print('Is valid file', self.is_valid_file,self.is_valid_file.get())
except Exception as e:
self.is_valid_file.set(False)
self.blank_image()
print(e)
class SaveTexture(tk.LabelFrame):
def __init__(self, app, text, button_text, im_preview, im, size_selected):
super().__init__(app, text=text)
self.preview = im_preview
self.im = im
self.size = size_selected
self.imtk = ImageTk.PhotoImage(im_preview)
self.image = tk.Label(self, image=self.imtk)
self.image.grid(row=0,column=0,sticky='we')
self.save_button = tk.Button(self, text=button_text,command=self.save_path)
self.save_button.grid(row=1, column=0, columnspan=2, sticky='we')
def save_path(self):
# -defaultextension, -filetypes, -initialdir, -initialfile, -multiple, -parent, -title, or -typevariable
path = fd.asksaveasfilename(filetypes=[("Image Files", "*.png;*.jpg;*.tiff"),("PNG Files", "*.png"),("Texture Files", "*.tiff"), ("All Files", "*.*")])
if path:
self.save_im = self.im.resize((self.size, self.size))
self.save_im.save(path)
else:
showinfo("Error", "Error al guardar imagen. Por favor, selecciona la ruta de guardado.")
def on_update(self, im, im_preview, event):
self.im = im
self.preview = im_preview
self.imtk = ImageTk.PhotoImage(im_preview)
self.image.configure(image=self.imtk)