[solved] Wiki dialog example broken?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
jdm
Posts: 24
Joined: Thu Jul 22, 2021 10:35 am

[solved] Wiki dialog example broken?

Post by jdm »

I'm beginning with scripting, but the example at https://wiki.freecadweb.org/Dialog_creation which I have copied below does not open any window or produce any visible effect. Note, script indentation is missing in this post but correct on my computer.

Can anyone tell if there is some issue with the example above? Or link another basic input form dialog example known to be working. Thanks.

OS: Fedora 34 (Workstation Edition) (GNOME/gnome)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.Unknown
Build type: Unknown
Python version: 3.9.4
Qt version: 5.15.2
Coin version: 4.0.0a
OCC version: 7.5.0
Locale: English/United States (en_US)

from PySide import QtCore, QtGui
import FreeCAD, Part

class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(187, 178)
self.title = QtGui.QLabel(Dialog)
self.title.setGeometry(QtCore.QRect(10, 10, 271, 16))
self.title.setObjectName("title")
self.label_width = QtGui.QLabel(Dialog)
self.label_width.setGeometry(QtCore.QRect(10, 50, 57, 16))
self.label_width.setObjectName("label_width")
self.label_height = QtGui.QLabel(Dialog)
self.label_height.setGeometry(QtCore.QRect(10, 90, 57, 16))
self.label_height.setObjectName("label_height")
self.width = QtGui.QLineEdit(Dialog)
self.width.setGeometry(QtCore.QRect(60, 40, 111, 26))
self.width.setObjectName("width")
self.height = QtGui.QLineEdit(Dialog)
self.height.setGeometry(QtCore.QRect(60, 80, 111, 26))
self.height.setObjectName("height")
self.create = QtGui.QPushButton(Dialog)
self.create.setGeometry(QtCore.QRect(50, 140, 83, 26))
self.create.setObjectName("create")

self.retranslateUi(Dialog)
QtCore.QObject.connect(self.create,QtCore.SIGNAL("pressed()"),self.createPlane)
QtCore.QMetaObject.connectSlotsByName(Dialog)

def retranslateUi(self, Dialog):
Dialog.setWindowTitle("Dialog")
self.title.setText("Plane-O-Matic")
self.label_width.setText("Width")
self.label_height.setText("Height")
self.create.setText("Create!")
print("tyty")
def createPlane(self):
try:
# first we check if valid numbers have been entered
w = float(self.width.text())
h = float(self.height.text())
except ValueError:
print("Error! Width and Height values must be valid numbers!")
else:
# create a face from 4 points
p1 = FreeCAD.Vector(0,0,0)
p2 = FreeCAD.Vector(w,0,0)
p3 = FreeCAD.Vector(w,h,0)
p4 = FreeCAD.Vector(0,h,0)
pointslist = [p1,p2,p3,p4,p1]
mywire = Part.makePolygon(pointslist)
myface = Part.Face(mywire)
Part.show(myface)

class plane():
def __init__(self):
self.d = QtGui.QWidget()
self.ui = Ui_Dialog()
self.ui.setupUi(self.d)
self.d.show()
Last edited by jdm on Tue Jul 27, 2021 10:28 am, edited 1 time in total.
User avatar
Chris_G
Veteran
Posts: 2579
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Wiki dialog example broken?

Post by Chris_G »

For code indentation, you can use the Code </> tag in your message.

The code you show defines 2 objects (Ui_Dialog and plane) but doesn't use it.

If you save your code into a file mywidget.py
Then you can use it with :

Code: Select all

import mywidget
myDialog = mywidget.plane()
Or, directly at the end of your code, you can create a plane object instance with :

Code: Select all

myDialog = plane()
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Wiki dialog example broken?

Post by onekk »

Here your code modified according to the last proposal of Chris_G

fdialog.py
(2.37 KiB) Downloaded 21 times
It works at least on 0.20 weekly build tested with a Linux AppImage.

use the </> code tag

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
jdm
Posts: 24
Joined: Thu Jul 22, 2021 10:35 am

Re: Wiki dialog example broken?

Post by jdm »

That worked of course, merci bien, grazie.

To be honest I'm supposedly able to program but have been too lazy too think before asking :oops:
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Wiki dialog example broken?

Post by onekk »

No problem, according you will learn how to post code properly :D.

Feel free to ask, the forum will help you, if you ask politely and you are willing to learn, better ask here than loose your time in watching youtube videos, some lines of code are more "concise" than an hour of video seeing the mouse pointer dance on the screen and trying to read the menu or to catch what is selected and what version of FreeCAD is used.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply