2w基于Tkinter日记交互

程序类的框架

要实现的功能

  • 文字输入
  • 文字保存
  • 历史文档自动显示

基于文档24.1,框架如下

# encoding: UTF-8

from Tkinter import *

class App(Frame):
def createWidgets(self):
    pass

def write_in(self, event):
    pass

def show(self, event):
    pass

def __init__(self, master=None):
    Frame.__init__(self, master)
    self.pack()
    self.createWidgets()

root = Tk()
app = App(master=root)

app.master.title('煎饼日记')
app.master.maxsize(500, 400)
app.mainloop()

创建和设置Widgets

组合

  • Entry与text
  • Entry与massage
  • Entry与Label

由于自己手里有一本叫GUI application development HOTSHOT的书,里面讲了text,所以我选择了组合一

def creatWidgets(self):
    self.mytext = Text(height = 20, width = 500)
    self.mytext.pack()
    self.mytext.config(background = "white smoke")

    self.dairyentry = Entry(width = 500)
    self.dairyentry.pack()

    opened_lines = open('daily.txt','r')
    lines = opened_lines.read()
    self.mytext.insert(END, lines + '\n') # 这三行用来显示历史信息

    opened_lines.close()

即时显示录入的文字

组合

  • Button与command功能
  • 与bind

为了在输入的同时显示文字--一个按键同时call两个函数,程序框架改为

class App(Frame):
def createWidgets(self):
    pass
    self.show_and_write_in()

def show_and_write_in(self, event): # 注意括号里是self, event
    self.write_in()
    self.show()

def show(self, event):
    pass

def write_in(self, event):
    pass

def __init__(self, master=None):
    Frame.__init__(self, master)
    self.pack()
    self.createWidgets()

Button与command功能

self.print = Button(text= "print",command=self.show_and_write_in)

Return与bind组合

self.dairyentry.bind('<Key-Return>', self.show_and_write_in)

后者更方便

def creatWidgets(self):
    self.mytext = Text(height = 20, width = 500)
    self.mytext.pack()
    self.mytext.config(background = "white smoke")

    self.dairyentry = Entry(width = 500)
    self.dairyentry.pack()

    opened_lines = open('daily.txt','r')
    lines = opened_lines.read()
    self.mytext.insert(END, lines + '\n') # 这三行用来显示历史信息

    opened_lines.close()

    self.dairyentry.bind('<Key-Return>', self.show_and_write_in)
  • bind的功能:通过' < Key-return > ', 把Entry输入的内容--self.dairyentry,与event--self.show_and_write_in这个函数,绑(bind)起来,也就是通过.bind('< key >', 建立其前后之间的联系

show():

就是把Entry这个Widget的信息insert到Widget Text里,得用到.get()这个命令

readlines = self.dairyentry.get()
self.mytext.insert(END, readlines + '\n') #END,末尾的index

write_in():

就是把Entry这个Widget的信息write 到 dairy.txt中

opened_lines = open('dairy.txt', 'a') # a = append
lines = self.dairyentry.get()
opened_lines.write('\n' + lines)
opened_lines.close()
self.dairyentry.delete(0, END)

每次敲回车后,自动删除Entry里的内容

可用代码已成型

简单的修改,增加代码的简洁性

import sys
from Tkinter import *
class App(Frame):
    def createWidgets(self):
        self.mytext = Text(height = 20, width = 500)
        self.mytext.pack()

        self.dairyentry = Entry(width = 500)
        self.dairyentry.pack()

        self.config()

    def config(self):
        self.mytext.config(background = "white  smoke")

        opened_lines = open('daily.txt','r')
        lines = opened_lines.read()
        self.mytext.insert(END, lines + '\n')
        opened_lines.close()

        self.dairyentry.bind('<Key-Return>', self.show_and_write_in)

    def show_and_write_in(self, event):
        self.show(event)
        self.write_in(event)

    def show(self, event):
        read_lines = self.dairyentry.get()
        self.mytext.insert(END, read_lines + '\n')       

    def write_in(self, event):
        opened_lines = open('daily.txt','a')
        lines = self.dairyentry.get()
        self.dairyentry.delete(0,END)
        opened_lines.write('\n' + lines)
        opened_lines.close()

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

root = Tk()
app = App(master=root)

app.master.title('煎饼日记')
app.master.maxsize(500, 400)
app.mainloop()