点击复选框时隐藏关键的PyQt警告

6 投票
1 回答
2497 浏览
提问于 2025-04-19 18:38

我正在Linux Ubuntu机器上(使用XFCE桌面环境)开发一个图形用户界面(GUI),用的是PyQt4。

我使用的是Python 2.7。

一切运行得很好,除了当我点击一个复选框时,控制台会显示一条消息(虽然这并不影响GUI的运行):

(python:9482): Gtk-CRITICAL **: IA__gtk_widget_get_direction: assertion 'GTK_IS_WIDGET (widget)' failed

这是我用来显示复选框列表的代码的一部分:

def SFCheckList(self):
    groupBox = QtGui.QGroupBox("SF Functions List")
    self.grid = QtGui.QGridLayout()
    self.checkBoxList = [[], []]

    #Reading supported SF functions
    try:
        sql = "SELECT SF FROM Locators"
        cursor.execute(sql)
        results = cursor.fetchall()
    except:
        print "Error: unable to fecth data (SF Functions)"


    #Building the Checkbox List
    for SF in results:
        self.checkBoxList[0].append(QtGui.QCheckBox(SF[0])) 
        self.checkBoxList[1].append(SF[0])

    self.SFCheckListDisplay()
    groupBox.setLayout(self.grid)

    return groupBox

def SFCheckListDisplay(self):
    l = sqrt(len(self.checkBoxList[0]))
    if(l!=int(l)):
        l= int(l) + 1

    i=0
    j=0
    for cb in self.checkBoxList[0]:
        self.grid.addWidget(cb, i, j)
        j+=1
        if(j==l):
            i+=1
            j=0

我在控制台里获取一些测试信息。
我该如何防止这个消息出现呢?

谢谢!

1 个回答

7

我在Windows 7上用PyQt5时遇到了类似的问题,代码如下:

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Window(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)
        self.stderr_backup = None

        self.label = QLabel("Test", self)

        self.layout = QHBoxLayout()
        self.layout.addWidget(self.label)

        self.setLayout(self.layout)

        self.show()

app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())

Qt给出的错误信息是这样的,不过应用程序还是正常运行。

QWindowsWindow::setGeometry: Unable to set geometry 42x35+520+270 on QWidgetWindow/'WindowClassWindow'. Resulting geometry:  112x35+520+270 (frame: 8, 29, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 42x35, maximum size: 16777215x16777215).

我通过以下方法关闭了这个信息:

def handler(msg_type, msg_log_context, msg_string):
    pass

PyQt5.QtCore.qInstallMessageHandler(handler)

不过,我不确定这个方法在PyQt4上是否有效,也不确定对你来说是否适用。

撰写回答