QPushButton doesn't do anything after clicking - python

I am a beginner with Qt and PySide with Python and I am facing a problem I don't know how to solve. I am working on FreeCAD and I try to design a simple window with push buttons but I can't make them work. When I click on them, their text should change but nothing happen.
Here is a piece of my code:
class ManageDialog:
def __init__(self, path):
self.form = FreeCADGui.PySideUic.loadUi(path)
self.form.setWindowTitle("Linked files manager")
QListWidgetItem("First item", self.form.listWidget)
QListWidgetItem("Second item", self.form.listWidget)
self.form.RemoveButton.clicked.connect(self.remove)
self.form.show()
def remove(self):
self.form.RemoveButton.setText("File removed")
path_to_ui = FreeCAD.getHomePath() + "/Mod/ThesisExistingBridges/ManageDialog.ui"
w = ManageDialog(path_to_ui)
My window is created according to a UI file done with Qt Creator. The design of the window is correct also and I can add items to a list widget for example but the buttons are not activated when clicked.
Here is a sample of UI file:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>494</width>
<height>459</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QListWidget" name="listWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>471</width>
<height>401</height>
</rect>
</property>
</widget>
<widget class="QWidget" name="horizontalLayoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>420</y>
<width>195</width>
<height>31</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="RemoveButton">
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="ReloadFromButton">
<property name="text">
<string>Reload from</string>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QPushButton" name="OKButton">
<property name="geometry">
<rect>
<x>390</x>
<y>420</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>OK</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>

I make it work in changing the method remove() in #static mode.
class ManageDialog:
def __init__(self, path):
self.form = FreeCADGui.PySideUic.loadUi(path)
self.form.setWindowTitle("Linked files manager")
QListWidgetItem("First item", self.form.listWidget)
QListWidgetItem("Second item", self.form.listWidget)
def clickedremove():
self.remove(self.form)
self.form.RemoveButton.clicked.connect(clickedremove)
self.form.show()
#staticmethod
def remove(f):
f.RemoveButton.setText("File removed")
path_to_ui = FreeCAD.getHomePath() + "/Mod/ThesisExistingBridges/ManageDialog.ui"
w = ManageDialog(path_to_ui)
It is maybe not the smartest way but it's working.

Related

how to copy frame from tab to new tab in tabwidget pyqt5?

