(solved) Help with Macro with GUI

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
user1234
Veteran
Posts: 3512
Joined: Mon Jul 11, 2016 5:08 pm

(solved) Help with Macro with GUI

Post by user1234 »

Hello!

I have a big understandig problem. This macro:

Code: Select all

import FreeCAD
from PySide import QtGui, QtCore
import TechDraw
 
class Thread(QtGui.QWidget):

    def __init__(self):
        super(Thread, self).__init__()
        self.initUI()

    def initUI(self):

        row_broad = 175

        self.label_Parameter = QtGui.QLabel("Parameters", self)
        self.label_Parameter.setFixedWidth(row_broad)

        self.label_thread_diameter = QtGui.QLabel("Thread Diameter", self)
        self.line_edit_thread_diamter = QtGui.QLineEdit(self)
        self.line_edit_thread_diamter.setFixedWidth(row_broad)
        self.label_mm_1 = QtGui.QLabel("mm", self)

        self.label_thread_lenght = QtGui.QLabel("Thread Lenght", self)
        self.line_edit_thread_lenght = QtGui.QLineEdit(self)
        self.line_edit_thread_lenght.setFixedWidth(row_broad)
        self.label_mm_2 = QtGui.QLabel("mm", self)

        self.label_hard_line_thickness = QtGui.QLabel("Hard Line Thickness", self)
        self.line_edit_hard_line_thickness = QtGui.QLineEdit(self)
        self.line_edit_hard_line_thickness.setFixedWidth(row_broad)
        self.line_edit_hard_line_thickness.setText("0.7")
        self.label_mm_3 = QtGui.QLabel("mm", self)

        self.label_smooth_line_thickness = QtGui.QLabel("Smooth Line Thickness", self)
        self.line_edit_smooth_line_thickness = QtGui.QLineEdit(self)
        self.line_edit_smooth_line_thickness.setFixedWidth(row_broad)
        self.line_edit_smooth_line_thickness.setText("0.35")
        self.label_mm_4 = QtGui.QLabel("mm", self)

        self.label_view = QtGui.QLabel("View", self)
        self.line_edit_View = QtGui.QLineEdit(self)
        self.line_edit_View.setFixedWidth(row_broad)

        self.label_points = QtGui.QLabel("Create Construction Points", self)
        self.checkbox_points = QtGui.QCheckBox(self)
        self.checkbox_points.setCheckState(QtCore.Qt.Checked)

        self.label_space_1 = QtGui.QLabel("", self)

        self.label_plane_view = QtGui.QLabel("Plane View", self)

        self.label_start_angle = QtGui.QLabel("Start Angle", self)
        self.line_edit_start_angle = QtGui.QLineEdit(self)
        self.line_edit_start_angle.setFixedWidth(row_broad)
        self.line_edit_start_angle.setText("75")
        self.label_degree_1 = QtGui.QLabel("°", self)

        self.label_end_angle = QtGui.QLabel("End Angle", self)
        self.line_edit_end_angle = QtGui.QLineEdit(self)
        self.line_edit_end_angle.setFixedWidth(row_broad)
        self.line_edit_end_angle.setText("345")
        self.label_degree_2 = QtGui.QLabel("°", self)

        self.label_center_arc = QtGui.QLabel("Center Arc", self)
        self.line_edit_center_arc = QtGui.QLineEdit(self)
        self.line_edit_center_arc.setFixedWidth(row_broad)

        self.button_inner_thread_plain_view = QtGui.QPushButton("Inner Thread Plain View", self)
        self.button_outer_thread_plain_view = QtGui.QPushButton("Outer Thread Plain View", self)

        self.label_space_2 = QtGui.QLabel("", self)

        self.label_side_section_view = QtGui.QLabel("Plane/Section View", self)

        self.label_start_point = QtGui.QLabel("Start Point", self)
        self.line_edit_start_point = QtGui.QLineEdit(self)
        self.line_edit_start_point.setFixedWidth(row_broad)

        self.label_direction_point = QtGui.QLabel("Direction Point", self)
        self.line_edit_direction_point = QtGui.QLineEdit(self)
        self.line_edit_direction_point.setFixedWidth(row_broad)

        self.button_inner_thread_side_view = QtGui.QPushButton("Inner Thread Side View", self)
        self.button_inner_thread_section_view = QtGui.QPushButton("Inner Thread Section View", self)

        self.button_outer_thread_side_view = QtGui.QPushButton("Outer Thread Side View", self)
        self.button_outer_thread_side_view.setFixedWidth(row_broad)
        self.button_outer_thread_section_view = QtGui.QPushButton("Outer Thread Section View", self)

        self.label_debug = QtGui.QLabel("x", self)

        layout = QtGui.QGridLayout()
        self.setWindowTitle("Threads")

        i = 0

        layout.addWidget(self.label_Parameter, i, 0)
        i = i + 1

        layout.addWidget(self.label_thread_diameter, i, 0)
        layout.addWidget(self.line_edit_thread_diamter, i, 1)
        layout.addWidget(self.label_mm_1, i, 2)
        i = i + 1

        layout.addWidget(self.label_thread_lenght, i, 0)
        layout.addWidget(self.line_edit_thread_lenght, i, 1)
        layout.addWidget(self.label_mm_2, i, 2)
        i = i + 1

        layout.addWidget(self.label_hard_line_thickness, i, 0)
        layout.addWidget(self.line_edit_hard_line_thickness, i, 1)
        layout.addWidget(self.label_mm_3, i, 2)
        i = i + 1

        layout.addWidget(self.label_smooth_line_thickness, i, 0)
        layout.addWidget(self.line_edit_smooth_line_thickness, i, 1)
        layout.addWidget(self.label_mm_4, i, 2)
        i = i + 1

        layout.addWidget(self.label_view, i, 0)
        layout.addWidget(self.line_edit_View, i, 1)
        i = i + 1

        layout.addWidget(self.label_points, i, 0)
        layout.addWidget(self.checkbox_points, i, 1)
        i = i + 1

        layout.addWidget(self.label_space_1, i, 0)
        i = i + 1

        layout.addWidget(self.label_plane_view, i, 0)
        i = i + 1

        layout.addWidget(self.label_start_angle, i, 0)
        layout.addWidget(self.line_edit_start_angle, i, 1)
        layout.addWidget(self.label_degree_1, i, 2)
        i = i + 1

        layout.addWidget(self.label_end_angle, i, 0)
        layout.addWidget(self.line_edit_end_angle, i, 1)
        layout.addWidget(self.label_degree_2, i, 2)
        i = i + 1

        layout.addWidget(self.label_center_arc, i, 0)
        layout.addWidget(self.line_edit_center_arc, i, 1)
        i = i + 1

        layout.addWidget(self.button_inner_thread_plain_view, i, 0)
        layout.addWidget(self.button_outer_thread_plain_view, i, 1)
        i = i + 1

        layout.addWidget(self.label_space_2, i, 0)
        i = i + 1

        layout.addWidget(self.label_side_section_view, i, 0)
        i = i + 1

        layout.addWidget(self.label_start_point, i, 0)
        layout.addWidget(self.line_edit_start_point, i, 1)
        i = i + 1

        layout.addWidget(self.label_direction_point, i, 0)
        layout.addWidget(self.line_edit_direction_point, i, 1)
        i = i + 1

        layout.addWidget(self.button_inner_thread_side_view, i, 0)
        layout.addWidget(self.button_inner_thread_section_view, i, 1)
        i = i + 1

        layout.addWidget(self.button_outer_thread_side_view, i, 0)
        layout.addWidget(self.button_outer_thread_section_view, i, 1)
        i = i + 1

        layout.addWidget(self.label_debug, i, 0)



        self.setLayout(layout)
        self.show()
        print("test")
        self.button_outer_thread_section_view.clicked.connect(self.xxxx)
        print("test")

