How do I open a file with IconFileSelect? (partially solved!!!)

More
02 Feb 2019 23:46 #125566 by pferrick
Hi all-

I'm trying to use the IconFileSelect widget to open a g-code file in AXIS. Here's what I have so far:

-a GladeVCP panel with a button that, when pressed, raises a tab containing an IconFileSelect widget called 'file'
- a python module with the following handler, referenced in the signals tab by the same name (I'm using the messagebox temporarily so at least I know that the two are connected!):

def on_file_selected(self,widget,data=None):
parent = None
md = gtk.MessageDialog(parent,
gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO,
gtk.BUTTONS_OK, "File selected")
md.run()
md.destroy()

But this messagebox does _not_ pop up when I select a file, or do any other action related to the icon file select widget. So apparently things are not yet connected the way they need to be.

I suspect that I'll need to pass [widget].get_selected() to AXIS somehow so it knows what file to open. [Do I replace [widget] by file, or by widget, or...?] And I'll bet that

import linuxcnc
c = linuxcnc.command()
c.program_open( ? )

will need to be in the python code as well. Or is there a simpler way I'm overlooking?

Thanks,
Patrick

Please Log in or Create an account to join the conversation.

More
03 Feb 2019 03:40 #125574 by pferrick
Following up on my own question...!

I just realized that NO handlers in my python code are responding to signals from any widgets located on my EMBED_TAB tabs.

The only way I can get a messagebox in a handler to pop up is if it's coming from a button on the main (right side of AXIS) GladeVCP panel. I'm sure this is a clue but unfortunately I'm not bright enough to decipher it...

Patrick

Please Log in or Create an account to join the conversation.

More
03 Feb 2019 07:46 #125575 by cmorley
Without the relevant file this is very difficult to get to the root of the problem.

The INI, GLADE and handler file would help immensely.

Chris M
The following user(s) said Thank You: pferrick

Please Log in or Create an account to join the conversation.

More
03 Feb 2019 18:31 - 03 Feb 2019 18:34 #125596 by pferrick
Great news! I figured out what a big part of the problem was: I was adding panels with a line like this:

EMBED_TAB_COMMAND = halcmd loadusr gladevcp -x {XID} pkf_probe2.glade

after having used a line like this:

GLADEVCP = -d -u pkf.py geometry_test.glade

...not realizing that I was failing to give the embedded tab objects a different name than gladevcp. No wonder it was confused!

Now I think I've boiled things down to a basic Python question: this code pops up a gtk.messagebox when I select a file via the IconFileSelect widget on a separate tab:
import gtk
import gladevcp
import linuxcnc
import pygtk
import os


class HandlerClass:


    def __init__(self, halcomp,builder,useropts):

       self.halcomp = halcomp
       self.builder = builder
 
    def on_file_selected(self,widget,data=None):
       parent = None
       md = gtk.MessageDialog(parent, 
       gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_QUESTION, 
       gtk.BUTTONS_YES_NO, "File selected.")
       md.run()
       md.destroy()

 
# boiler code
def get_handlers(halcomp,builder,useropts):
    return [HandlerClass(halcomp,builder,useropts)]

which I can still hardly believe I figured out how to do! The question is: how do I use what this object/widget returns (the path) to tell AXIS to open that file?

I've tried making the text of the message something like widget.get_selected() which shows nothing, and I have a feeling that I'm just not referring to _something_ in the right way. Once I can get at the path, I'm fairly confident that I can use something along the lines of:

import linuxcnc
c = linuxcnc.command()
c.program_open( ? )

to open the file. Anyway, I can happily bash away at the Python for quite a while if I can just get past this particular speed bump.

Thanks to everyone (especially cmorley) for all your help. When I have this whole thing completely figured out I will post a screenshot and details for anyone interested.

Patrick
Last edit: 03 Feb 2019 18:34 by pferrick.

Please Log in or Create an account to join the conversation.

More
03 Feb 2019 19:04 - 03 Feb 2019 19:05 #125598 by pferrick
If it helps any, here is what I get when I include a line in my handler file like the following to try and retrieve the path returned by IconFileSelect:

path = self.get_selected()

AttributeError: HandlerClass instance has no attribute 'get_selected'
Traceback (most recent call last):

I'm clearly not thinking about/using get_selected() the right way...

PKF
Last edit: 03 Feb 2019 19:05 by pferrick.

Please Log in or Create an account to join the conversation.

More
03 Feb 2019 23:16 - 03 Feb 2019 23:18 #125621 by cmorley
The function self.get_selected() calls 'get_selected() on self - which is the handler class -not right.

You want to call get_selected on the file selection dialog:
You need a reference to it, which you don't have.
You can do this two way - make a reference to it in the handler file with (you didn't supply the file so I can't use that actual names):
self.builder.get_object('NAME')
So it might look like this:
    def on_file_selected(self,widget,data=None):
       parent = None
       md = gtk.MessageDialog(parent, 
       gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_QUESTION, 
       gtk.BUTTONS_YES_NO, "File selected.")
       md.run()
       file =  self.builder.get_object('FILE_SELECTOR_NAME'.get_selected()
       if file:
           print file
       md.destroy()

Or you can add a 'selected' signal to the IconView in GLADE eg: on_IconFileSelection1_selected

then add this to your handler file:
    def on_IconFileSelection1_selected(self, widget, path=None):
        if path:
            print path

Now to get it to actually load the file then you need to add a hal_action_open. action to your GLADE file.
The add a reference to it (put it in the _init__ section:
self.open_action = self.builder.get_object('hal_action_open')

then to load the file the command would be:
self.open_action.load_file(path)

so to do it the first way:
class HandlerClass:


    def __init__(self, halcomp,builder,useropts):

       self.halcomp = halcomp
       self.builder = builder
       self.open_action =  self.builder.get_object('hal_action_open')

    def on_file_selected(self,widget,data=None):
       parent = None
       md = gtk.MessageDialog(parent, 
       gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_QUESTION, 
       gtk.BUTTONS_YES_NO, "File selected.")
       md.run()
       file =  self.builder.get_object('FILE_SELECTOR_NAME'.get_selected()
       if file:
            self.open_action.load_file(path)
       md.destroy()

# boiler code
def get_handlers(halcomp,builder,useropts):
    return [HandlerClass(halcomp,builder,useropts)]

I haven't actually tested this code so there could be typos but in essence that is how to do it.

Chris M
Last edit: 03 Feb 2019 23:18 by cmorley.
The following user(s) said Thank You: pferrick, Nico2017

Please Log in or Create an account to join the conversation.

More
04 Feb 2019 00:53 #125625 by pferrick
Thanks again, Chris!

I'm definitely getting better at python all the time, thanks in part to some great online tutorials I've been following. Gtk, on the other hand, is still largely mysterious! I'm going to start fishing around for some good Gtk tutorials so i can stop making a pest of myself on the forum. You sir, are a saint!

Ultimately, I hope to be able to create all sorts of custom windows that pop up and gather information needed by AXIS to modify probing parameters, etc. etc.
I feel like I'm well on my way to that goal.

Please Log in or Create an account to join the conversation.

More
More
06 Feb 2019 22:17 #125882 by pferrick
Hi again-

I'm still not able to get a path from my IconFileSelect widget (which is on a separate glade file by itself, and called simply 'file')
I tried this (my changes to your suggested code in bold):

def on_file_selected(self,widget,data=None):
path = self.builder.get_object(file.get_selected())
parent = None
md = gtk.MessageDialog(parent,
gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_QUESTION,
gtk.BUTTONS_YES_NO, path)
md.run()
md.destroy()

which does not even pop up a messagebox. (A messagebox does appear if I substitute a string in quotes for 'path' in the third-last line. This tells me that the handler file is being read and the signal on_file_selected() is responding.)

It seems to me that path would end up being a reference to the path itself, and not to the file widget. So I also tried this:

def on_file_selected(self,widget,data=None):
p = self.builder.get_object(file)
path = p.get_selected()
parent = None
md = gtk.MessageDialog(parent,
gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_QUESTION,
gtk.BUTTONS_YES_NO, path)
md.run()
md.destroy()

which doesn't work either. I'm certainly very close to having this working, but I'll be darned if I can figure out what's wrong. Once I get hold of the path, using it to open a selected file should be no problem!

tnx,
Patrick

Please Log in or Create an account to join the conversation.

More
06 Feb 2019 22:31 #125885 by cmorley
def on_file_selected(self,widget,data=None):
    md = gtk.MessageDialog(None,
    gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_QUESTION,
    gtk.BUTTONS_YES_NO, path)
    md.run()
    path = self.builder.get_object('file').get_selected()
    print path
    md.destroy()

Please Log in or Create an account to join the conversation.

Moderators: HansU
Time to create page: 0.222 seconds
Powered by Kunena Forum