I want to create PYQGIS plugin and for my task i need my QGIS GUI form to have one more button except to original button ok.
the additional button to do something and original button to do something else for my script.
first i add new button in initGui
:
def initGui(self):
icon_path = ':/plugins/twoobutton/icon.png'
self.add_action(
icon_path,
text=self.tr(u'twoobutton'),
callback=self.run,
parent=self.iface.mainWindow())
self.dlg.pushButton.clicked.connect(self.run1)
and after I create new def run1
:
def run1(self):
self.dlg.show()
result = self.dlg.exec_()
if result:
myvar = str(self.dlg.lineEdit_5.text())
if myvar==1000:
text1='hello'
text2='world'
self.dockWidget.dockWidgetContents.lineEdit.setText(str(text1))
self.dockWidget.dockWidgetContents.lineEdit_2.setText(str(text2))
pass
update :
class fff:
"""QGIS Plugin Implementation."""
def __init__(self, iface):
self.dlg = fffDialog()
self.dlg.pushButton.clicked.connect(self.run1)
"""Constructor.
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgsInterface
"""
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
# initialize locale
locale = QSettings().value('locale/userLocale')[0:2]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'fff_{}.qm'.format(locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
# Declare instance attributes
self.actions = []
self.menu = self.tr(u'&ffffff')
# TODO: We are going to let the user set this up in a future iteration
self.toolbar = self.iface.addToolBar(u'fff')
self.toolbar.setObjectName(u'fff')
But if I click on pushbutton then don't show me anything. How to fix that?
1 Answer 1
Just put self.dlg.pushButton.clicked.connect(self.run1)
in __init__
not in initGui()
.
def __init__(self, iface):
# some code
self.dlg.pushButton.clicked.connect(self.run1)
-
if i put button in the
__init__
then show me no atrribute error ,why ?Chris Papas– Chris Papas2017年03月13日 15:20:22 +00:00Commented Mar 13, 2017 at 15:20 -
1AttributeError: nameplugin instance has no attribute 'dlg'Chris Papas– Chris Papas2017年03月13日 15:44:03 +00:00Commented Mar 13, 2017 at 15:44
-
Now you know that connectiion is ok. Error is occuring when you try to execute this line:
self.dlg.show()
are you sure you want to do this there? What is that? You don't have any self.dlg object.dmh126– dmh1262017年03月13日 15:47:46 +00:00Commented Mar 13, 2017 at 15:47 -
that error show me every time to start QGIS i have two buttons and 5 line edits what you mean self.dlg object ?Chris Papas– Chris Papas2017年03月13日 15:58:59 +00:00Commented Mar 13, 2017 at 15:58
-
1not i put now you are correct now dont show me error but my code in run1 dont excetute if i push button :( i paste you my initChris Papas– Chris Papas2017年03月13日 16:08:27 +00:00Commented Mar 13, 2017 at 16:08