1

我正在尝试根据 Photoshop Script UI 复选框的状态更改一些标签文本。只是它根本不更新。不知道为什么。

理想情况下,我希望它在勾选时说“一些文本” ,而在未选中时说“一些其他文本”。并动态变化。

简单的用户界面

这是我的代码

// DIALOG
// ======
var dlg = new Window("dialog");
   dlg.text = "Title"; 
   dlg.preferredSize.width = 180; 
   dlg.preferredSize.height = 100; 
   dlg.orientation = "column"; 
   dlg.alignChildren = ["center","top"]; 
   dlg.spacing = 10; 
   dlg.margins = 16; 

var statictext1 = dlg.add("statictext", undefined, undefined, {name: "statictext1"}); 
   statictext1.text = "Some static text"; 
   statictext1.justify = "center"; 

var checkbox1 = dlg.add("checkbox", undefined, undefined, {name: "checkbox1"}); 
   checkbox1.text = "Some text";
   checkbox1.value = true;
   

// GROUP1
// ======
var group1 = dlg.add("group", undefined, {name: "group1"}); 
    group1.orientation = "row"; 
    group1.alignChildren = ["left","center"]; 
    group1.spacing = 10; 
    group1.margins = 0; 

// add buttons
group1.add ("button", undefined, "OK");
group1.add ("button", undefined, "Cancel");

// Define behavior for when the slider value changes
dlg.checkbox1.onChanging = function() 
{
   var textArr = ["Some text", "Some other text"]
   var val = dlg.checkbox1.value;
   // Update the label text with the current checkbox value.
   dlg.checkbox1.text = textArr[val];
}

var myReturn = dlg.show();
4

1 回答 1

1

Checkbox控件触发事件而onClick不是onChanging事件。

触发onChanging事件的唯一控件是:

  • EditText
  • Scrollbar
  • Slider

因此,请考虑在脚本中更改此部分:

// Define behavior for when the slider value changes
dlg.checkbox1.onChanging = function() {
  // ...
}

改为:

// Define behavior for when the checkbox value changes
dlg.checkbox1.onClick = function() {
   var textArr = ["Some text", "Some other text"]
   var val = checkbox1.value ? textArr[0] : textArr[1];
   statictext1.text = val;
}

解释

onClick事件的前一个函数的主体中,即读取的部分;

var val = checkbox1.value ? textArr[0] : textArr[1];

我们利用条件(三元)运算符来检查 的value属性checkbox1。如果checkbox1选中(true)我们将字符串分配给"Some text"变量val,否则如果checkbox1未选中(false)我们将字符串分配给"Some other text"变量val

其他变化:

还需要进行以下更改:

  1. 由于checkbox1' 的初始value设置为true,即已检查,您需要将第 13 行更改为:

    statictext1.text = "Some static text";
    

    对此:

    statictext1.text = "Some text";
    
  2. 还可以考虑为 设置size属性statictext1。正如您在下面的第 15 行中看到的那样,添加了以下内容:

    statictext1.size = [180, 20];
    

    这将确保它的宽度适应"Some other text"字符串。

修改后的脚本:

这是修改后的完整脚本:

// DIALOG
// ======
var dlg = new Window("dialog");
    dlg.text = "Title";
    dlg.preferredSize.width = 180;
    dlg.preferredSize.height = 100;
    dlg.orientation = "column";
    dlg.alignChildren = ["center","top"];
    dlg.spacing = 10;
    dlg.margins = 16;

var statictext1 = dlg.add("statictext", undefined, undefined, {name: "statictext1"});
    statictext1.text = "Some text"; // <--- Note: Also change this !
    statictext1.justify = "center";
    statictext1.size = [180, 20];   // <--- Note: Also set the width/height !

var checkbox1 = dlg.add("checkbox", undefined, undefined, {name: "checkbox1"});
    checkbox1.text = "Some text";
    checkbox1.value = true;


// GROUP1
// ======
var group1 = dlg.add("group", undefined, {name: "group1"});
    group1.orientation = "row";
    group1.alignChildren = ["left","center"];
    group1.spacing = 10;
    group1.margins = 0;

// add buttons
group1.add ("button", undefined, "OK");
group1.add ("button", undefined, "Cancel");

// Define behavior for when the checkbox value changes
dlg.checkbox1.onClick = function() {
   var textArr = ["Some text", "Some other text"]
   var val = checkbox1.value ? textArr[0] : textArr[1];
   statictext1.text = val;
}

var myReturn = dlg.show();
于 2022-02-18T22:01:10.613 回答