How to copy tab1 frame to new tab which is dynamically added using QPushButton and addTab function.
Trying to create gui with "Insert Tab" button that will add new tab with predefined frame and settings,
but its replacing frame from tab1 to new tab if I go back to tab one its missing the frame which i used in new tab.
How can I copy the widgets instead of moving it while adding new tab?
ui file:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1120</width>
<height>850</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QTabWidget" name="tabWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>130</y>
<width>1121</width>
<height>721</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(253, 255, 242);</string>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Tab 1</string>
</attribute>
<widget class="QWidget" name="widget" native="true">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1111</width>
<height>691</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 243, 242);</string>
</property>
<widget class="QFrame" name="frame_frame">
<property name="geometry">
<rect>
<x>700</x>
<y>210</y>
<width>171</width>
<height>80</height>
</rect>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>30</x>
<y>30</y>
<width>47</width>
<height>13</height>
</rect>
</property>
<property name="text">
<string>Hello saqib</string>
</property>
</widget>
</widget>
</widget>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Tab 2</string>
</attribute>
</widget>
</widget>
<widget class="QPushButton" name="change">
<property name="geometry">
<rect>
<x>60</x>
<y>50</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>Change Text</string>
</property>
</widget>
<widget class="QPushButton" name="insert">
<property name="geometry">
<rect>
<x>220</x>
<y>50</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>Insert Tab</string>
</property>
</widget>
<widget class="QPushButton" name="remove">
<property name="geometry">
<rect>
<x>390</x>
<y>50</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>Remove Tab</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
python main:
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi
from PySide2.QtCore import *
class MainWindow(QDialog):
def __init__(self):
super(MainWindow, self).__init__()
loadUi("tabtutorial.ui", self)
self.tabWidget.currentChanged.connect(self.tabChanged)
self.change.clicked.connect(self.changeTabText)
self.insert.clicked.connect(self.insertTab)
self.remove.clicked.connect(self.removeTab)
def tabChanged(self):
print("Tab was changed to ", self.tabWidget.currentIndex())
def changeTabText(self):
self.tabWidget.setTabText(0, "First tab")
self.tabWidget.setTabText(1, "Second tab")
def insertTab(self):
self.label.setText(QCoreApplication.translate("Dialog", "Hello test", None))
self.tabWidget.addTab(self.widget, "New Tab")
def removeTab(self):
self.tabWidget.removeTab(0)
# main
app = QApplication(sys.argv)
mainwindow = MainWindow()
widget = QtWidgets.QStackedWidget()
widget.addWidget(mainwindow)
widget.setFixedHeight(850)
widget.setFixedWidth(1120)
widget.show()
try:
sys.exit(app.exec_())
except:
print("Exiting")
Output:
this Tab1 have a frame with "Hello Saq" label under frame widget.
If I insert new tab (using the above code addTab(self.widget, "New Tab") function its adding the frame to new tab but now its missing in the Tab1
Is there any way to copy widget instead of moving it?

How to access a button from Tab Widget in PyQt5

I have made a Tab Widget Window in my Main Window using Qt-Designer and I am trying to access a button in my 1st Tab.
This is my code.
import sys
from PyQt5 import QtCore, QtGui,uic,QtWidgets
class AddWindow(QtWidgets.QDialog):
def __init__(self):
super(AddWindow,self).__init__()
uic.loadUi('AddWindow.ui',self)
class MyApp(QtWidgets.QMainWindow):
def __init__(self):
super(MyApp,self).__init__()
uic.loadUi('MainWindow.ui',self)
self.AddButton.clicked.connect(self.Addwindow)
def printW(self):
print("hello")
def Addwindow(self):
addwindow=AddWindow()
addwindow.exec_()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
I just want to have my button on 1st tab (named print) connected with function printW So I can display something on screen.
here is my MainWindow.ui file.
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>756</width>
<height>549</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="AddButton">
<property name="geometry">
<rect>
<x>630</x>
<y>440</y>
<width>121</width>
<height>61</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>18</pointsize>
<weight>50</weight>
<italic>false</italic>
<bold>false</bold>
<underline>true</underline>
<strikeout>false</strikeout>
</font>
</property>
<property name="text">
<string>Add</string>
</property>
</widget>
<widget class="QPushButton" name="Next">
<property name="geometry">
<rect>
<x>170</x>
<y>420</y>
<width>121</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>Next</string>
</property>
</widget>
<widget class="QTabWidget" name="tabWidget">
<property name="geometry">
<rect>
<x>20</x>
<y>50</y>
<width>711</width>
<height>311</height>
</rect>
</property>
<property name="currentIndex">
<number>2</number>
</property>
<widget class="QWidget" name="tab1">
<attribute name="title">
<string>Tab 1</string>
</attribute>
<widget class="QPushButton" name="print">
<property name="geometry">
<rect>
<x>190</x>
<y>100</y>
<width>101</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>print</string>
</property>
</widget>
</widget>
<widget class="QWidget" name="tab2">
<attribute name="title">
<string>tab 2</string>
</attribute>
</widget>
<widget class="QWidget" name="tab3">
<attribute name="title">
<string>Page</string>
</attribute>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>756</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
I have tried creating a class and accessing button but could not do it.

Pyside2 QAction triggers once automatically but not when user clicks the menu

I've created a simple GUI using qt designer and imported it into my python project. The main window comes up, and the menus/buttons are responsive, but I cannot manage to connect my QActions to custom functions (I did it for buttons though, and it works). The weird thing is that my custom function (on_action_clicked) is called once when I run the application, but not when I click on the menu items or icons of the toolbar (I tried connecting both). Below are the test codes. Am I missing something?
imbrowser3d.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>851</width>
<height>649</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QGraphicsView" name="gv_image">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>731</width>
<height>501</height>
</rect>
</property>
</widget>
<widget class="QScrollBar" name="sb_index">
<property name="geometry">
<rect>
<x>0</x>
<y>510</y>
<width>701</width>
<height>20</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QScrollBar" name="sb_zeta">
<property name="geometry">
<rect>
<x>0</x>
<y>530</y>
<width>701</width>
<height>20</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QLabel" name="label_index">
<property name="geometry">
<rect>
<x>700</x>
<y>510</y>
<width>55</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>index</string>
</property>
</widget>
<widget class="QLabel" name="label_zeta">
<property name="geometry">
<rect>
<x>700</x>
<y>530</y>
<width>55</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>zeta</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>740</x>
<y>500</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>851</width>
<height>26</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
<addaction name="menu_open"/>
<addaction name="menu_save"/>
<addaction name="menu_load"/>
</widget>
<addaction name="menuFile"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<widget class="QToolBar" name="action_toolbar">
<property name="windowTitle">
<string>toolBar</string>
</property>
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="action_save_state"/>
</widget>
<action name="menu_open">
<property name="text">
<string>Open</string>
</property>
</action>
<action name="menu_save">
<property name="icon">
<iconset>
<normaloff>ui_icons/save_icon.png</normaloff>ui_icons/save_icon.png</iconset>
</property>
<property name="text">
<string>Save state</string>
</property>
</action>
<action name="menu_load">
<property name="text">
<string>Load state</string>
</property>
</action>
<action name="action_save_state">
<property name="icon">
<iconset>
<normaloff>ui_icons/save_icon.png</normaloff>ui_icons/save_icon.png</iconset>
</property>
<property name="text">
<string>Save state</string>
</property>
<property name="toolTip">
<string>Save state</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>
main.py
import sys
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication, QAction, QPushButton, QFileDialog
from PySide2.QtCore import QFile, QObject
class UiMainWindow(QObject):
def __init__(self, ui_file, parent=None):
super(UiMainWindow, self).__init__(parent)
ui_file = QFile(ui_file)
ui_file.open(QFile.ReadOnly)
loader = QUiLoader()
self.window = loader.load(ui_file)
ui_file.close()
self.btn = self.window.findChild(QPushButton, 'pushButton')
self.btn.clicked.connect(self.on_btn_clicked)
self.action_save = self.window.findChild(QAction, 'action_save_state')
self.action_save.triggered.connect(self.on_action_clicked('action clicked'))
self.window.show()
def on_action_clicked(self, txt):
print(txt)
def on_btn_clicked():
print('button clicked')
if __name__ == '__main__':
app = QApplication(sys.argv)
form = UiMainWindow('imbrowser3d.ui')
sys.exit(app.exec_())
You have 2 errors:
on_btn_clicked is a method of the class so it must have as the first parameter the instance, that is, self.
the connection is with the name of the function without evaluating, in your case the slot on_action_clicked you are evaluating it, so you are getting it printed at the beginning, for it there are 2 possible solutions: use partial or use a lambda function.
self.btn = self.window.findChild(QPushButton, 'pushButton')
self.btn.clicked.connect(self.on_btn_clicked)
self.action_save = self.window.findChild(QAction, 'action_save_state')
self.action_save.triggered.connect(partial(self.on_action_clicked, 'action clicked'))
# self.action_save.triggered.connect(lambda: self.on_action_clicked('action clicked')) # use lambda function
self.window.show()
def on_action_clicked(self, txt):
print(txt)
def on_btn_clicked(self):
print('button clicked')

dynamically populate comboBox Qt

I am trying to write a small app that compares xml. But right I am having a hard time doing the UI. I have a button that triggers a QFileDialog. I take the string input and populate a combobox. Unfortunately, the combobox stays empty. It seems to work when I hardcode it. But I am unable to get the app to do it dynamically. Is there something I am missing?
Here's the code:
import sys
from qtpy import QtCore, QtWidgets, uic
from qtpy.QtWidgets import QMainWindow, QApplication, QFileDialog
from qtpy.QtCore import QObject
class CompareSiteAndRepoWindow(QMainWindow):
def __init__(self):
super(CompareSiteAndRepoWindow,self).__init__()
uic.loadUi('CompareSiteAndRepo.ui',self)
self.BrowseLRPath.clicked.connect(self.browseFile)
self.buttonBox.rejected.connect(self.reject)
self.show()
def reject(self):
self.close()
def browseFile(self):
fileDiag = QFileDialog.getOpenFileName(self, 'Open file',
'c:\\',"xml/html (*.xml *.html)")
if(not fileDiag[0]):
print(fileDiag[0])
self.LRPathComboBox.addItem(fileDiag[0],0)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = CompareSiteAndRepoWindow()
sys.exit(app.exec())
the CompareSiteAndRepo.ui file
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CompareLabToSiteDLG</class>
<widget class="QMainWindow" name="CompareLabToSiteDLG">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>316</width>
<height>262</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="LRLabel">
<property name="text">
<string>Load Report</string>
</property>
</widget>
</item>
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QComboBox" name="LRPathComboBox"/>
<widget class="QPushButton" name="BrowseLRPath">
<property name="text">
<string>Browse</string>
</property>
</widget>
</widget>
</item>
<item>
<widget class="QLabel" name="LP2Label">
<property name="text">
<string>LaunchPadData repo layout</string>
</property>
</widget>
</item>
<item>
<widget class="QSplitter" name="splitter_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QComboBox" name="LP2RepoPath"/>
<widget class="QPushButton" name="BrowseLP2RepoPath">
<property name="text">
<string>Browse</string>
</property>
</widget>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>316</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
the problem is the statement if (not fileDiag[0]):, fileDialog[0] is a text that if we evaluate it as boolean will return True for any text except it is empty and if you deny it will be False if the text is not empty or True if it is, which is contrary to what you want:
fileDiag[0] not fileDiag[0]
+--------------+--------------+
"" True
"some text" False
One solution is to suppress the not:
if fileDiag[0]:
[...]
But another solution is to compare if the text is not empty as I show below:
def browseFile(self):
filename, _ = QFileDialog.getOpenFileName(self, 'Open file', 'c:\\',"xml/html (*.xml *.html)")
if filename != "":
self.LRPathComboBox.addItem(filename, 0)

Maya PySide2 UI Get QLineEdit value

I am starting using QtCreator to create a UI for a little tool for Maya 2017. QtCreator gave me this .ui file :
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DockWidget</class>
<widget class="QDockWidget" name="DockWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>550</width>
<height>251</height>
</rect>
</property>
<property name="floating">
<bool>true</bool>
</property>
<property name="windowTitle">
<string>Attr Editor</string>
</property>
<widget class="QWidget" name="dockWidgetContents">
<widget class="QLineEdit" name="attr_value_textfield">
<property name="geometry">
<rect>
<x>130</x>
<y>128</y>
<width>391</width>
<height>20</height>
</rect>
</property>
<property name="toolTip">
<string>attr_value</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>227</x>
<y>20</y>
<width>97</width>
<height>25</height>
</rect>
</property>
<property name="font">
<font>
<family>Century Gothic</family>
<pointsize>16</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Attr Editor</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>240</x>
<y>180</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Submit</string>
</property>
<property name="+command" stdset="0">
<string>submitCommand</string>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>20</x>
<y>130</y>
<width>80</width>
<height>16</height>
</rect>
</property>
<property name="font">
<font>
<family>Century Gothic</family>
</font>
</property>
<property name="text">
<string>Attribute Value</string>
</property>
</widget>
<widget class="QLineEdit" name="attr_name_textfield">
<property name="geometry">
<rect>
<x>130</x>
<y>78</y>
<width>391</width>
<height>20</height>
</rect>
</property>
<property name="toolTip">
<string>attr_name</string>
</property>
<property name="whatsThis">
<string/>
</property>
</widget>
<widget class="QPushButton" name="pushButton_3">
<property name="geometry">
<rect>
<x>440</x>
<y>180</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Cancel</string>
</property>
<property name="+command" stdset="0">
<string>cancelCommand</string>
</property>
</widget>
<widget class="QPushButton" name="display_button">
<property name="geometry">
<rect>
<x>30</x>
<y>180</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Display</string>
</property>
<property name="+command" stdset="0">
<string>displayCommand</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>20</x>
<y>80</y>
<width>82</width>
<height>16</height>
</rect>
</property>
<property name="font">
<font>
<family>Century Gothic</family>
</font>
</property>
<property name="text">
<string>Attribute Name</string>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>
And I have this code wich displays my UI :
import maya.cmds as cmds
from PySide2.QtWidgets import *
from PySide2.QtCore import *
if cmds.window(main_window, ex = True):
cmds.deleteUI(main_window)
main_window = cmds.loadUI(uiFile = "C:/Users/thornydre/Desktop/attreditorui.ui")
cmds.showWindow(main_window)
def displayCommand(e):
print(attr_name_textfield.text())
print(attr_value_textfield.text())
def submitCommand(e):
attr_name = attr_name_textfield.text()
attr_value = attr_value_textfield.text()
is_string = False
try:
new_attr_value = float(attr_value)
if float(attr_value) % 1 == 0:
new_attr_value = int(attr_value)
except:
is_string = True
new_attr_value = attr_value
print(new_attr_value)
for name in cmds.ls(sl = True):
if is_string:
cmds.setAttr(name + "." + attr_name, new_attr_value, type = "string")
else:
cmds.setAttr(name + "." + attr_name, new_attr_value)
def cancelCommand(e):
cmds.deleteUI(main_window, window = True)
And if I click on my display_button, I have an error :
# Result: dockWidgetContents|display_button #
# Error: AttributeError: file <maya console> line 12: 'bool' object has no attribute 'attr_name_textfield' #
I tried to do it with a subclass of QtWidgets.QWidget as I found somewhere on the internet, but I don't really found any tutorial on how to build it properly:
from PySide2 import QtCore, QtGui, QtWidgets, QtUiTools
class Interface(QtWidgets.QWidget):
def __init__(self, parent = None):
super(Interface, self).__init__(parent)
ui_filename = "C:/Users/thornydre/Desktop/attreditorui.ui"
ui_file = QtCore.QFile(ui_filename)
ui_file.open(QtCore.QFile.ReadOnly)
self.ui = QtUiTools.QUiLoader().load(ui_file, parentWidget=self)
ui_file.close()
def connectInterface(self):
QtCore.QtObject.connect(self.displayCommand, QtCore.SIGNAL("clicked()"), self.displayCommandWin)
def displayCommand(self):
print(self.attr_name_textfield.text())
print(self.attr_value_textfield.text())
def main():
global ui
ui = Interface()
if __name__ == "__main__":
main()
Same thing here, the UI shows up, but really nothing happens with clicking on the display_button
Your second try with the class seems good but it's completely normal that you can't execute displayCommand, your button is not connected to your method.
Juste delete your connectInterface method, and right after ui_file.close() copy this:
self.ui.display_button.clicked.connect(self.displayCommand)
It should work better :) (and you have to use the eyllanesc solution too to access to the widgets values)

Categories

Resources