Python 在Tkinter中打开文件会在文本区域中产生垃圾值
在创建基于GUI的文本编辑器时,我使用“打开文件”对话框打开了一个文件 然而,我发现每当我选择一个文件打开,它就会读取它并在文本区域显示垃圾。更具体地说,它显示数字Python 在Tkinter中打开文件会在文本区域中产生垃圾值,python,Python,在创建基于GUI的文本编辑器时,我使用“打开文件”对话框打开了一个文件 然而,我发现每当我选择一个文件打开,它就会读取它并在文本区域显示垃圾。更具体地说,它显示数字.140598120872128 为什么会发生这种情况,我如何纠正 这是我的密码。我做错什么了吗 from Tkinter import * import tkMessageBox import Tkinter import tkFileDialog def donothing(): print "a" def file_s
.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()
简单输入:
text
是文本小部件对象。它应该是txt
替换以下行:
text.insert(END, text)
与:
简单输入:
text
是文本小部件对象。它应该是txt
替换以下行:
text.insert(END, text)
与:
顺便说一句,你知道为什么会显示那个特定的数字吗?@RohitShinde,试试Tkinter import的
;b=按钮();打印b;print repr(b)
@RohitShinde,它是tcl/tk使用的内部唯一标识符。另外,您知道为什么会显示该特定数字吗?@RohitShinde,请尝试Tkinter import*中的;b=按钮();打印b;print repr(b)
@RohitShinde,它是tcl/tk使用的内部唯一标识符。