#    def xxxx(self):
#        print("test")


Thread()
on Line 181 i call a function. On line 184 - 185 i define the function (yet comment out). When i run the macro, i get an error

Code: Select all

Traceback (most recent call last):
  File "/home/user/.FreeCAD/Macro/Thread.FCMacro", line 186, in <module>
    Thread()
  File "/home/user/.FreeCAD/Macro/Thread.FCMacro", line 9, in __init__
    self.initUI()
  File "/home/user/.FreeCAD/Macro/Thread.FCMacro", line 181, in initUI
    self.button_outer_thread_section_view.clicked.connect(self.xxxx)
<class 'AttributeError'>: 'Thread' object has no attribute 'xxxx'
, as expectet. But i get the GUI

But when i uncomment the line 184 - 185, the script does not work (or it does work but do not get a GUI, because i do not get an error message). Has someone an idea why. Work with this https://wiki.freecadweb.org/PySide_Inte ... e_Examples documentation.

Note: i am not a python guy, sorry when the question is dumb. Also note: in the docu there is used PySide with Python 2, i use Python 3 with Pyside 2, but is on a small GUI like that a difference? I have not find a PySide 2 docu.

FreeCAD Info:

Code: Select all

OS: Debian GNU/Linux bullseye/sid (X-Cinnamon/lightdm-xsession)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.23110 (Git)
Build type: ReleaseWithDebInfo
Branch: master
Hash: 711b4440f25e2fb24d20d72fe2b8af415d5959b1
Python version: 3.9.0+
Qt version: 5.15.1
Coin version: 4.0.0
OCC version: 7.5.0
Locale: English/United States (en_US)
Just for unterstanding what i want to achieve. Some macros for TechDraw for threads, like that.
3.png
3.png (210.18 KiB) Viewed 365 times
2.png
2.png (215.23 KiB) Viewed 365 times
1.png
1.png (152.43 KiB) Viewed 365 times
Except the circle, erverthing can be done with the GUI in 0.19 , but it is very slow to handle. So i want to make a small macro with GUI for that.

Sorry for bad english

Greetings
user1234
Last edited by user1234 on Fri Nov 27, 2020 3:23 pm, edited 2 times in total.
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Help with Marco with GUI

Post by openBrain »

user1234 wrote: Wed Nov 25, 2020 2:28 pm But when i uncomment the line 184 - 185, the script does not work (or it does work but do not get a GUI, because i do not get an error message). Has someone an idea why. Work with this https://wiki.freecadweb.org/PySide_Inte ... e_Examples documentation.
This is because your Thread() instance is garbage-collected.
Your last line should be:

Code: Select all

thread = Thread()
As you inherit QWidget, ideally you should even remove the 'self.show()' from 'initUI()' and end your file with :

Code: Select all

thread = Thread()
thread.show()
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Help with Marco with GUI

Post by Kunda1 »

please fix typo in thread title (should be 'macro')
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
user1234
Veteran
Posts: 3512
Joined: Mon Jul 11, 2016 5:08 pm

Re: Help with Macro with GUI

Post by user1234 »

Hello!
openBrain wrote: Wed Nov 25, 2020 3:18 pm This is because your Thread() instance is garbage-collected.
Your last line should be:
Thanks! And worked.!
Kunda1 wrote: Wed Nov 25, 2020 7:07 pm please fix typo
done

Greetings
user1234
Post Reply