python+tkinter一步步展示漂亮的棒棒糖和云朵

news/2025/3/4 5:15:56/

作为专题的效果,也记录下这些代码,分享给大家,希望大家喜欢。

一、先上个效果图吧

在这里插入图片描述
换一个棒棒糖的图,生活甜蜜蜜。
在这里插入图片描述
好的,下面慢慢开始介绍整个实现过程。

二、准备图片素材

在这里插入图片描述
在这里插入图片描述

三、搭个框架,上个云朵图

import tkinter
from PIL import ImageTk,Imageroot = tkinter.Tk()
root.geometry('500x500+200+100')
root.resizable(False,False)
root.overrideredirect(True)
root.wm_attributes('-transparentcolor','red')
root.wm_attributes('-topmost',1)image = Image.open('云朵3.png').resize((500,500))
image = ImageTk.PhotoImage(image)
lbImage = tkinter.Label(root,image=image)
lbImage.pack(fill=tkinter.BOTH,expand=tkinter.YES)
root.mainloop()

在这里插入图片描述
云朵出来了。

四、增加点文字

人生苦短,我用python,然后也换个棒棒糖的图。好看点。

lbImage = tkinter.Label(root,text="人生苦短\n我用python\n\n\n\n",font=("微软雅黑", 26),foreground='blue',compound="center",image=image)

完整代码:

import tkinter
from PIL import ImageTk,Imageroot = tkinter.Tk()
root.geometry('500x500+200+100')
root.resizable(False,False)
root.overrideredirect(True)
root.wm_attributes('-transparentcolor','white')
root.wm_attributes('-topmost',1)
root.config(highlightthickness=0)image = Image.open('timg.jpg').resize((500,500))
# image = Image.open('云朵3.png').resize((500,500))image = ImageTk.PhotoImage(image)
lbImage = tkinter.Label(root,text="人生苦短\n我用python\n\n\n\n",font=("微软雅黑", 26),foreground='blue',compound="center",image=image)
lbImage.config(highlightthickness=0)
lbImage.pack(fill=tkinter.BOTH,expand=tkinter.YES)root.mainloop()

五、增加鼠标移动效果

(一)记录变量

canMove = tkinter.IntVar(root,value=0)
X = tkinter.IntVar(root,value=0)
Y = tkinter.IntVar(root,value=0)

(二)鼠标左键按下

def onLeftButtonDown(event):X.set(event.x)Y.set(event.y)canMove.set(1)lbImage.bind('<Button-1>',onLeftButtonDown)

(三)鼠标左键弹起

def onLeftButtonUp(event):canMove.set(0)

(四)鼠标移动

def onLeftButtonMove(event):if canMove.get()==0:returnnewX = root.winfo_x()+(event.x-X.get())newY = root.winfo_y()+(event.y-Y.get())root.geometry(f'500x500+{newX}+{newY}')lbImage.bind('<B1-Motion>',onLeftButtonMove)

完整代码:

import tkinter
from PIL import ImageTk,Imageroot = tkinter.Tk()
root.geometry('500x500+200+100')
root.resizable(False,False)
root.overrideredirect(True)
root.wm_attributes('-transparentcolor','white')
root.wm_attributes('-topmost',1)
root.config(highlightthickness=0)image = Image.open('timg.jpg').resize((500,500))
# image = Image.open('云朵3.png').resize((500,500))image = ImageTk.PhotoImage(image)
lbImage = tkinter.Label(root,text="人生苦短\n我用python\n\n\n\n",font=("微软雅黑", 26),foreground='blue',compound="center",image=image)
lbImage.config(highlightthickness=0)
lbImage.pack(fill=tkinter.BOTH,expand=tkinter.YES)canMove = tkinter.IntVar(root,value=0)
X = tkinter.IntVar(root,value=0)
Y = tkinter.IntVar(root,value=0)def onLeftButtonDown(event):X.set(event.x)Y.set(event.y)canMove.set(1)lbImage.bind('<Button-1>',onLeftButtonDown)def onLeftButtonUp(event):canMove.set(0)lbImage.bind('<ButtonRelease-1>',onLeftButtonUp)def onLeftButtonMove(event):if canMove.get()==0:returnnewX = root.winfo_x()+(event.x-X.get())newY = root.winfo_y()+(event.y-Y.get())root.geometry(f'500x500+{newX}+{newY}')lbImage.bind('<B1-Motion>',onLeftButtonMove)root.mainloop()

六、自己动起来

