对于背景,我正在运行 Debian Lenny,并已在 GNOME 和 Fluxbox 上尝试过。
无论如何,我一直在研究如何在桌面上绘图,我在这里找到并尝试了这段代码: http://blog.prashanthellina.com/2007/08/24/drawing-on-your-desktop/
它工作正常,除了在终止它时(通过点击控制 C),X 失去了创建新窗口的能力。
我以为问题可能是 pygame 没有释放一些资源,所以我添加了一段代码来捕获 kill 信号,给了我以下内容:
"""
Run the following command in the shell before executing this script
export SDL_WINDOWID=`xwininfo -root|grep "id:"|sed 's/^.*id: //'|sed 's/ (.*$//'`
"""
import pygame
import sys
import random
import time
import signal
pygame.init()
window = pygame.display.set_mode((1280, 1024))
screen = pygame.display.get_surface()
def handle_sigint(signum, frame):
"""I want to ensure resources are released before bailing."""
print("SIGINT received.");
pygame.display.quit()
pygame.quit()
sys.exit(0)
# Set handler to catch C^C Interupts
signal.signal(signal.SIGINT, handle_sigint)
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit(0)
x = random.choice(range(640))
y = random.choice(range(480))
radius = random.choice(range(100))
col_r = random.choice(range(255))
col_g = random.choice(range(255))
col_b = random.choice(range(255))
time.sleep(.03)
rect = pygame.draw.circle(screen, (col_r, col_g, col_b), (x,y), radius)
pygame.display.update(rect)
所以我又试了一次。中断处理程序中的打印语句告诉我,当我退出时处理程序确实运行了,但我仍然遇到同样的问题。更有趣的是,X 在运行时没有任何问题。只有在终止它之后。
可能有人知道发生了什么,以及我可以做些什么来修复代码,以免它破坏我的 X session ?提前致谢。
最佳答案
问题、想法和要尝试的事情:
- 如果您在尝试使用 -c 之前启动了终端,您还能在之后输入吗?如果是,则可能是您的 session 或窗口管理器出现问题,而不是 x 服务器。
- 您尝试过 Ctrl-Alt-Backspace 组合吗?这能解决您的问题吗?
- gtk-window-decorator 和 x-session-manager 是否仍在运行?
pygame 通常会在窗口外运行,并且通常会尝试自行清理。您已经添加了对 pygame.display.quit() 的显式调用,但我认为这不会改变任何东西——pygame 会尝试删除 SDL_WINDOWID 变量引用的窗口。实际上成功删除根窗口可能是一件坏事。我猜你从那个运行 ubuntu 的人那里得到这个,pygame 无法删除窗口,因为他没有权限。你可能在你的操作系统上。
既然杀死你的根窗口是不好的,那么将根窗口的控制权恢复到 nautilus (gnome) 怎么样?类似于 gconftool-2 --type bool --set/apps/nautilus/preferences/show_desktop true
?
关于linux - 为什么在我绘制到根窗口后 X 会卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1302976/