How to control Python with Yad?

a very small Live CD shaped to look and act like Puppy Linux.


Moderator: fredx181

Post Reply
User avatar
Kennel Dweller
Posts: 42
Joined: Sat Sep 12, 2020 10:29 am
Location: England
Has thanked: 7 times
Been thanked: 1 time

How to control Python with Yad?

Post by Kennel Dweller »

Hi. I know this is the wrong place to ask for this sort of help and I hope to be forgiven for that.
I don't belong to many groups and rarely need help but with all the knowledge and talent here I hope to solve my nightmare with this issue.
Basically I have found a python script which I like to use and it works well, rather than messing with it I would like to link to the necessary and not have to alter the code in future.
With that in mind I thought I could use yad and create a box with a launcher, not being well versed in either yad or python I did much reading and got myself well and truly confused.
I cannot for the life of me get the info from yad to go were I want it in the python script having tried all sorts of links, commands, options etc, stdin/out, input, arg, you name it?
I would have thought it should be easy but a big fat NO is all I get.
Can somebody tell me what to do or put me on the right track~? Better still do it for me lol.
I am using python 3.9, the yad works in it's own right and links to the python file ok, the python script works fine when type the filename in the red box, I just need the green box to be the red box if that makes sense.

Screenshot_2021-03-26_13-44-12.png
Screenshot_2021-03-26_13-44-12.png (204.81 KiB) Viewed 866 times
User avatar
rockedge
Site Admin
Posts: 6817
Joined: Mon Dec 02, 2019 1:38 am
Location: Connecticut,U.S.A.
Has thanked: 2968 times
Been thanked: 2790 times
Contact:

Re: Yad to Python HELP !!

Post by rockedge »

Hello!

So you need the variable in the shell (yad) script to equal "sample.dxf"?

could you rename both programs by adding a".gz" to the end and post them here? So I can run them to see what would be best in this case.

User avatar
Kennel Dweller
Posts: 42
Joined: Sat Sep 12, 2020 10:29 am
Location: England
Has thanked: 7 times
Been thanked: 1 time

Re: Yad to Python HELP !!

Post by Kennel Dweller »

Any help would be much appreciated, many thanks.

dxf2img.py.tar.gz
(938 Bytes) Downloaded 47 times
User avatar
Kennel Dweller
Posts: 42
Joined: Sat Sep 12, 2020 10:29 am
Location: England
Has thanked: 7 times
Been thanked: 1 time

Re: Yad to Python HELP !!

Post by Kennel Dweller »

I obviously didn't read your instruction properly.
Thrilled to get a response lol.
Is this better?

dxf2img.py.gz[/attachment ][attachment=1]dxf2png.sh.gz
Attachments
dxf2img.py.gz
(1.36 KiB) Downloaded 48 times
dxf2png.sh.gz
(235 Bytes) Downloaded 58 times
User avatar
Kennel Dweller
Posts: 42
Joined: Sat Sep 12, 2020 10:29 am
Location: England
Has thanked: 7 times
Been thanked: 1 time

Re: How to control Python with Yad?

Post by Kennel Dweller »

I have a feeling I might have posted the version I had been messing with.
Here is the original.

dxf2img.py.gz
(1.53 KiB) Downloaded 77 times
User avatar
rockedge
Site Admin
Posts: 6817
Joined: Mon Dec 02, 2019 1:38 am
Location: Connecticut,U.S.A.
Has thanked: 2968 times
Been thanked: 2790 times
Contact:

Re: How to control Python with Yad?

Post by rockedge »

I don't have the needed python modules loaded at the moment to actually see but this seems to work

Code: Select all

#!/bin/bash

cd /root/Desktop/funpics/in/
dialog=$(yad --title "DXF2PNG" --image="/root/apps/logo2.jpg" --form --field="imgname")


name=$(echo $dialog | awk 'BEGIN {FS="|" } { print $1 }')

