Skip to main content
Solved

Use Python variable in an attribute

  • May 15, 2023
  • 7 replies
  • 107 views

quelozantonin
Contributor

Hello,

 

Could someone tell me how, from a PythonCaller calling a dropdown list via the PyQt5 module, to use the selected string (in the example below, the "select" variable) in the list to fill in a feature attribute and then leave the dialog box once the choice is made?

# coding: utf-8
from PyQt5.QtWidgets import QApplication, QWidget , QComboBox
import sys    
 
def actionSelect():
    select = qcombo.currentText()
    print("Selected street : " + select)    
app = QApplication(sys.argv)
root = QWidget()
root.setWindowTitle("Street Selected")
root.setGeometry(100 , 100 , 500 , 300)
 
# création de la liste QCombobox
qcombo = QComboBox(root)
qcombo.setGeometry(100 , 10 , 150 , 30)
qcombo.activated.connect(actionSelect)
# création de la liste des items
L = ["Street A, City A" , "Street B, City B" , "Street C, City C"]
# ajout des items à la liste QCombobox
qcombo.addItems(["Street A, City A" , "Street B, City B" , "Street C, City C"])
root.show()
sys.exit(app.exec_())

For example, in the example below:

image.pngGive this result:

image 

Thanks a lot in advance for your help 🙏 

 

Antonin

Best answer by david_r

huge thank you David, this is exactly what I wanted 💃 🏆 🙏  and it work perfectly!!!

Try replacing line 23 with this:

list_items = feature.getAttribute('array{}.display_name') or []
dlg = StreetNameDialog(list_items)

 

 

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

7 replies

david_r
Celebrity
  • 8391 replies
  • May 15, 2023

I've got very little experience with Qt, but you could try to make the dialog box modal to prevent the "asynchronous" behavior. You could then retrieve the value of the combobox using either a class instance member, or a global variable.


quelozantonin
Contributor
  • Author
  • Contributor
  • 8 replies
  • May 15, 2023

Can you propose me an example in my original code ?

I'm a beginner in Python and I don't know which code and where I need to define the attribute.

I've try to put

feature.setAttribute("Street",select)
self.pyoutput(feature)

in "def actionSelect():" but it doesn't work 😓 


david_r
Celebrity
  • 8391 replies
  • May 15, 2023

Try something like this to let the user select from a list of predefined items:

import fme
import fmeobjects
from PyQt5.QtWidgets import (QApplication, QWidget, QInputDialog)
 
class StreetNameDialog(QWidget):
    
    def __init__(self, items):
        super().__init__()
        self.text = ''
        self.showDialog(items)
 
    def showDialog(self, items):
        text, ok = QInputDialog.getItem(self, 'Street', 'Street name:', items, editable=False)
        if ok:
            self.text = str(text)
 
class SelectStreet(object):
    def __init__(self):
        pass
 
    def input(self, feature):
        app = QApplication([])
        dlg = StreetNameDialog(["Street A, City A" , "Street B, City B" , "Street C, City C"])
        feature.setAttribute('Street', dlg.text)
        
        self.pyoutput(feature)
                               
    def close(self):
        pass

Note that the PythonCaller must be configured to call the "SelectStreet" class:

imageExample:

imageThe resulting attribute "Street" will contain an empty string if the user selects Cancel, otherwise the text of the selected list item.


quelozantonin
Contributor
  • Author
  • Contributor
  • 8 replies
  • May 16, 2023

Try something like this to let the user select from a list of predefined items:

import fme
import fmeobjects
from PyQt5.QtWidgets import (QApplication, QWidget, QInputDialog)
 
class StreetNameDialog(QWidget):
    
    def __init__(self, items):
        super().__init__()
        self.text = ''
        self.showDialog(items)
 
    def showDialog(self, items):
        text, ok = QInputDialog.getItem(self, 'Street', 'Street name:', items, editable=False)
        if ok:
            self.text = str(text)
 
class SelectStreet(object):
    def __init__(self):
        pass
 
    def input(self, feature):
        app = QApplication([])
        dlg = StreetNameDialog(["Street A, City A" , "Street B, City B" , "Street C, City C"])
        feature.setAttribute('Street', dlg.text)
        
        self.pyoutput(feature)
                               
    def close(self):
        pass

Note that the PythonCaller must be configured to call the "SelectStreet" class:

imageExample:

imageThe resulting attribute "Street" will contain an empty string if the user selects Cancel, otherwise the text of the selected list item.

huge thank you David, this is exactly what I wanted 💃 🏆 🙏  and it work perfectly!!!


quelozantonin
Contributor
  • Author
  • Contributor
  • 8 replies
  • May 16, 2023

A huge thank you David, this is exactly what I wanted 💃 🏆 🙏 and it work perfectly!!!

I take advantage of the fact that you are a king for one last question:

How do I get all the items in an FME list into my dropdown (I know I should go through "feature.getAttribute('array{}.display_name')" but not sure how to operate it ;-)?


david_r
Celebrity
  • 8391 replies
  • Best Answer
  • May 16, 2023

huge thank you David, this is exactly what I wanted 💃 🏆 🙏  and it work perfectly!!!

Try replacing line 23 with this:

list_items = feature.getAttribute('array{}.display_name') or []
dlg = StreetNameDialog(list_items)

 

 


quelozantonin
Contributor
  • Author
  • Contributor
  • 8 replies
  • May 16, 2023

A huge thank you David, this is exactly what I wanted 💃 🏆 🙏 and it work perfectly!!!

Quick and Efficient, I love that 😍 👏

Great Thanks.