Filebrowser and Gcode editor

More
03 Jun 2022 00:51 #244480 by cakeslob
I apologize for my ineptitude in advance. Enthusiasm can only get me so far before I have more questions than results.

File browser-

For some reason, the linux tk file browser is not very good. I found a good candidate replacement for it
 

 github.com/j4321/tkFileBrowser

For implementation into usercommand file, I figured I change the open_file command here
github.com/LinuxCNC/linuxcnc/blob/3b17ed...cripts/axis.py#L2198
f = root_window.tk.call("tk_getOpenFile", "-initialdir", open_directory,
            "-filetypes", types)
change this to the new one, and any others i find
probably add something like this to the top, I think I can remove the ttk part
from tkfilebrowser import askopendirname, askopenfilenames, asksaveasfilename, askopenpathnames
try:
    import tkinter as tk
    from tkinter import ttk
    from tkinter import filedialog
except ImportError:
    import Tkinter as tk
    import ttk
    import tkFileDialog as filedialog


I have questions as to what I do with the new filebrower files....do I install it (globally?) like it says in the github using pip, or should I be putting all the files into the config folder with the usercommand , and calling on them from there? What is that called and how would I change the import to reflect that?



This next question is about an embedded editor, but also related to the file browser
If I wanted it to not be a dialog but and embedded tab......
Is the reason I cannot embedded because things like genedit, tooledit, this new filedialog, and vismach create a top level window?


github.com/LinuxCNC/linuxcnc/blob/master/tcl/bin/genedit.tcl
github.com/LinuxCNC/linuxcnc/blob/master/tcl/tooledit.tcl
Attachments:

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

More
04 Jun 2022 17:29 #244578 by cakeslob
At any rate, on the axis editor,
I think I am close, but I run into trouble when trying to get the current file path. here is what I have tried so far. some feel close, but not quite.

if anyone can chime in and point me in the right direction on this, that would be cool.

here are the things Ive tried so far
import linuxcnc
s = linuxcnc.stat()
### we will change this back to w later  ####
rC = root_window.tk.call

######## doesnt work    #################

#open_file=open(os.path.basename(f),'r')
#open_file=open(os.path.join(tempdir, os.path.basename(loaded_file)),'r')
#open_file=open(os.path.abspath(f),'r')
#open_file=open(os.path.exists(initialfile),'r')
#open_file=open(os.path.dirname(f),'r')
#open_file=open(os.path.(loaded_file),'r')

#open_file=open(os.stat(loaded_file),'r')
#TypeError: expected str, bytes or os.PathLike object, not os.stat_result


### maybe close ######

#open_file=open(os.access(loaded_file,os.W_OK),'r')
# hard crashes axis

###########  closest ones i think    #####################

#open_file=open(os.path.basename(loaded_file),'r')
#FileNotFoundError: [Errno 2] No such file or directory: 'axis.ngc'

#open_file=open(s.file(),'r')
#TypeError: 'str' object is not callable

rC('.pane.top.tabs.fedit.t.text','delete','1.0','end')
rC('.pane.top.tabs.fedit.t.text','insert','end','open_file.read()')
open_file=open(os.path.basename(loaded_file),'r')
this one feels pretty close but I cant figure out what gives me the full path name
#open_file=open(s.file(),'r')
this one I know outputs the full file path, but it complains

and some of those options....seem to not error, but the text they insert isnt the gcode file, it will insert 'open_file.read()' into my text widget
Attachments:

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

More
05 Jun 2022 00:06 #244590 by phillc54
I am possibly misunderstanding what you want to do but...
If you are looking for just the path name then you don't need the open command, this should do it:
file_path = os.path.dirname(loaded_file)
for the file:
file_name = os.path.basename(loaded_file)
or for both you could also do:
file_path, file_name = loaded_file.rsplit('/', 1)
 

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

More
05 Jun 2022 04:17 - 05 Jun 2022 05:28 #244594 by cakeslob
shit i didnt explain what i was trying to do.

Trying to make embeded gcode editor, using a text widget and automatically loading the same file thats loaded into axis, and then on exiting the tab, it saves and reloads the program in axis

I didnt think about combining them, but when i used your example it was complaining about a list instead of a string, which made me think I can test these paths using print

i tried linuxcnc.stat.file again and it didnt print, i was adding brackets s.file() and that was messing it up

open_file=open(s.file,'r')
rC('.pane.top.tabs.fedit.t.text','delete','1.0','end')
rC('.pane.top.tabs.fedit.t.text','insert','end','open_filea.read()')