python3 /root/app/dxf2img.py $(echo $name)
User avatar
Kennel Dweller
Posts: 42
Joined: Sat Sep 12, 2020 10:29 am
Location: England
Has thanked: 7 times
Been thanked: 1 time

Re: How to control Python with Yad?

Post by Kennel Dweller »

Thanks for such a swift response but that returns error.
python3.9/dist-packages/ezdxf/lldxf/validator.py", line 229, in is_binary_dxf_file
with open(filename, 'rb') as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'name'

But I have seen that before, I think the error pertains to the type of file entered although the script should read both ascii and binary.
I'll try with another dxf.

User avatar
Kennel Dweller
Posts: 42
Joined: Sat Sep 12, 2020 10:29 am
Location: England
Has thanked: 7 times
Been thanked: 1 time

Re: How to control Python with Yad?

Post by Kennel Dweller »

That returned same error, I am back to trying to replace the text in red square with a variable from yad, is see that as what I need to do but don't know how to do it.

User avatar
rockedge
Site Admin
Posts: 6817
Joined: Mon Dec 02, 2019 1:38 am
Location: Connecticut,U.S.A.
Has thanked: 2968 times
Been thanked: 2790 times
Contact:

Re: How to control Python with Yad?

Post by rockedge »

my mistake! I have to install a couple of python modules to check it

Code: Select all

#!/bin/bash

cd /root/Desktop/funpics/in/
dialog=$(yad --title "DXF2PNG" --image="/root/apps/logo2.jpg" --form --field="imgname")


name=$(echo $dialog | awk 'BEGIN {FS="|" } { print $1 }')

python3 /root/app/dxf2img.py $name

this is what I am seeing:

Code: Select all

root-# ./dxf2png.sh
lopqj6x7.dxf
Traceback (most recent call last):
  File "/root/Build/Test/dxf2img.py", line 40, in <module>
    first.convert_dxf2img(["9.dxf"],img_format='.png')
  File "/root/Build/Test/dxf2img.py", line 16, in convert_dxf2img
    doc = ezdxf.readfile(name)
  File "/opt/ActivePython-3.7/lib/python3.7/site-packages/ezdxf/filemanagement.py", line 138, in readfile
    if is_binary_dxf_file(filename):
  File "/opt/ActivePython-3.7/lib/python3.7/site-packages/ezdxf/lldxf/validator.py", line 229, in is_binary_dxf_file
    with open(filename, 'rb') as fp:
FileNotFoundError: [Errno 2] No such file or directory: '9.dxf'
root-# ./dxf2png.sh
/root/Build/Test/lopqj6x7.dxf
Traceback (most recent call last):
  File "/root/Build/Test/dxf2img.py", line 40, in <module>
    first.convert_dxf2img(["9.dxf"],img_format='.png')
  File "/root/Build/Test/dxf2img.py", line 16, in convert_dxf2img
    doc = ezdxf.readfile(name)
  File "/opt/ActivePython-3.7/lib/python3.7/site-packages/ezdxf/filemanagement.py", line 138, in readfile
    if is_binary_dxf_file(filename):
  File "/opt/ActivePython-3.7/lib/python3.7/site-packages/ezdxf/lldxf/validator.py", line 229, in is_binary_dxf_file
    with open(filename, 'rb') as fp:
FileNotFoundError: [Errno 2] No such file or directory: '9.dxf'
User avatar
rockedge
Site Admin
Posts: 6817
Joined: Mon Dec 02, 2019 1:38 am
Location: Connecticut,U.S.A.
Has thanked: 2968 times
Been thanked: 2790 times
Contact:

Re: How to control Python with Yad?

Post by rockedge »

This looks good. 2 things, one line added and one line modified in the python program

Code: Select all

import sys

Code: Select all

if __name__ == '__main__':
    first =  DXF2IMG()
    first.convert_dxf2img([(sys.argv[2])],img_format='.png')

And the Bash script like this:

Code: Select all

#!/bin/bash

