本文在前面文章基础上介绍tkinter添加图片和文本,在这之前,我们需要安装一个图片库,叫Pillow,这个需要下载exe文件,根据下面图片下载和安装。
下载完后直接双击安装exe,默认点击下一步,直到安装完成,会自动安装到Python3.6下的\lib\site-packages\PIL
# tkinter实现菜单功能
from tkinter import *
from PIL import Image, ImageTk
class Window(Frame):
def __init__(self, master= None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.master.title("第一个窗体")
self.pack(fill=BOTH, expand=1)
# 实例化一个Menu对象,这个在主窗体添加一个菜单
menu = Menu(self.master)
self.master.config(menu=menu)
# 创建File菜单,下面有Save和Exit两个子菜单
file = Menu(menu)
file.add_command(label='Save')
file.add_command(label='Exit', command=self.client_exit)
menu.add_cascade(label='File',menu=file)
# 创建Edit菜单,下面有一个Undo菜单
edit = Menu(menu)
edit.add_command(label='Undo')
edit.add_command(label='Show Image',command=self.showImg)
edit.add_command(label='Show Text',command=self.showTxt)
menu.add_cascade(label='Edit',menu=edit)
def client_exit(self):
exit()
def showImg(self):
load = Image.open('pic.jpg') # 我图片放桌面上
render= ImageTk.PhotoImage(load)
img = Label(self,image=render)
img.image = render
img.place(x=0,y=0)
def showTxt(self):
text = Label(self, text='GUI图形编程')
text.pack()
root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()
运行,点击Edit菜单下的Show Image,会显示一张图片,点击Show Text会出现一行文本。
来源:https://blog.csdn.net/u011541946/article/details/71374512
标签:python,图片,文本
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Go语言七篇入门教程七GC垃圾回收三色标记
2024-05-05 09:28:45
![](https://img.aspxhome.com/file/2023/3/128683_0s.gif)
Python实现桌面翻译工具【新手必学】
2021-03-27 08:32:47
关于Matplotlib绘制动态实时曲线的方法改进指南
2021-03-17 23:48:10
![](https://img.aspxhome.com/file/2023/6/115046_0s.png)
python实现指定字符串补全空格、前面填充0的方法
2022-04-06 21:13:58
Python中排序函数sorted()函数的使用实例
2021-08-01 09:52:02
![](https://img.aspxhome.com/file/2023/5/117465_0s.jpg)
java实现的连接oracle/mysql数据库功能简单示例【附oracle+mysql数据库驱动包】
2024-01-14 11:26:55
python七种方法判断字符串是否包含子串
2023-09-19 04:06:20
手把手教你导入Go语言第三方库
2024-04-28 10:46:13
![](https://img.aspxhome.com/file/2023/6/134136_0s.png)
Python OpenCV超详细讲解调整大小与图像操作的实现
2022-07-09 21:16:27
![](https://img.aspxhome.com/file/2023/2/105582_0s.png)
python字符串的一些常见实用操作
2022-05-08 15:53:30
![](https://img.aspxhome.com/file/2023/5/71375_0s.gif)
django框架之cookie/session的使用示例(小结)
2023-06-24 08:24:42
OpenCV实现背景分离(证件照背景替换)
2023-04-18 19:41:22
![](https://img.aspxhome.com/file/2023/1/104801_0s.jpg)
基于Python PaddleSpeech实现语音文字处理
2023-04-26 19:14:42
![](https://img.aspxhome.com/file/2023/1/131321_0s.jpg)
Centos7 安装 PHP7最新版的详细教程
2023-10-16 21:14:12
python中将zip压缩包转为gz.tar的方法
2022-02-28 18:50:49
Python生成pdf目录书签的实例方法
2023-10-11 15:43:21
![](https://img.aspxhome.com/file/2023/0/78780_0s.png)
pygame实现贪吃蛇游戏
2021-09-23 00:29:21
![](https://img.aspxhome.com/file/2023/2/115512_0s.jpg)
python3.7 使用pymssql往sqlserver插入数据的方法
2021-10-13 00:23:34
Go语言标准输入输出库的基本使用教程
2023-08-25 19:44:11
![](https://img.aspxhome.com/file/2023/3/101053_0s.png)
Vue前端后端的交互方式 axios
2024-05-21 10:28:58