这一章读的不是很仔细,因为感觉很多工具性的东西都可以用的时候再查。下面仅附上代码。
#==============================================================================
##5-1
# #最简单的第一个程序:显示一个窗口,里面显示文本
# import tkinter
# top=tkinter.Tk()
# label=tkinter.Label(top,text='我活的好悲伤,我在雨中拉肖邦')
# label.pack()
# tkinter.mainloop()
#==============================================================================
#==============================================================================
##5-2
# #按钮,但不知道为什么这个按钮一按就卡死,试了一下换成别的函数就可以了,应该是退出函数哪里出了bug
# import tkinter
# top=tkinter.Tk()
# hello=tkinter.Button(top,text='quit',command=top.quit())
# hello.pack()
# tkinter.mainloop()
#==============================================================================
#==============================================================================
##5-3
# #又有按钮又有文本
# import tkinter
# top=tkinter.Tk()
# label=tkinter.Label(top,text='我活的好悲伤,我在雨中拉肖邦')
# label.pack()
# quit=tkinter.Button(top,text='quit',command=top.quit)
# quit.pack(fill=tkinter.X,expand=1)
# tkinter.mainloop()
#==============================================================================
#==============================================================================
##5-4
# #用按钮改变文本的大小
# from tkinter import *
#
# def resize(ev=None):
# label.config(font='Helvetica -%d bold'%scale.get())
# def f():
# print(1)
#
# top=Tk()
#
# top.geometry('250x150')# 这是 字母x,不是乘号
# label=Label(top,text='我活的好悲伤,我在雨中拉肖邦',font='Helvetica -12 bold')
# # label.config(font='Helvetica -%d bold'%40)
# label.pack(fill=X, expand=1)
# scale=Scale(top,from_=10,to=40,orient=HORIZONTAL,command=resize)
# #
# scale.set(12)
# scale.pack(fill=X,expand=1)
#
# quit=Button(top,text='quit',command=f,\
# activeforeground='white',activebackground='red')
#
# quit.pack()
# mainloop()
#==============================================================================
#==============================================================================
##5-5
# #偏函数模块
# from functools import partial as pto
# from tkinter import Tk,Button,X
# #三种不同的窗口通知方式:叉号、小三角、字母i
# from tkinter.messagebox import showerror,showinfo,showwarning
#
# WARN='warn'
# CRIT='crit'
# REGU='regu'
#
# SIGNS={
# 'do not enter':CRIT,
# 'railroad crossing':WARN,
# '55\n speed limit':REGU,
# 'wrong way':CRIT,
# 'merging traffic':WARN,
# 'obe way':REGU,
# }
#
# #匿名函数
# critCB=lambda:showerror('Error','Error Button Pressed')
# warnCB=lambda:showwarning('Warning',
# 'Warning Button Pressed!')
# infoCB=lambda:showinfo('InfO','Info Button Pressed')
#
# top=Tk()
# top.title('Rod Signs')
# Button(top,text='quit',command=top.quit,
# bg='red',fg='white').pack(fill=X,expand=1)
#
# #二阶偏函数:避免重复赋值
# MyButton=pto(Button,top)
# CritButton=pto(MyButton,command=critCB,bg='white',fg='red')
# WarnButton=pto(MyButton,command=warnCB,bg='goldenrod1')
# ReguButton=pto(MyButton,command=infoCB,bg='white')
#
# for eachsign in SIGNS:
# signType=SIGNS[eachsign]
# cmd='%sButton(text=%r%s).pack(fill=X,expand=True)'%(
# signType.title(),eachsign,
# '.upper()'if signType==CRIT else '.title()')
# #eval:将字符串当做有效的命令来求值
# eval(cmd)
#
# top.mainloop()
#==============================================================================
#==============================================================================
##5-6
##文件系统
# import os
# from time import sleep
# from tkinter import *
# from tkinter import TclError
# class DirList(object):
# def __init__(self,initdir=None):
# self.top=Tk()
# self.label=Label(self.top,text='directory lister v1.1')
# self.label.pack()
#
# self.cwd=StringVar(self.top)
# self.dirl=Label(self.top,fg='blue',font=('Helvetica',12,'bold'))
# self.dirl.pack()
#
# self.dirfm=Frame(self.top)
# self.dirsb=Scrollbar(self.dirfm)
# self.dirsb.pack(side=RIGHT,fill=Y)
# self.dirs=Listbox(self.dirfm,height=15,width=50,yscrollcommand=self.dirsb.set)
# # Shift
# self.dirs.bind('<Double-1>',self.setDirAndGo)
# # self.dirs.bind('<ButtonRelease-1>',self.setDir)
# self.dirsb.config(command=self.dirs.yview)
# self.dirs.pack()
# self.dirfm.pack()
#
# self.dirn=Entry(self.top,width=50,textvariable=self.cwd)
# self.dirn.bind('<Return>',self.dols)
# self.dirn.pack()
#
# self.bfm=Frame(self.top)
# self.clr=Button(self.bfm,text='clear',command=self.clrDir,activeforeground='white',activebackground='blue')
# self.ls=Button(self.bfm,text='list directory',command=self.dols,activeforeground='white',activebackground='green')
# self.quit=Button(self.bfm,text='Quit',command=self.top.quit,activeforeground='white',activebackground='red')
# self.clr.pack()
# self.ls.pack()
# self.quit.pack()
# self.bfm.pack()
#
# if initdir:
# self.cwd.set(os.curdir)
# self.dols()
#
# def clrDir(self,ev=None):
# self.cwd.set('')
# def setDir(self,ev=None):
#
# check=self.dirs.get(self.dirs.curselection())
# self.cwd.set(check)
# print(check)
# self.dirn.update()
#
# def setDirAndGo(self,ev=None):
# self.last=self.cwd.get()
# self.dirs.config(selectbackground='red')
# check=self.dirs.get(self.dirs.curselection())
# if not check:
# check=os.curdir
# self.cwd.set(check)
# self.dols()
#
# def dols(self,ev=None):
# error=''
# tdir=self.cwd.get()
# if tdir=='.':
# try:
# tdir=self.dirs.get(self.dirs.curselection())
# except:
# pass
# if not tdir:
# tdir=os.curdir
# if not os.path.exists(tdir):
# error=tdir+':'+'no such file'
# elif not os.path.isdir(tdir):
# error=tdir+':'+'not a directory'
# if error:
# self.cwd.set(error)
# self.top.update()
# sleep(1)
# if not (hasattr(self,'last') and self.last):
# self.last=os.curdir
# self.cwd.set(self.last)
# self.dirs.config(selectbackground='LightSkyBlue')
# self.top.update()
# return
# self.cwd.set('FETCHING DIRECTORY CONTENTS...')
# self.top.update()
# dirlist=os.listdir(tdir)
# dirlist.sort()
# os.chdir(tdir)
#
# self.dirl.config(text=os.getcwd())
# self.dirs.delete(0,END)
# self.dirs.insert(END,os.curdir)
# self.dirs.insert(END,os.pardir)
# for eachFile in dirlist:
# self.dirs.insert(END,eachFile)
# self.cwd.set(os.curdir)
# self.dirs.config(selectbackground='LightSkyBlue')
#
# def main():
# d=DirList(os.curdir)
# mainloop()
#
# if __name__=='__main__':
# main()
#
#==============================================================================
#==============================================================================
# #5-7
# #tix的应用
# from tkinter import Label,Button,END
# from tkinter.tix import Tk,Control,ComboBox
#
# top=Tk()
#
# label=Label(top,text='Anmials(in pairs;min:pair,max:dozen)')
# label.pack()
#
# ct=Control(top,label='Number:',integer=True,max=12,min=2,value=3,step=2)
# ct.label.config(font='Helvetival -14 bold')
# ct.pack()
#
# cb=ComboBox(top,label='Type:',editable=True)
# for anmail in ('dog','cat','hamster','python'):
# cb.insert(END,anmail)
# cb.pack()
#
# qb=Button(top,text='quit',command=top.quit,bg='red',fg='white')
# qb.pack()
#
# top.mainloop()
#==============================================================================