-2
我打算從tkinter.filedialog.askopenfile打開文件中的py文件的內容Tkinter文本部件。下面的代碼:如何將文件的內容放入Tkinter文本中?
# -*- coding: utf-8 -*-
import tkinter.messagebox
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.filedialog
myfonts = {'times': 'times 18 bold', 'verdana': 'Verdana 10'}
mypatterns = [('Python', '*.py'), ('Python', '*.pyw')]
class IdlePlus(tk.Frame):
def __init__(self, parent=None):
tk.Frame.__init__(self, parent, class_='IdlePlus')
self.master = parent
self.master.title('IDLE Plus')
self.master.geometry('500x500+250+50')
self.master.configure(bg='orange')
self.master.bind('<Control-q>', self.idleplusQuit)
self.master.bind('<Control-o>', self.myfileOpen)
self.myBarMenu()
self.myNoteBook()
def myfileOpen(self):
self.myfile = tk.filedialog.askopenfile(filetypes=mypatterns,
title='Open a Python file', mode='rb')
loadedfile = self.myfile.read()
self.myfile.close()
print(loadedfile)
def myNoteBook(self):
self.notebook = ttk.Notebook(self.master, width=500, height=600)
self.framenotebook1 = ttk.Frame(self.notebook)
self.framenotebook2 = ttk.Frame(self.notebook)
self.notebook.add(self.framenotebook1, text='One')
self.notebook.add(self.framenotebook2, text='Two')
self.notebook.pack(side='top', expand='yes', fill='both')
self.scrollbarView = tk.Scrollbar(self.framenotebook1,
orient='vertical', takefocus=False,
highlightthickness=0)
self.textView = tk.Text(self.framenotebook1, bg='blue', fg='white',
wrap='word', highlightthickness=0)
self.scrollbarView.config(command=self.textView.yview)
self.textView.config(yscrollcommand=self.scrollbarView.set)
self.scrollbarView.pack(side='right', fill='y')
self.textView.pack(side='left', expand='yes', fill='both')
def myBarMenu(self):
mymenubar = tk.Menu(self.master)
filemenu = tk.Menu(mymenubar, tearoff=0)
filemenu.add_command(label='New File', underline=0,
accelerator='CTRL+N')
filemenu.add_command(label='Open File', underline=0,
accelerator='CTRL+O', command=self.myfileOpen)
filemenu.add_separator()
filemenu.add_command(label='Save File', underline=0,
accelerator='CTRL+S')
filemenu.add_command(label='Save As...', accelerator='CTRL+SHIFT+S')
filemenu.add_command(label='Close File', underline=0,
accelerator='ALT+F4')
filemenu.add_separator()
filemenu.add_command(label='Print File', underline=0,
accelerator='CTRL+P')
filemenu.add_separator()
filemenu.add_command(label='Exit', underline=0,
accelerator='CTRL+Q', command=self.idleplusQuit)
mymenubar.add_cascade(label='File', underline=0, menu=filemenu)
aboutmenu = tk.Menu(mymenubar, tearoff=0)
aboutmenu.add_command(label='Help', underline=0, accelerator='F1')
aboutmenu.add_command(label='About IDLE-Plus', underline=0)
mymenubar.add_cascade(label='Help', underline=0, menu=aboutmenu)
self.master.configure(menu=mymenubar)
def idleplusQuit(self, event=None):
if tk.messagebox.askokcancel('Quit', 'Do you really want to exit?',
parent=self.master):
self.master.destroy()
window = tk.Tk()
myapp = IdlePlus(window)
window.mainloop()
我希望把內容loadedfile在self.textView,實際內容將顯示在終端打印的罰款。 感謝
好吧,它的工作原理,但我有一個小問題,我怎麼可以指定編碼= UTF-8? askopenfile沒有這個選項。 – Tobal 2014-11-05 19:52:09
在python 3中,當你用'mode ='r''打開文件時,默認編碼是utf-8。但是你可以用二進制模式'mode ='rb'打開一個文件,然後使用utf-8解碼它。我不記得是怎麼回事,但它是其中一個python庫。 – 2014-11-05 20:30:36