首页 > 解决方案 > PyQt5:截图给​​出错误(xcb)

问题描述

我是 python 新手,所以这可能很容易,如果是的话,我很抱歉,但我找不到这样做的方法。问题是当我调用 snip() 时,无论在哪里,它都会截取一次屏幕截图,但是当我再次按下按钮时,它会停止、冻结然后强制退出。到目前为止,我遇到了 2 个不同的错误。一个是以下,每当我第二次单击该按钮时:

Gdk 消息:python3:X 服务器上的致命 IO 错误 11(资源暂时不可用):0。被杀

我在更大的项目中找到的另一个是以下一个:

[xcb] 处理队列时序列号未知

[xcb] 这很可能是一个多线程客户端并且 XInitThreads 没有被调用

[xcb] 中止,抱歉。

python3:../../src/xcb_io.c:259:poll_for_event:断言'!xcb_xlib_threads_sequence_lost'失败。

中止

有没有办法在我每次单击按钮时截取屏幕而不出现此问题?这是问题的简化代码。感谢你们提供的任何帮助。

import pyscreenshot as ImageGrab
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys


class MyWindow(QMainWindow):
    def __init__(self):
        super(MyWindow,self).__init__()
        self.initUI()

    def button_clicked(self):
        self.label.setText("you pressed the button")
        self.snip()     #heres the problem
        self.update()

    def initUI(self):
        self.setGeometry(200, 200, 300, 300)
        self.setWindowTitle("Tech With Tim")

        self.label = QtWidgets.QLabel(self)
        self.label.setText("my first label!")
        self.label.move(50,50)

        self.b1 = QtWidgets.QPushButton(self)
        self.b1.setText("click me!")
        self.b1.clicked.connect(self.button_clicked)

    def update(self):
        self.label.adjustSize()

    def snip(self):
        img = ImageGrab.grab(bbox=(200, 300, 400, 500))
        img.show()

def window():
    app = QApplication(sys.argv)
    win = MyWindow()
    win.show()
    sys.exit(app.exec_())

window()

标签: pythonpyqt

解决方案


推荐阅读