cd /root/Desktop/funpics/in/
dialog=$(yad --title "DXF2PNG" --image="/root/apps/logo2.jpg" --form --field="imgname")


name=$(echo $dialog | awk 'BEGIN {FS="|" } { print $1 }')

python3 /root/app/dxf2img.py $name
dxf2img.py.new.gz
(1.54 KiB) Downloaded 33 times
dxf2png.sh.new.gz
(298 Bytes) Downloaded 54 times
User avatar
fredx181
Posts: 3250
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 408 times
Been thanked: 1412 times
Contact:

Re: How to control Python with Yad?

Post by fredx181 »

Yes, the python script needs modification so it accept arguments, here it is:

dxf2img.py.gz
remove fake .gz and make executable
(1.54 KiB) Downloaded 53 times

The yad script as rockedge provided works with above python script (when giving the name of one .dfx file in the same directory as running the script from)
But you you may want to add to the yad script a file selector so you can browse for (multiple) .dfx files to convert, like this:

Code: Select all

#!/bin/bash

cd /root/Desktop/funpics/in/
dialog=$(yad --title "DXF2PNG" --image="/root/apps/logo2.jpg" --form --field="imgname:mfl")


name=$(echo $dialog | awk 'BEGIN {FS="|" } { print $1 }' | sed 's/!/ /')


python3 /root/apps/dxf2img.py $name

Works only with filenames not containing spaces.

EDIT: Haha, I see now that rockedge beat me with similar python script :D

Fred

User avatar
Kennel Dweller
Posts: 42
Joined: Sat Sep 12, 2020 10:29 am
Location: England
Has thanked: 7 times
Been thanked: 1 time

Re: How to control Python with Yad?

Post by Kennel Dweller »

You've done it!!! absolutely amazing, thank you so much.
4 days and sleepless nights of head scratching, reading, thinking, trying and you do it in a few minutes.
Maybe I shouldn't be so stubborn and ask for help sooner.
It's now working perfectly many thanks indeed.

User avatar
Kennel Dweller
Posts: 42
Joined: Sat Sep 12, 2020 10:29 am
Location: England
Has thanked: 7 times
Been thanked: 1 time

Re: How to control Python with Yad?

Post by Kennel Dweller »

Just looked back at previous attempts and I was quite close to solving it but went off on a tangent and missed the point again.
At one time I had imported sys and followed a tutorial to no avail but looking at the changes Rockedge has made I see I wasn't far away.
I am so chuffed to get this working I can't thank you enough, It was nice to get some real genuine help instead of the sarky comments I was expecting.
It saddens me when I see so often unhelpful comments.
"Can anyone help with this issue I only get syntax error?
That's you ain't using correct syntax"
Why bother to make the comment? Is it me?

User avatar
rockedge
Site Admin
Posts: 6817
Joined: Mon Dec 02, 2019 1:38 am
Location: Connecticut,U.S.A.
Has thanked: 2968 times
Been thanked: 2790 times
Contact:

Re: How to control Python with Yad?

Post by rockedge »

@Kennel Dweller
I find outside of this forum it's usual to be answered by comments that would NEVER be said face to face, but behind the keyboard real "warriors" abound with talent to point out how stupid my question is/was.

What if one day I had a question to a problem I need to be solved? And you just happen to be the guy with that answer? And I sometime before belittled you....Karma can be a bitch.

My brother is a rocket scientist...a real one.....sometimes he asks the most basic question about some computer procedure that any 8 year old kid knows inside and out. I help the best I can to answer that....he is a rocket scientist for Pete's sake (his name is Peter) and I know there will be a time when I'll need a hand and he is full of engineer/scientist ideas and thoughts, and if I acted like a know-it-all-better jerk he would answer my question with that in mind.....if at all.

We here in "the kennels" try our best to be kind and insightful. Who knows? You might be the next guy answering the call for some help when someone is SO close to getting the code to work but just is making some simple syntax error because they are learning and had the guts to try to make a tool on their own.

Post Reply

Return to “DebianDogs”