PyQt5 / PySide2代码仅执行ascii字符的工作

时间:2018-05-24 19:46:41

标签: python unicode ascii pyqt5 pyside2

我为一个名为Nuke的程序创建了一个脚本扩展,它使用lineedit打开一个对话框,允许用户输入标签。但是,当lineedit中只有ascii字符时,脚本只按Enter键执行setLabel()。

text()返回unicode,如果通过正常的ui执行此操作,Nuke对标签中的äöü等特殊字符没有任何问题

这是我的代码:

# -*- coding: utf-8 -*-
from PySide2 import QtCore, QtGui, QtWidgets
import nuke
import sys
import os

class setLabelTool(QtWidgets.QDialog):
    def __init__(self, node):
        self.n = node
        super(setLabelTool, self).__init__()
        self.setObjectName("Dialog")
        self.setWindowTitle("Test")
        self.setFixedSize(199, 43)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)

        self.lineEdit = QtWidgets.QLineEdit(self)
        self.lineEdit.setGeometry(QtCore.QRect(10, 10, 181, 25))
        self.lineEdit.setObjectName("lineEdit")

        self.lineEdit.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.lineEdit.setAlignment(QtCore.Qt.AlignHCenter)

        currentlabel = self.n['label'].value()
        if len(currentlabel) == 0:
            self.lineEdit.setPlaceholderText("Set node label")
        else:
            self.lineEdit.setText(currentlabel)
            self.lineEdit.selectAll()

        self.lineEdit.returnPressed.connect(self.setLabel)

    def setLabel(self):
        label = self.lineEdit.text()
        self.n['label'].setValue(label)
        print ("Node: " + self.n['name'].value() + " labeled " + label)
        self.close()

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_Escape:
            print "Exit setLabelTool"
            self.close()

def showLabelTool():
    n = nuke.selectedNodes()[-1]
    if n != None:
        Tool = setLabelTool(n)
        Tool.exec_()
    else:
        print "Error in showLabelTool()"

2 个答案:

答案 0 :(得分:0)

昨天我遇到了同样的问题。 text()返回unicode类型,你的方法需要一个字符串对象,转换只需使用function strokeincr() { var circl = document.getElementById("circ"); var r = parseInt(circl.getAttribute("r")); circl.setAttribute("r", r + 10); }

答案 1 :(得分:0)

这可能有点脱离了上下文,但如果您正在尝试完成的事情,可以使用getInput更轻松地更改Nuke中节点的标签:

new_label = nuke.getInput("enter label")
nuke.selectedNode()['label'].setValue(new_label)