为什么我绘制到根窗口后 X 会卡住
对于背景,我正在跑步 Debian Lenny,并已在 GNOME 和 Fluxbox 上尝试过此操作。
不管怎样,我一直在研究如何在桌面上绘图,我在这里找到并尝试了这段代码: http://blog.prashanthellina.com/2007/08 /24/drawing-on-your-desktop/
它工作得很好,除了终止它(通过按下 Control C),X 失去了创建新窗口的能力。
我原以为问题可能是 pygame 没有释放一些资源,所以我添加了一段代码来捕获终止信号,给我以下内容:
"""
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 会话? 提前致谢。
For background, I'm running
Debian Lenny, and have tried this with both GNOME and Fluxbox.
Anyway, I've been looking at how to draw on the desktop, and I found and tried this code here:
http://blog.prashanthellina.com/2007/08/24/drawing-on-your-desktop/
It worked fine, except upon terminating it (by hitting control C), X loses it's ability to create new windows.
I had thought that maybe the problem was pygame not releasing some resource, so I added in a block of code to trap the kill signal, giving me the following:
"""
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)
And so I tried again. The print statement in the interrupt handler tells me that the handler does run when I quit, but I still have the same problem. And even more interestingly, X has no problems while it's running. It's only after terminating it.
Might anybody out there have any idea what's happening, and what I can do to fix the code so it doesn't wreck my X session? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题、想法和 值得尝试的事情:
pygame 通常会在窗口外运行,并且通常会尝试自行清理。 您已添加对 pygame.display.quit() 的显式调用,但我认为这不会改变任何内容 - pygame 尝试删除 SDL_WINDOWID 变量引用的窗口。 实际上,成功删除根窗口可能是一件坏事。 我猜你从运行 ubuntu、pygame 那里得到这个的人无法删除窗口,因为他没有权限。 您可能在您的操作系统上。
既然杀死你的根窗口是不好的,那么将根窗口的控制权恢复到 nautilus (gnome) 怎么样? 像 gconftool-2 --type bool --set /apps/nautilus/preferences/show_desktop true 之类的东西?
Questions, ideas & things to try:
pygame is typically run out of a window and is normally going to try to cleanup after itself. You've added an explicit call to pygame.display.quit(), but I don't think this changes anything - pygame tries to delete the window referred to by the SDL_WINDOWID variable. Actually succeeding in deleting your root window is probably a bad thing. I'm going to guess that the guy you got this from, running ubuntu, pygame fails to delete the window because he doesn't have permission. You might on your OS.
Since killing your root window is bad, how about just restoring control of the root window back to nautilus (gnome)? something like
gconftool-2 --type bool --set /apps/nautilus/preferences/show_desktop true
?