使用图像作为按钮tkinter python 3.6
我希望在我的石头剪刀游戏中使用图像作为按钮,但是我只能使用3.6中没有的模块(如PIL)找到python旧版本的答案,如果有帮助,请使用jpg文件)使用图像作为按钮tkinter python 3.6,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我希望在我的石头剪刀游戏中使用图像作为按钮,但是我只能使用3.6中没有的模块(如PIL)找到python旧版本的答案,如果有帮助,请使用jpg文件) 谢谢这里有一个简单的例子: 代码示例: 图像文件 应用程序运行示例: 我刚刚发现3.5 xd有PIL可用。是的,实际上有一个PIL叉正在维护中(称为pillow),您也可以安装它,它的工作原理几乎相同。jpg更难满足您需要pillow。Tkinter默认支持Png和Gif(非动画和透明背景)。您可以考虑将图像格式转换为Png或Gif。 impo
谢谢这里有一个简单的例子: 代码示例: 图像文件 应用程序运行示例:
我刚刚发现3.5 xd有PIL可用。是的,实际上有一个PIL叉正在维护中(称为pillow),您也可以安装它,它的工作原理几乎相同。jpg更难满足您需要pillow。Tkinter默认支持Png和Gif(非动画和透明背景)。您可以考虑将图像格式转换为Png或Gif。
import pygame
# --- class ---
class imageButton(object):
def __init__(self, position, size):
image1 = pygame.image.load('rock.png')
image2 = pygame.image.load('paper.png')
image3 = pygame.image.load('scissors.png')
self._images = [
pygame.Surface(size),
pygame.Surface(size),
pygame.Surface(size),
]
# create 3 images
self._images[0].blit(image1, image1.get_rect())
self._images[1].blit(image2, image2.get_rect())
self._images[2].blit(image3, image3.get_rect())
# get image size and position
self._rect = pygame.Rect(position, size)
# select first image
self._index = 0
def draw(self, screen):
# draw selected image
screen.blit(self._images[self._index], self._rect)
def event_handler(self, event):
# change selected color if rectange clicked
if event.type == pygame.MOUSEBUTTONDOWN: # is some button clicked
if event.button == 1: # is left button clicked
if self._rect.collidepoint(event.pos): # is mouse over button
self._index = (self._index + 1) % 3 # change image
# --- main ---
# init
pygame.init()
screen = pygame.display.set_mode((320, 110))
# create buttons
button1 = imageButton((5, 5), (100, 100))
button2 = imageButton((110, 5), (100, 100))
button3 = imageButton((215, 5), (100, 100))
# mainloop
running = True
while running:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
button1.event_handler(event)
button2.event_handler(event)
button3.event_handler(event)
# --- draws ---
button1.draw(screen)
button2.draw(screen)
button3.draw(screen)
pygame.display.update()
# --- the end ---
pygame.quit()