Now I am having an issue with it updating the file when it changes. I put it into user live update, but now it updates too much.
another unforeseen issue is all the keyboard hotkeys going wild when i edit the gcode file. even with takefocus, I did not expect this because i did not have this issue with my search entry widget
Attachments:
Last edit: 05 Jun 2022 05:28 by cakeslob.

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

More
06 Jun 2022 04:33 #244648 by cakeslob
 

I have a working in progress editor tab(it looks the same as gcode tab but with broken buttons). I can open, edit and save the current file in axis. I had to do this using keybinds 1 and 2 because for some reason the commands dont work with buttons

problems:

- keybinds still work when text widget is in focus. entry widget works fine but text widget processes all keybinds, making it awful. dont know what to do about this

- from what I understand, the undo causes an issue? what if i have like max undo limit?

- doing a command on tab change, i thought this was an option for notebook but its not. axis does it somehow. trying to load the file only on tab change to edit tab, which would solve my update file problem

- doing a clean exit where it can save and reload the file into axis, and maybe close the file if thats a thing. dont know what i was planning on this one. can you do a command on tab exit some how?
Attachments:

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

More
06 Jun 2022 07:10 #244654 by phillc54

- doing a command on tab change, i thought this was an option for notebook but its not. axis does it somehow. trying to load the file only on tab change to edit tab, which would solve my update file problem


The following should do this:
def page_switched(page):
    print('switched to', page)


TclCommands.page_switched = page_switched


rC('.pane.top.tabs','bindtabs','<ButtonPress-1>','page_switched')

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

More
07 Jun 2022 03:50 - 07 Jun 2022 03:52 #244704 by cakeslob
Hey Phil, thanks for helping me out.

I was trying to work with your example, but I keep getting the similar error for that, as I do with my save and openfile commands(when they are not bound to a key). Im getting a similar error when I try to do anything else with tabs. If you can enlightening me on whats going on, this is an issue i face sometimes that I have yet to understand except it works if you bind it to a key.
Also when looking into bindtab, I realised bwidget appears to be better than ttk notebook, bwidget has the tab command thing I was looking for
def page_switched(page):
print('switched to', page)

TclCommands.page_switched = page_switched
rC('.pane.top.tabs','bindtabs','<ButtonPress-1>',page_switched)

#TCL error in asynchronous code:
#invalid command name "<function"
# while executing
#"<function page_switched at 0xaf2dd468> [NoteBook::_get_page_name .pane.top.tabs current 1]"
# (command bound to event)

def load_editfile(self):
print (s.file)
open_filea = open(s.file,'r')
rC('.pane.top.tabs.fedit.t.text','delete','1.0','end')
rC('.pane.top.tabs.fedit.t.text','insert','end',open_filea.read())



def save_editfile(self):
print (s.file)
open_filea = open(s.file,'w')
edit_gcode = rC('.pane.top.tabs.fedit.t.text','get','1.0','end')
open_filea.write(edit_gcode)
return 1



TclCommands.save_editfile = save_editfile
TclCommands.load_editfile = load_editfile


rC('.pane.top.tabs','insert','end','edit','-text',' edit ','-raisecmd',load_editfile,'-leavecmd',save_editfile)

#TCL error in asynchronous code:
#invalid command name "<function"
# while executing
#"<function load_editfile at 0xaf361e88>"
# ("uplevel" body line 1)
# invoked from within
#"uplevel \#0 $cmd"
# (procedure "NoteBook::_select" line 34)
# invoked from within
#"NoteBook::_select .pane.top.tabs edit"
# (command bound to event)
 
Attachments:
Last edit: 07 Jun 2022 03:52 by cakeslob.

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

More
07 Jun 2022 08:09 #244712 by phillc54
Have you tried with single quotes around the commands?
'page_switched' or 'save_editfile'
 

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

More
07 Jun 2022 13:35 #244720 by cakeslob
Yeah, right after I posted I noticed yours had the quotes (sending a command as a string to tcl?), so I tried it with quotes thinking that would do it, but it was the same thing ,for save_editfile at least

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

More
07 Jun 2022 23:55 #244741 by phillc54
You need to put the quotes around load_editfie as well. Basically everything in a tk.call needs to be text. The following works for me:
def page_switched(page):
    print('switched to', page)

def save_editfile():
    print('saving', s.file)

def load_editfile():
    print('loading', s.file)

TclCommands.page_switched = page_switched
TclCommands.save_editfile = save_editfile
TclCommands.load_editfile = load_editfile

rC('.pane.top.tabs','bindtabs','<ButtonPress-1>','page_switched')
rC('.pane.top.tabs','insert','end','edit','-text',' edit ','-raisecmd','load_editfile','-leavecmd','save_editfile')

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

Time to create page: 0.204 seconds
Powered by Kunena Forum