import tkinter
from PIL import ImageTk,Imageroot = tkinter.Tk()
root.geometry('500x500+200+100')
root.resizable(False,False)
root.overrideredirect(True)
root.wm_attributes('-transparentcolor','white')
root.wm_attributes('-topmost',1)
root.config(highlightthickness=0)image = Image.open('timg.jpg').resize((500,500))
image = ImageTk.PhotoImage(image)
lbImage = tkinter.Label(root,# padx=10,# pady=20,# background="pink",# relief="ridge",# borderwidth=10,# 文本text="人生苦短\n我用python\n\n\n\n",font=("微软雅黑", 26),# justify="center",foreground='blue',# underline=4,# anchor="ne",# 图像# image=normal_image,compound="center",image=image)
lbImage.config(highlightthickness=0)
lbImage.pack(fill=tkinter.BOTH,expand=tkinter.YES)canMove = tkinter.IntVar(root,value=0)
X = tkinter.IntVar(root,value=0)
Y = tkinter.IntVar(root,value=0)def onLeftButtonDown(event):X.set(event.x)Y.set(event.y)canMove.set(1)lbImage.bind('<Button-1>',onLeftButtonDown)def onLeftButtonUp(event):canMove.set(0)lbImage.bind('<ButtonRelease-1>',onLeftButtonUp)def onLeftButtonMove(event):if canMove.get()==0:returnnewX = root.winfo_x()+(event.x-X.get())newY = root.winfo_y()+(event.y-Y.get())root.geometry(f'500x500+{newX}+{newY}')lbImage.bind('<B1-Motion>',onLeftButtonMove)# 定义函数
def getMove():newX = root.winfo_x()+1newY = root.winfo_y()+1root.geometry(f'500x500+{newX}+{newY}')root.after(100, getMove)  # 每隔1s调用函数 gettime 自身获取时间# 调用函数
getMove()root.mainloop()

七、不足

可以看到棒棒糖旁边有很多白色的锯齿状的东东,这个应该是图片处理的需要优化的地方,而且动态图在拖动的时候会闪动,需要优化。

八、通过canvas实现了一个版本

在这里插入图片描述
看起来会舒服些,大家有兴趣想知道怎么实现的吗?

欢迎持续关注,欲知详情如何,请听下回分解!


http://www.ppmy.cn/news/701777.html

相关文章

跟着iMeta学做图|ggplot2包绘制棒棒糖图展示变量间的相关性

如果你使用本代码&#xff0c;请引用&#xff1a;Changwu Wu. 2022. Pan-cancer analyses reveal molecular and clinical characteristics of cuproptosis regulators. iMeta 1: e68. https://doi.org/10.1002/imt2.68 代码编写及注释&#xff1a;农心生信工作室。 写在前面 …

python+tkinter+canvas实现天降棒棒糖,生活甜甜蜜蜜

pythontkintercanvas实现天降棒棒糖&#xff0c;生活甜甜蜜蜜。 一、先看看效果吧。 直接开始介绍了吧。 二、准备资源图片 通过ps或者ppt软件把这个图片抠图干净&#xff0c;就会出来的效果好看些。 三、实现逻辑 &#xff08;一&#xff09;自定义棒棒糖类 class Ball:d…

R语言绘制棒棒糖图(火柴杆图)

本博客介绍几种利用R语言绘制棒棒糖图&#xff08;火柴杆图&#xff09;的方法。 2. 使用原生ggplot方法 最容易也是最简单想到的方法是直接使用ggplot2包进行更新&#xff0c;这里需要使用ggplot本身的特性&#xff0c;通过图层叠加的方式&#xff0c;进行最终棒棒糖图的展现…

Flink运行原理

Apache Flink是什么&#xff1f;对于这个问题&#xff0c;Apache软件基金会官方给出了定义&#xff1a;Flink是一种框架和分布式处理引擎&#xff0c;主要用于对无界和有界数据流进行有状态计算。 本文将从以下几个方面来了解flink运行原理&#xff1a; 【Flink运行时四大组件…

R语言如何绘制棒棒糖图(22)

1.什么是棒棒糖图&#xff1f; 棒棒糖图&#xff0c;顾名思义&#xff0c;由点棍组成&#xff0c;形似棒棒糖。 棒棒糖图&#xff08;lollipop chart&#xff09;&#xff1a;棒棒糖图传达了与柱形图或者条形图相同的信息&#xff0c;只是将矩形转变成线条&#xff0c;这样可…

用Python绘制棒棒糖图表

条形图在数据可视化里&#xff0c;是一个经常被使用到的图表。 虽然很好用&#xff0c;也还是存在着缺陷。比如条形图条目太多时&#xff0c;会显得臃肿&#xff0c;不够直观。 棒棒糖图表则是对条形图的改进&#xff0c;以一种小清新的设计&#xff0c;清晰明了表达了我们的数…

R语言绘制棒棒糖图

ggplot2包绘制棒棒糖图 library(ggplot2) # 创建数据框 city<-c("广东","江苏","山东","浙江","河南","四川","福建","湖北","湖南","上海","安徽","…

R语言作图——Lollipop chart(棒棒糖图)

原创&#xff1a;黄小仙 今天给大家分享的是**Lollipop chart(棒棒糖图)**的画法。棒棒糖图的用途跟条形图的用法类似&#xff0c;只是看起来更加美观一些&#xff0c;图表形式更加丰富(数据不够、拿图来凑&#xff0c;啥也不能阻止我优秀 )。 为了跟之前画的柱状图更好的比较…