当我从 tkinter.filedialog.askopenfile 打开文件时,我试图将py文件的内容放在 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 中,实际上内容在终端中打印得很好。 感谢
答案 0 :(得分:3)
你可以这样做:
def myfileOpen(self):
self.myfile = tk.filedialog.askopenfile(filetypes=mypatterns,
title='Open a Python file', mode='r')
loadedfile = self.myfile.read()
self.myfile.close()
self.textView.insert("end", loadedfile)