Tkinter example 2
from tkinter import * from tkinter.ttk import * from tkinter.font import * from tkinter.simpledialog import * from tkinter.messagebox import * from tkinter.scrolledtext import ScrolledText import tkinterDnD from PIL import ImageTk, Image class TkTab(Notebook): def __init__(self,parent): super().__init__(parent) def AddItem(self,node,text): self.add(node,text=text) class TkSplitter(PanedWindow): def __init__(self,parent,orient=HORIZONTAL): super().__init__(parent,orient=orient) def AddItem(self,node): self.add(node) class TkLabel(Label): def __init__(self,parent,text=""): super().__init__(parent,text=text) class TkButton(Button): def __init__(self,parent,text="",handler=None,width=None): super().__init__(parent,text=text,command=handler,width=width) class TkText(ScrolledText): def __init__(self,parent,values=None,handler=None): super().__init__(parent) def Clear(self): self.delete("1.0",END); return self def AddText(self,value): self.insert(END,value); return self def SetText(self,value): self.Clear(); self.AddText(value); return self def GetText(self): return self.get("1.0",END) class TkScrolledList(Frame): def __init__(self,parent,handler=None): super().__init__(parent) frame = self self.listbox = Listbox(frame) self.listbox.config(selectmode='extended') self.listbox.config()#width=w,height=h) self.listbox.pack(side=LEFT,fill=Y,expand=True) if handler: self.listbox.bind('<<ListboxSelect>>', handler) scroll = Scrollbar(frame,orient=VERTICAL,command=self.listbox.yview) scroll.pack( side=RIGHT, fill=Y ) self.listbox.config( yscrollcommand = scroll.set) def AddItem(self,value,index=END): self.listbox.insert(index,value) class PilImage(): def __init__(self, image): if type(image) == str: self.image = Image.open(image) else: self.image = Image.new('P', image) newData = [(255, 255, 255, 0) for i in range(len(self.image.getdata()))] self.image.putdata(newData) def resize(self,size): width = size[0] radio = width / self.image.width height = int(radio * self.image.height) if height > size[1]: height = size[1] radio = height / self.image.height width = int(radio * self.image.width) self.image = self.image.resize((width,height), resample=0) return self def ListFrame(parent): global listbox frame = Frame(parent) sl = TkScrolledList(frame, list_selected) sl.pack(side=TOP,expand=True,fill=Y) makebtn = TkButton(frame, "만들기",make_handler,width=6) makebtn.pack(side=TOP,anchor=E,pady=10,padx=20) listbox = sl.listbox return frame def ImageFrame(parent): global image_label, text_edit frame = Frame(parent) addbtn = TkButton(frame, "추가",add_handler,width=6) addbtn.pack(side=BOTTOM,anchor=E,pady=5,padx=20) text_edit = TkText(frame) text_edit.config(height = 3) text_edit.pack(side=BOTTOM,anchor=S,fill=BOTH,pady=5) image_label = TkLabel(frame) image_label.config(bg='lightgrey') image_label.register_drop_target("*") image_label.bind("<<Drop>>",image_drop) image_label.pack(expand=True,fill=BOTH) return frame def list_selected(ev): global text_edit, image_label, page_list, page_item index = ev.widget.curselection()[0] image_label.config(image = page_list[index][1]) text_edit.SetText(page_list[index][2]) def image_drop(ev): global image_label, image_thumb, image_file, text_edit im = PilImage(ev.data).resize((image_label.winfo_width(), image_label.winfo_height())) image_thumb = ImageTk.PhotoImage(im.image) #Image.open("Lenna.png") image_label.config(image = image_thumb); #image_label.image = image_thumb image_file = ev.data def add_handler(): global image_label, image_thumb, image_file, text_edit, listbox, page_list page_item = [image_file, image_thumb, text_edit.GetText()] page_list.append( page_item ); print(page_list) listbox.insert(END,"Page " + str(len(page_list))) im = PilImage((image_label.winfo_width(), image_label.winfo_height())) image_label.config( image = ImageTk.PhotoImage(im.image) ) text_edit.Clear() def make_handler(): import pdf import json data = {} data['title'] = 'Album' data['pages'] = [] for page in page_list: data['pages'].append( { \ 'pictures' : [ page[0] ], \ 'comment' : page[2] \ } ) pdf.make_pdf(json.dumps(data),'tkalbum.pdf') showinfo( title="알림", message="생성되었습니다" ) page_list = [] image_file = None image_label = None listbox = None text_edit = None if __name__ == "__main__": root= tkinterDnD.Tk() root.title("Album") root.geometry("400x300") root.resizable(False,False) splitter = TkSplitter(root) splitter.AddItem(ListFrame(splitter)) splitter.AddItem(ImageFrame(splitter)) splitter.pack(side=TOP,expand=True,fill=BOTH,pady=10,padx=10) root.mainloop()
댓글 없음:
댓글 쓰기