Python gui中的意外行为

Python gui中的意外行为,python,pyqt,python-2.x,Python,Pyqt,Python 2.x,两天来,我一直在忍受一个奇怪的错误,我不明白有什么问题需要你的帮助。我有一个独特的特点 def find_config(self, path): dic = list(path.split('/')) print dic size = len(dic) print '/'.join(dic) 当我在类构造函数中运行它时,它工作正常,但当我在事件处理程序按钮中运行它时,它挂起在join函数上 建造商: class MainWindow(QtGui.QMainWin

两天来,我一直在忍受一个奇怪的错误,我不明白有什么问题需要你的帮助。我有一个独特的特点

def find_config(self, path):
    dic = list(path.split('/'))
    print dic
    size = len(dic)
    print '/'.join(dic)
当我在类构造函数中运行它时,它工作正常,但当我在事件处理程序按钮中运行它时,它挂起在join函数上

建造商:

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
            QMainWindow.__init__(self)
            self.setupUi(self)
            self.filepath = ""
            self.action_Open.activated.connect(self.file_open_func)

            print self.find_config('/home/juster/asdfa/main.c')
处理程序按钮:

def file_open_func(self, path = 0):

    try:
        self.filepath = 0;
        if not path:        
            self.filepath = QFileDialog.getOpenFileName(self, 'Open file', self.rootpath, "C/C++ (*.c);; All (*.*);; Makefile (makefile)")  
        else:
            self.filepath = path

        print self.find_config(self.filepath)
        f = open(self.filepath, 'a+')
看看我给终端变量dic带来了什么

函数find_config from constructor:

['', 'home', 'juster', 'asdfa', 'main.c']
函数find_config from handler:

[PyQt4.QtCore.QString(u''), PyQt4.QtCore.QString(u'home'), PyQt4.QtCore.QString(u'juster'), PyQt4.QtCore.QString(u'asdfa'), PyQt4.QtCore.QString(u'main.c')]

这很神奇吗?

从处理程序调用时,看起来您的
路径
字符串不是常规的Python
str
,而是QT类
QString
的实例。这似乎适用于
拆分
,但不适用于
加入
。我想如果您使用
str
将其转换为常规字符串,您会发现问题消失了

def find_config(self, path):
    dic = list(str(path).split('/')) # added str() call to this line
    print dic
    size = len(dic)
    print '/'.join(dic)

请注意,您的
dic
变量有一个相当容易引起误解的名称。它是一个列表,而不是一个字典,因此称它为
dic
会引起混淆(尽管创建时的
list
调用似乎没有必要)。我也不确定这个函数做什么。它似乎拆分了一个字符串,然后完全按原样重新连接它。

为什么您认为
它挂在连接函数上
您应该使用
os.path
来进行路径名操作。错误是什么?你没有提到任何错误,也没有发布回溯。