0

GUI ベースのテキスト エディターを作成しているときに、[ファイルを開く] ダイアログ ボックスを使用してファイルを開きました。

ただし、開くファイルを選択するたびに、ファイルが読み取られ、テキスト領域にゴミが表示されることがわかりました。より具体的には、番号を表示します.140598120872128

なぜこれが起こっているのですか、どうすれば修正できますか?

これは私のコードです。私は何か間違ったことをしていますか?

from Tkinter import *
import tkMessageBox
import Tkinter
import tkFileDialog

def donothing():
   print "a"

def file_save():
    name=tkFileDialog.asksaveasfile(mode='w',defaultextension=".txt")
    if name is None:
        return
    text2save=str(text.get(0.0,END))
    name.write(text2save)
    name.close()

def file_open():
    ftypes = [('Text files', '*.txt'), ('All files', '*')]
    dlg = tkFileDialog.Open(filetypes = ftypes)
        fl = dlg.show()

        if fl != '':
            txt = readFile(fl)
            text.insert(END, text)

def readFile(filename):

        f = open(filename, "r")
        text = f.read()
        return text


root = Tk()
root.geometry("500x500")
menubar=Menu(root)
text=Text(root)
text.pack()
filemenu=Menu(menubar,tearoff=0)
filemenu.add_command(label="New", command=donothing)
filemenu.add_command(label="Open", command=file_open)
filemenu.add_command(label="Save", command=file_save)
filemenu.add_command(label="Save as...", command=donothing)
filemenu.add_command(label="Close", command=donothing)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)

editmenu=Menu(menubar,tearoff=0)
editmenu.add_command(label="Undo", command=donothing)
editmenu.add_command(label="Copy", command=donothing)
editmenu.add_command(label="Paste", command=donothing)
menubar.add_cascade(label="Edit", menu=editmenu)

helpmenu=Menu(menubar,tearoff=0)
helpmenu.add_command(label="Help",command=donothing)
menubar.add_cascade(label="Help",menu=helpmenu)

root.config(menu=menubar)
root.mainloop()
4

1 に答える 1

4

単純なタイプミス:textテキスト ウィジェット オブジェクトです。である必要がありますtxt

次の行を置き換えます。

    text.insert(END, text)

と:

    text.insert(END, txt)
于 2013-10-20T11:07:21.527 に答える