首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >可以打开和关闭图像的Python模块

可以打开和关闭图像的Python模块
EN

Stack Overflow用户
提问于 2019-10-09 00:07:15
回答 1查看 934关注 0票数 0

我已经尝试过PIL、Matplotlib和pygame,它们都可以轻松地在一个新窗口中打开一个图像,但是它要么不能通过命令关闭,要么--在pygame的情况下--不能用新的图像重新打开。有什么模块可以做到这一点吗?

下面是代码的一个示例:

代码语言:javascript
运行
复制
#this just waits for input from the user to continue
def pause():
  ps = input("\n[Press 'Enter' to continue]\n")


image = eval(unit).image
#yes, I know eval is bad but shhhhh, it works

#the main function
def function(foo):
  #Code displays information, nothing that should cause problems
  something.display(image.png)
  #line or several lines of code to hide image()
  page(pg)

有可能发生这种事吗?唯一的要求是,无论打开什么窗口来显示窗口,都可以关闭,然后用不同的图像重新打开。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-09 01:10:00

在Linux上使用新映像重新打开PyGame窗口没有问题,但可能取决于系统。(顺便说一句:有些系统可能需要让事件显示窗口)

代码语言:javascript
运行
复制
import pygame
import time

#pygame.init()

def imshow(filename):
    #pygame.init()
    img = pygame.image.load(filename)
    size = img.get_rect().size
    screen = pygame.display.set_mode(size)
    screen.blit(img, (0, 0))
    pygame.display.flip()
    #pygame.event.clear()

def imclose():
    #pygame.quit()
    pygame.display.quit()

imshow('image1.jpg')
time.sleep(3)
imclose()

imshow('image2.png')
time.sleep(3)
imclose()

编辑:我在matplotlib中做同样的事情没有问题

代码语言:javascript
运行
复制
import matplotlib.pyplot as plt

img = plt.imread('image1.jpg')
plt.imshow(img)
plt.pause(3)
plt.close()

img = plt.imread('image2.png')
plt.imshow(img)
plt.pause(3)
plt.close()

编辑:游戏版本,关闭窗口按下键Enter/Return (或使用按钮[X])。

但是它阻止了其他代码,它必须等到你关闭窗口。

代码语言:javascript
运行
复制
import pygame

#pygame.init()

def imshow(filename):
    #pygame.init()
    img = pygame.image.load(filename)
    size = img.get_rect().size
    screen = pygame.display.set_mode(size)
    screen.blit(img, (0, 0))
    pygame.display.flip()
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: # close by button [X]
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    running = False
    pygame.display.quit()
    #pygame.quit()


imshow('image1.jpg')
imshow('image2.png')

要显示Enter可以关闭的窗口,同时运行其他命令,则必须在线程中运行PyGame

这段代码在线程中运行PyGame,您可以使用Enter/Return关闭它。如果您不关闭它,那么在其他几个命令(由imclose()模拟)之后,代码将使用sleep()关闭它。

代码语言:javascript
运行
复制
import pygame

import threading
import time

def window(filename):
    global running 

    running = True

    #pygame.init()
    img = pygame.image.load(filename)
    size = img.get_rect().size
    screen = pygame.display.set_mode(size)
    screen.blit(img, (0, 0))
    pygame.display.flip()

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: # close by button [X]
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN: # close by ENTER
                    running = False

    pygame.display.quit()
    #pygame.quit()

def imshow(filename):
    threading.Thread(target=window, args=(filename,)).start()

def imclose():
    global running
    running = False

# ----------------------------------------------------------------------------

imshow('image1.jpg')
# emulate other commands
for x in range(3):
    print('1. other command ...', x)
    time.sleep(1)
imclose() # close by command

# emulate other commands
for x in range(3):
    print('2. other command ...', x)
    time.sleep(1)

imshow('image2.jpg')
# emulate other commands
for x in range(3):
    print('3. other command ...', x)
    time.sleep(1) # emulate other code
imclose() # close by command

使用Tkinter的类似代码

代码语言:javascript
运行
复制
import tkinter as tk
from PIL import Image, ImageTk
import threading
import time

def window(filename):
    global running

    running = True

    def on_press(event):
        global running
        running = False

    root = tk.Tk()    
    photo = ImageTk.PhotoImage(Image.open(filename))
    label = tk.Label(root, image=photo)
    label.photo = photo
    label.pack()
    root.bind('<Return>', on_press) # close by ENTER
    #root.mainloop()

    while running:
        root.update()

    root.destroy()

def imshow(filename):
    threading.Thread(target=window, args=(filename,)).start()

def imclose():
    global running
    running = False

# ----------------------------------------------------------------------------

imshow('image1.jpg')
# emulate other commands
for x in range(3):
    print('1. other command ...', x)
    time.sleep(1)
imclose() # close by command

# emulate other commands
for x in range(3):
    print('2. other command ...', x)
    time.sleep(1)

imshow('image2.jpg')
# emulate other commands
for x in range(3):
    print('3. other command ...', x)
    time.sleep(1) # emulate other code
imclose() # close by command
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58295441

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档