Python简单的GUI程序示例详解
程序员文章站
2022-06-09 22:22:53
目录一、记事本二、简单画图总结一、记事本源码#python简易记事本from tkinter import *from tkinter import messageboxfrom tkinter im...
一、记事本
源码
#python简易记事本 from tkinter import * from tkinter import messagebox from tkinter import filedialog import os filename='' #文件名,全局变量 def about(): messagebox.showinfo('提示','这是一个消息框') def openfile(): global filename #使用全局变量 filename=filedialog.askopenfilename(defaultextension='.txt') #打开文件,默认扩展名是xtxt if filename=='': #如果没有选择 filename=none else: root.title('filename:'+os.path.basename(filename)) #将文件名显示在标题栏中 textpad.delete(1.0,end) #删除文本框中原有内容 f=open(filename,'r',encoding='utf-8') #获取文件中内容 textpad.insert(1.0,f.read()) #显示到文本框 f.close() def saveotherfile(): global filename f=filedialog.asksaveasfilename(initialfile='未命名.txt',defaultextension='.txt') #保存文件 filename=f root.title('filename:' + os.path.basename(filename)) fh=open(f,'w',encoding='utf-8') #只写方式打开文件 msg=textpad.get(1.0,end) #从文本框中获取内容 fh.write(msg) fh.close() root=tk() #设置菜单栏 root.title('记事本') root.geometry('500x500+100+100') #widthxheight+x+y menubar=menu(root) root.config(menu=menubar) filemenu=menu(menubar) #第一个菜单栏 filemenu.add_command(label='打开',accelerator='ctrl+n',command=openfile) filemenu.add_command(label='保存',accelerator='ctrl+t',command=saveotherfile) menubar.add_cascade(label='文件',menu=filemenu) aboutmenu=menu(menubar) aboutmenu.add_command(label='关于',accelerator='ctrl+y',command=about) menubar.add_cascade(label='帮助',menu=aboutmenu) status=label(root,text='ln20',bd=1,relief=sunken,anchor=w) #底部状态栏 status.pack(side=bottom,fill=x) lnlabel=label(root,width=2,bg='antique white') #左边边界栏 lnlabel.pack(side=left,fill=y) textpad=text(root,undo=true) #设置文本框 textpad.pack(expand=yes,fill=both) scroll=scrollbar(textpad) #右边进度条栏 textpad.config(yscrollcommand=scroll.set) scroll.config(command=textpad.yview) scroll.pack(side=right,fill=y) root.mainloop()#python简易记事本 from tkinter import * from tkinter import messagebox from tkinter import filedialog import os filename='' #文件名,全局变量 def about(): messagebox.showinfo('提示','这是一个消息框') def openfile(): global filename #使用全局变量 filename=filedialog.askopenfilename(defaultextension='.txt') #打开文件,默认扩展名是xtxt if filename=='': #如果没有选择 filename=none else: root.title('filename:'+os.path.basename(filename)) #将文件名显示在标题栏中 textpad.delete(1.0,end) #删除文本框中原有内容 f=open(filename,'r',encoding='utf-8') #获取文件中内容 textpad.insert(1.0,f.read()) #显示到文本框 f.close() def saveotherfile(): global filename f=filedialog.asksaveasfilename(initialfile='未命名.txt',defaultextension='.txt') #保存文件 filename=f root.title('filename:' + os.path.basename(filename)) fh=open(f,'w',encoding='utf-8') #只写方式打开文件 msg=textpad.get(1.0,end) #从文本框中获取内容 fh.write(msg) fh.close() root=tk() #设置菜单栏 root.title('记事本') root.geometry('500x500+100+100') #widthxheight+x+y menubar=menu(root) root.config(menu=menubar) filemenu=menu(menubar) #第一个菜单栏 filemenu.add_command(label='打开',accelerator='ctrl+n',command=openfile) filemenu.add_command(label='保存',accelerator='ctrl+t',command=saveotherfile) menubar.add_cascade(label='文件',menu=filemenu) aboutmenu=menu(menubar) aboutmenu.add_command(label='关于',accelerator='ctrl+y',command=about) menubar.add_cascade(label='帮助',menu=aboutmenu) status=label(root,text='ln20',bd=1,relief=sunken,anchor=w) #底部状态栏 status.pack(side=bottom,fill=x) lnlabel=label(root,width=2,bg='antique white') #左边边界栏 lnlabel.pack(side=left,fill=y) textpad=text(root,undo=true) #设置文本框 textpad.pack(expand=yes,fill=both) scroll=scrollbar(textpad) #右边进度条栏 textpad.config(yscrollcommand=scroll.set) scroll.config(command=textpad.yview) scroll.pack(side=right,fill=y) root.mainloop()
运行效果:
二、简单画图
# 画图程序 import os import tkinter import tkinter.simpledialog import tkinter.colorchooser import tkinter.filedialog from pil import image from pil import imagegrab def openitem(): global img filename=tkinter.filedialog.askopenfilename(title="open",filetypes=[('image','*.png *.gif')]) if filename: img=tkinter.photoimage(file=filename) canvas.create_image(400,300,image=img) def saveitem(): savex=int(root.winfo_rootx()) savey=int(root.winfo_rooty()) savew=root.winfo_width() saveh=root.winfo_height() im=imagegrab.grab((savex,savey,savex+savew,savey+saveh)) filename=tkinter.filedialog.asksaveasfilename(title='save',filetypes=[('image','*.png *.gif')]) if not filename: return try: if filename[filename.index('.'):] in ['.png','.gif']==false: filename+='.png' except: filename+='.png' im.save(filename) def clearitem(): for i in canvas.find_all(): canvas.delete(i) def curvetype(): pentype.set(1) def linetype(): pentype.set(2) def rectangletype(): pentype.set(3) def texttype(): global usertext usertext=tkinter.simpledialog.askstring(title="please input your words", prompt='') pentype.set(4) def erasetype(): pentype.set(5) def chooseforecolor(): global forecolor forecolor=tkinter.colorchooser.askcolor()[1] def choosebackcolor(): global backcolor backcolor=tkinter.colorchooser.askcolor()[1] def onclickleft(event): global usertext candraw.set(1) x.set(event.x) y.set(event.y) if pentype.get()==4: canvas.create_text(event.x,event.y,text=usertext) def onmoveleft(event): global lastpaint,forecolor,backcolor if candraw.get()==0: return if pentype.get()==1: #绘制曲线 canvas.create_line(x.get(),y.get(),event.x,event.y,fill=forecolor) x.set(event.x) y.set(event.y) elif pentype.get()==2: #绘制直线(预览) try: canvas.delete(lastpaint) except: pass lastpaint=canvas.create_line(x.get(),y.get(),event.x,event.y,fill=forecolor) elif pentype.get()==3: #绘制矩形(预览) try: canvas.delete(lastpaint) except: pass lastpaint=canvas.create_rectangle(x.get(),y.get(),event.x,event.y,fill=backcolor,outline=forecolor) elif pentype.get()==5: #橡皮擦 canvas.create_rectangle(event.x-5,event.y-5,event.x+5,event.y+5,fill=backcolor,outline=backcolor) def onreleaseleft(event): global lastpaint,forecolor,backcolor lastpaint=0 if pentype.get()==2: #抬起左键画直线 canvas.create_line(x.get(),y.get(),event.x,event.y,fill=forecolor) elif pentype.get()==3: #抬起左键就画矩形 canvas.create_rectangle(x.get(),y.get(),event.x,event.y,fill=backcolor,outline=forecolor) candraw.set(0) def onreleaseright(event): menu.post(event.x_root, event.y_root) root=tkinter.tk() root.title("paint") root.geometry("800x600+280+50") lastpaint=0 usertext="" forecolor='black' backcolor='white' x=tkinter.intvar(value=0) y=tkinter.intvar(value=0) candraw=tkinter.intvar(value=0) #能否画图,1:能,0:不能 pentype=tkinter.intvar(value=1) #画笔类型,1:曲线,2:直线,3:矩形,4:文本,5:橡皮擦 menu=tkinter.menu(root,tearoff=0) submenu=tkinter.menu(menu,tearoff=0) img=tkinter.photoimage() canvas=tkinter.canvas(root,bg='white',width=800,height=600) canvas.create_image(800,600,image=img) menu.add_command(label='open',command=openitem) menu.add_command(label='save as',command=saveitem) menu.add_separator() submenu.add_command(label='curve',command=curvetype) submenu.add_command(label='line',command=linetype) submenu.add_command(label='rectangle',command=rectangletype) submenu.add_command(label='text',command=texttype) submenu.add_command(label='erase',command=erasetype) menu.add_cascade(label='pen type',menu=submenu) menu.add_separator() menu.add_command(label='foreground color',command=chooseforecolor) menu.add_command(label='bakckground color',command=choosebackcolor) menu.add_separator() menu.add_command(label='clear',command=clearitem) canvas.bind('<button-1>', onclickleft) canvas.bind('<b1-motion>',onmoveleft) canvas.bind('<buttonrelease-1>',onreleaseleft) canvas.bind('<buttonrelease-3>',onreleaseright) canvas.pack(fill=tkinter.both,expand=tkinter.yes) root.mainloop()
运行效果:
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!
上一篇: java中加过滤器实例用法汇总
下一篇: Python正则表达式的另类解答