简体   繁体   English

PyQt5 / PySide2代码仅执行带有ascii字符的作品

[英]PyQt5 / PySide2 code only executes works with ascii characters

I have made a script extension for a programm called Nuke which opens a dialog with lineedit and allows the user to enter a label. 我为名为Nuke的程序做了脚本扩展,它使用lineedit打开一个对话框,并允许用户输入标签。 However the script only executes setLabel() by pressing enter when there are only ascii characters in the lineedit. 但是,当lineedit中只有ascii字符时,脚本仅通过按Enter来执行setLabel()。

text() returns unicode and Nuke has no problem with special characters like äöü in labels if you do it through the normal ui text()返回unicode,如果您通过常规ui进行操作,Nuke不会对标签中的特殊字符(如äöü)感到满意

Here is my code: 这是我的代码:

# -*- 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()"

I had the same problem yesterday. 我昨天有同样的问题。 text() returns of type unicode and your method needs a string object, to convert just use encode('utf-8') text()返回unicode类型,并且您的方法需要一个字符串对象,只需使用encode('utf-8')即可进行转换

This might be a bit out of context, but there's easier ways to change the label for a node in Nuke using getInput if that's what you're trying to accomplish: 这可能有点脱离上下文,但是如果您要完成此操作,则有更简单的方法可以使用getInput来更改Nuke中节点的标签:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM