《python核心编程》读书笔记 第五章 GUI

本文通过多个示例展示了使用Python的Tkinter库进行GUI编程的方法,包括创建窗口、按钮、文本显示、字体调整、文件系统操作及Tix控件应用,适合初学者实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这一章读的不是很仔细,因为感觉很多工具性的东西都可以用的时候再查。下面仅附上代码。

#==============================================================================
##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()
#==============================================================================

 

基于Spring Boot搭建的一个多功能在线学习系统的实现细节。系统分为管理员和用户两个主要模块。管理员负责视频、文件和文章资料的管理以及系统运营维护;用户则可以进行视频播放、资料下载、参与学习论坛并享受个性化学习服务。文中重点探讨了文件下载的安全性和性能优化(如使用Resource对象避免内存溢出),积分排行榜的高效实现(采用Redis Sorted Set结构),敏感词过滤机制(利用DFA算法构建内存过滤树)以及视频播放的浏览器兼容性解决方案(通过FFmpeg调整MOOV原子位置)。此外,还提到了权限管理方面自定义动态加载器的应用,提高了系统的灵活性和易用性。 适合人群:对Spring Boot有一定了解,希望深入理解其实际应用的技术人员,尤其是从事在线教育平台开发的相关从业者。 使用场景及目标:适用于需要快速搭建稳定高效的在线学习平台的企业或团队。目标在于提供一套完整的解决方案,涵盖从资源管理到用户体验优化等多个方面,帮助开发者更好地理解和掌握Spring Boot框架的实际运用技巧。 其他说明:文中不仅提供了具体的代码示例和技术思路,还分享了许多实践经验教训,对于提高项目质量有着重要的指导意义。同时强调了安全性、性能优化等方面的重要性,确保系统能够应对大规模用户的并发访问需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值