鱼C论坛

 找回密码
 立即注册
查看: 1830|回复: 1

[已解决]Python记事本,有点小问题,求教

[复制链接]
发表于 2016-8-20 12:59:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
运行图片截图运行时一切正常,关闭后出现错误
# coding:utf-8
from Tkinter import *
from tkMessageBox import *
from tkFileDialog import *
from threading import Timer
import os
import sys

reload(sys)
sys.setdefaultencoding('utf8')

filename = ''


def author():
    showinfo('作者信息', '本软件由Delav完成')


def about():
    showinfo('版权信息', '本软件版权归属Delav')


def openfile():
    global filename
    filename = askopenfilename(defaultextension='.txt')
    if filename == '':
        filename = None
    else:
        root.title('FileName'+os.path.basename(filename))
        textPad.delete(1.0, END)
        f = open(filename, 'r')
        textPad.insert(1.0, f.read())
        f.close()


def new():
    global filename
    root.title('未命名文件')
    filename = None
    textPad.delete(1.0, END)


def save():
    global filename
    try:
        f = open(filename, 'w')
        msg = textPad.get(1.0, END)
        f.write(msg)
        f.close()
    except:
        saves()


def saves():
    f = asksaveasfilename(initialfile='未命名.txt', defaultextension='.txt')
    global filename
    filename = f
    fh = open(f, 'w')
    msg = textPad.get(1.0, END)
    fh.write(msg)
    fh.close()
    root.title(os.path.basename(f))


def cut():
    textPad.event_generate('<<Cut>>')


def copy():
    textPad.event_generate('<<Copy>>')


def paste():
    textPad.event_generate('<<Paste>>')


def undo():
    textPad.event_generate('<<Undo>>')


def redo():
    textPad.event_generate('<<Redo>>')


def seleteAll():
    textPad.tag_add('sel', '1.0', END)


def search():
    topsearch = Toplevel(root)
    topsearch.geometry('300x30+200+250')
    label = Label(topsearch, text='Find')
    label.grid(row=0, column=0, padx=5)
    entry = Entry(topsearch, width=20)
    entry.grid(row=0, column=1, padx=5)
    button1 = Button(topsearch, text='查找')
    button1.grid(row=0, column=2)


def bg_color():
    textPad.config(bg='#66CCFF')


def fn_color():
    textPad.config(fg='#FFFFFF')

root = Tk()
root.title('Delav note')
root.geometry('800x500+100+100')  # 首大小为800x500,位置100*100

# Menu

menubar = Menu(root)
root.config(menu=menubar)

filemenu = Menu(menubar)
menubar.add_cascade(label='文件', menu=filemenu)
filemenu.add_command(label='新建', accelerator='Ctrl+N', command=new)
filemenu.add_command(label='打开', accelerator='Ctrl+O', command=openfile)
filemenu.add_command(label='保存', accelerator='Ctrl+S', command=save)
filemenu.add_command(label='另存为', accelerator='Ctrl+Shift+S', command=saves)
filemenu.add_command(label='退出', accelerator='Ctrl+Q', command=quit)

editmenu = Menu(menubar)
menubar.add_cascade(label='编辑', menu=editmenu)
editmenu.add_command(label='撤销', accelerator='Ctrl+Z', command=undo)
editmenu.add_command(label='重做', accelerator='Ctrl+Y', command=redo)
editmenu.add_separator()
editmenu.add_command(label='剪切', accelerator='Ctrl+X', command=cut)
editmenu.add_command(label='复制', accelerator='Ctrl+C', command=copy)
editmenu.add_command(label='粘贴', accelerator='Ctrl+V', command=paste)
editmenu.add_separator()
editmenu.add_command(label='查找', accelerator='Ctrl+F', command=search)
editmenu.add_command(label='全选', accelerator='Ctrl+A', command=seleteAll)

fontmenu = Menu(root)
menubar.add_cascade(label='字体', menu=fontmenu)
fontmenu.add_command(label='样式')
fontmenu.add_command(label='大小')
fontmenu.add_command(label='颜色', command=fn_color)
fontmenu.add_separator()
fontmenu.add_command(label='背景颜色', command=bg_color)

aboutmenu = Menu(root)
menubar.add_cascade(label='关于', menu=aboutmenu)
aboutmenu.add_command(label='作者', command=author)
aboutmenu.add_command(label='版权', command=about)

# Toolbar
toolbar = Frame(root, height=10, bg='light sea green')
shortButton = Button(toolbar, text='打开', padx=5, pady=1, command=openfile)
shortButton.pack(side=LEFT, padx=5, pady=2)

shortButton = Button(toolbar, text='保存', padx=5, pady=1, command=save)
shortButton.pack(side=LEFT)
toolbar.pack(expand=NO, fill=X)

# text
textPad = Text(root, undo=True)
textPad.pack(expand=YES, fill=BOTH)

scroll = Scrollbar(textPad)
textPad.config(yscrollcommand=scroll.set, bg='#B0E0E6', fg="#FFFFFF", font=18)
scroll.config(command=textPad.yview)
scroll.pack(side=RIGHT, fill=Y)


def get_line():
    global t
    row, col = textPad.index(INSERT).split('.')
    line_num = 'Ln:  ' + row + '   ' + 'Col:  ' + col
    var.set(line_num)

    t = Timer(1, get_line)
    t.start()

t = Timer(1, get_line)
t.start()

# Status Bar
var = StringVar()
status = Label(root, anchor=E, text='Ln', textvariable=var, padx=1)
status.pack(side=BOTTOM, fill=X)

root.mainloop()



出现错误:
E:\Python27\python.exe F:/TKinter/node.pyException in thread Thread-18:Traceback (most recent call last):  File "E:\Python27\lib\threading.py", line 810, in __bootstrap_inner    self.run()  File "E:\Python27\lib\threading.py", line 1082, in run    self.function(*self.args, **self.kwargs)  File "F:/TKinter/node.py", line 170, in get_line    row, col = textPad.index(INSERT).split('.')  File "E:\Python27\lib\lib-tk\Tkinter.py", line 3100, in index    return str(self.tk.call(self._w, 'index', index))TclError: invalid command name ".42537736L"Process finished with exit code 0
最佳答案
2016-8-20 13:12:27
thinter 窗口关闭时出错 main thread is not in main loo
http://bbs.fishc.com/thread-74705-1-1.html
(出处: 鱼C论坛)
C08L37{M7MGID~QIS9P[LVT.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-8-20 13:12:27 | 显示全部楼层    本楼为最佳答案   
thinter 窗口关闭时出错 main thread is not in main loo
http://bbs.fishc.com/thread-74705-1-1.html
(出处: 鱼C论坛)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-18 12:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表