Python scripts: handling PF_OPTION and PF_RADIO choices by name - Printable Version +- Gimp-Forum.net (https://www.gimp-forum.net) +-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP) +--- Forum: Tutorials and tips (https://www.gimp-forum.net/Forum-Tutorials-and-tips) +--- Thread: Python scripts: handling PF_OPTION and PF_RADIO choices by name (/Thread-Python-scripts-handling-PF-OPTION-and-PF-RADIO-choices-by-name) |
Python scripts: handling PF_OPTION and PF_RADIO choices by name - Ofnuts - 07-29-2018 Handling PF_OPTION in scripts is always a bit of a problem when there are many of them or long list of choices. Here is a technique that has several advantages:
Code: from collections import namedtuple Then you define your options as a list of (name,label) tuples, where name is how you refer to that option in the code, and label is how it appears in the PF_OPTION or PF_RADIO widget: Code: [('NONE','None'),('LINKED','Linked layers'),('TEXT','Text layers')] To create the object that will carry the options, you call the function above, giving it a name, and the list of tuples, and keep the result is a variable: Code: MergeOptions=createOptions('Merge',[('NONE','None'),('LINKED','Linked layers'),('TEXT','Text layers')]) The name as passed to the function is not too important, it just needs to be unique among your various set of options. What really counts is the name of the variable in which you keep the object. In the resulting variable:
Code: (PF_OPTION, 'mergeLayers', 'Merge layers',MergeOptions.TEXT,MergeOptions.labels) or your PF_RADIO like this: Code: (PF_RADIO, 'mergeLayers', 'Merge layers',MergeOptions.TEXT,MergeOptions.labelTuples) To test an option in the code you just use its name: Code: if merge==MergeOptions.TEXT: or even: Code: if merge in [MergeOptions.TEXT,MergeOptions.LINKED]: Happy coding. RE: Python scripts: handling PF_OPTION choices by name - trandoductin - 07-30-2018 Thanks! RE: Python scripts: handling PF_OPTION choices by name - lordcayleth - 08-15-2018 I'm rather new to python. Can you please give me an example how to use this convention with PF_RADIO? I suspect there is a way to use the existing data structure to give PF_RADIO the pairs it needs the way it wants them, but I don't have a clue how to do it. Thanks. RE: Python scripts: handling PF_OPTION choices by name - Ofnuts - 08-15-2018 (08-15-2018, 10:19 PM)lordcayleth Wrote: I'm rather new to python. For radio button the function to create the symbols and labels is a bit different: For instance, taking $random_script: Code: #!/usr/bin/env python Independently of the use of the Directions object, you will note that the registration is a bit different. There are now explicit Image/Drawable parameters. When the filter is called as usual, Gimp automatically fills them with the current image and drawable and the dialog only shows widgets for the 3rd parameter and next. But if you use Filters>Re-run|Re-show (that now work...) these parameters have matching widgets in the dialog. EDIT: updated initial post with function that creates an object usable with both PF_OPTIONS and PF_RADIO. Incidentally, PF_RADIO is seldom used because it takes a lot of vertical space. RE: Python scripts: handling PF_OPTION choices by name - lordcayleth - 08-16-2018 (08-15-2018, 11:17 PM)Ofnuts Wrote: You've been super helpful, and I thank you a lot. I have a new respect for the power and flexibility of python, too. I only have one more question. What would I look up to gain a better understanding of how the for/in syntax works in this code? I'm quite curious about it, but I don't even know what terminology it would fall under. RE: Python scripts: handling PF_OPTION and PF_RADIO choices by name - Ofnuts - 08-16-2018 Generating a list by [expression(something) for something in some_iteration] is called a "comprehension". Tremendously useful. If you want to really "grok" python, there are several Youtube videos with Ned Batchelder. He goes into some very important (and often overlooked) concepts. Python-forum.io is also a a good place to ask general Python questions. |