我已经尝试过PIL、Matplotlib和pygame,它们都可以轻松地在一个新窗口中打开一个图像,但是它要么不能通过命令关闭,要么--在pygame的情况下--不能用新的图像重新打开。有什么模块可以做到这一点吗?
下面是代码的一个示例:
#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)
有可能发生这种事吗?唯一的要求是,无论打开什么窗口来显示窗口,都可以关闭,然后用不同的图像重新打开。
发布于 2019-10-09 01:10:00
在Linux上使用新映像重新打开PyGame
窗口没有问题,但可能取决于系统。(顺便说一句:有些系统可能需要让事件显示窗口)
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
中做同样的事情没有问题
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]
)。
但是它阻止了其他代码,它必须等到你关闭窗口。
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()
关闭它。
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
的类似代码
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
https://stackoverflow.com/questions/58295441
复制相似问题