'Tkinter'에 해당되는 글 1건

  1. 2010.01.02 [Python] Simple file viewer
Coding/Python 삽질기2010. 1. 2. 00:47

#-*- coding: cp949 -*-
# Name        : py_fileview.py
# Author      : chobocho.com
# Version     :
# Copyright   :
# Description : Simple text file viewer
#

from Tkinter import *
import tkMessageBox
import os.path
import glob 
 
class App:
    def __init__ (self, master):
        frame = Frame(master)
        frame.pack()
       
        # Text Area
        f0 = Frame(frame, width = 100, height = 100)
        f0.grid(row = 0, column = 0)
        self.text_scrollbar = Scrollbar (f0, orient=VERTICAL)
        self.text = Text(f0, width=80, height = 20, yscrollcommand=self.text_scrollbar.set)
        self.text_scrollbar.config(command=self.text.yview)
        self.text_scrollbar.pack(side=RIGHT, fill=Y)
        self.text.pack() 
       
        # List Box area
        f1 = Frame(frame, width = 100, height = 100)
        f1.grid(row = 0, column = 1)
       
        self.list_button = Button(f1, text="File List", command=self.ShowInfo)
        self.list_button.pack()
       
        self.listbox_scrollbar = Scrollbar (f1, orient=VERTICAL)
        self.listbox = Listbox(f1, height=19, yscrollcommand=self.listbox_scrollbar.set)
        self.listbox_scrollbar.config(command=self.listbox.yview)
        self.listbox_scrollbar.pack(side=RIGHT, fill=Y)
        # 파일 내용을 읽어서 보여 줄 것
        self.ReadTemplate()
        self.listbox.bind("<Double-Button-1>", self.LoadFile)
        self.listbox.pack(side=RIGHT)

    def ClearText(self):
        self.text.delete(1.0, END) 
   
    def ShowInfo(self):
        tkMessageBox.showinfo("Information","http://chobocho.com\nVersion 0.2")
   
    def ReadTemplate(self):
        all_flist = glob.glob ("*.txt")
       
        for f in all_flist:
            if os.path.exists(f):
                self.listbox.insert(END, f.decode('cp949'))
               
    def LoadFile(self, event):
        self.items = self.listbox.curselection()
        self.el = self.listbox.get(self.items)
        self.ClearText()
        if os.path.exists(self.el):
            self.fp = open(self.el, 'r')
            self.tempFileData = self.fp.read()
            self.fp.close()
            self.text.insert(1.0, self.tempFileData.decode('cp949'))

#----------------------------------------------------------
# main
if __name__ == "__main__":     
    root = Tk()
    app = App(root)
    root.mainloop()



실행화면

'Coding > Python 삽질기' 카테고리의 다른 글

[Python] 초간단 Lotto 생성기 소스  (0) 2010.01.22
[Python] 회전 이동  (0) 2009.08.25
WxPython - HelloWorld  (0) 2008.12.22
Posted by chobocho