python tkinter GUI绘制,以及点击更新显示图片代码
作者:未名亚柳 时间:2021-07-27 14:11:33
tkinter 绘制GUI简单明了,制作一些简单的GUI足够,目前遇到的一个问题是不能同时排列显示多幅图片(目前没找到同时显示解决方法),
退而求其次,改成增加一个update按钮,每次点下按钮自动更新图片,注释掉的部分是原来的内核包,替换成自己的程序即可
import tkinter as tk
from tkinter import *
from tkinter import ttk
from urllib.request import urlopen
from PIL import Image, ImageTk
#import make_pic_2 在这里导入自己的py包文件
num=1
url = "./PICTURE/Rain.png"def calculate(*args):
print(addr.get(),":",port.get())
#make_pic_2.main(int(fft.get()),int(start.get()),int(stop.get()),int(cent.get()))
#调用自己的内核函数def change(): #更新图片操作
global num
num=num+1
if num%3==0:
url1="./PICTURE/Rain.png"
pil_image = Image.open(url1)
img= ImageTk.PhotoImage(pil_image)
label_img.configure(image = img)
if num%3==1:
url1="./PICTURE/Oscillogram.png"
pil_image = Image.open(url1)
img= ImageTk.PhotoImage(pil_image)
label_img.configure(image = img)
if num%3==2:
url1="./PICTURE/Spectrum.png"
pil_image = Image.open(url1)
img= ImageTk.PhotoImage(pil_image)
label_img.configure(image = img)
root.update_idletasks() #更新图片,必须updateroot = Tk()
root.title("Draw GUI")mainframe = ttk.Frame(root, padding="5 4 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
addr = StringVar()
port = StringVar()
fft = StringVar()
cent = StringVar()
start= StringVar()
stop = StringVar()ttk.Label(mainframe, text="Address:").grid(column=1, row=1, sticky=W)
addr_entry = ttk.Entry(mainframe, width=7, textvariable=addr)
addr_entry.grid(column=2, row=1, sticky=(W, E))ttk.Label(mainframe, text="Port:").grid(column=3, row=1, sticky=W)
port_entry = ttk.Entry(mainframe, width=7, textvariable=port)
port_entry.grid(column=4, row=1, sticky=(W, E))ttk.Label(mainframe, text="FFt:").grid(column=1, row=2, sticky=W)
fft_entry = ttk.Combobox(mainframe, width=7, textvariable=fft)
fft_entry['values'] = (2048, 4096, 8192)
fft_entry.current(1)
fft_entry.grid(column=2, row=2, sticky=(W, E))ttk.Label(mainframe, text="CenterFR:").grid(column=3, row=2, sticky=W)
cent_entry = ttk.Entry(mainframe, width=7, textvariable=cent)
cent_entry.grid(column=4, row=2, sticky=(W, E))ttk.Label(mainframe, text="StartFR:").grid(column=1, row=3, sticky=W)
start_entry = ttk.Entry(mainframe, width=7, textvariable=start)
start_entry.grid(column=2, row=3, sticky=(W, E))ttk.Label(mainframe, text="StopFR:").grid(column=3, row=3, sticky=W)
stop_entry = ttk.Entry(mainframe, width=7, textvariable=stop)
stop_entry.grid(column=4, row=3, sticky=(W, E))ttk.Button(mainframe, text="Draw!", command=calculate).grid(column=2, row=4, sticky=W)
ttk.Button(mainframe, text="Update!", command=change).grid(column=4, row=4, sticky=W)
pil_image = Image.open(url)
img= ImageTk.PhotoImage(pil_image)
label_img = ttk.Label(root, image = img ,compound=CENTER)
label_img.grid(column=0,row=5, sticky=W)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
addr_entry.focus()root.bind('<Return>', calculate) #主循环,除了这一行可以一直循环,其他行只执行一次root.mainloop()
来源:https://www.cnblogs.com/dzzy/p/9431824.html
标签:python,tkinter,GUI,图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python 面向对象编程的三大特性之继承
2021-08-18 18:19:04
![](https://img.aspxhome.com/file/2023/2/79412_0s.png)
Python Matplotlib初阶使用入门教程
2022-03-17 21:02:33
![](https://img.aspxhome.com/file/2023/9/99169_0s.png)
Python中的左斜杠、右斜杠(正斜杠和反斜杠)
2022-02-08 07:00:34
numpy中的meshgrid函数的使用
2021-10-07 06:11:39
link 和 style 元素在 HTML 文档中的位置
2008-06-02 13:56:00
Python 动态变量名定义与调用方法
2023-07-29 22:36:05
Python 编程速成(推荐)
2021-04-03 04:30:10
Python同步遍历多个列表的示例
2023-08-08 00:20:10
![](https://img.aspxhome.com/file/2023/4/95744_0s.jpg)
css中浮动思考与小结
2008-10-30 11:57:00
Python实现的石头剪子布代码分享
2023-04-11 09:14:58
PHP中使用cURL实现Get和Post请求的方法
2023-08-18 13:15:35
利用PyQt5中QLabel组件实现亚克力磨砂效果
2023-12-13 18:33:04
![](https://img.aspxhome.com/file/2023/5/72125_0s.jpg)
检测SQL Server是否有特洛伊木马
2009-02-24 15:19:00
python实现学生通讯录管理系统
2023-05-06 22:00:05
Dreamweaver MX弹出窗口全攻略
2010-09-05 21:14:00
![](https://img.aspxhome.com/file/UploadPic/20072/20072311313962s.jpg)
详解使用Python处理文件目录的相关方法
2023-08-01 02:55:30
最简单的tab切换实例代码
2023-08-22 08:38:59
![](https://img.aspxhome.com/file/2023/6/56006_0s.jpg)
Python实现双向链表基本操作
2022-07-22 10:14:12
![](https://img.aspxhome.com/file/2023/4/99214_0s.jpg)
Python不支持 i ++ 语法的原因解析
2022-02-07 12:16:49
![](https://img.aspxhome.com/file/2023/7/67237_0s.jpg)
关于DOM元素定位属性的深入学习
2008-04-02 11:46:00