Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Finding a more elegant way of communicating between views
-
@superrune file name in label
#self.superview.name = selectedFile self.superview['File Label'].text = selectedFile
-
@superrune other problem
def fileSelected(self,sender): # This does not work.. filePreview.background_color = 'red'
does not work because filePreview does not exist
-
@superrune TableView does not have an action...I'll show you how to do, wait
-
@superrune this
filelist.delegate = self #filelist.action = self.fileSelected # This does not work...
and this instead your fileSelected
def tableview_did_select(self, tableview, section, row): # Called when a row was selected. self['filePreview'].background_color = 'red' #def fileSelected(self,sender): # This does not work.. #self['filePreview'].background_color = 'red'
-
Cool, now most of the script seems to work as it should. Thanks again!
I changed the text assignment to this, and now it appears where I intended:
# Sends the selected image to the pixel Editor self.superview['File Label'].text = selectedFile
filePreview
should exist though, should it not? I created it as an imageview a couple lines up, so is should be within the scope of the file window. Looks more like the function isnt called at all when I press the different table lines.Here is how it looks right now:
import ui from glob import glob from os.path import basename class fileWindow(ui.View): def __init__(self): self.frame=(100, 150, 300, 300) self.name = 'File window' self.border_width = 2 print ('Loader superview at init:', self.superview) imagefiles = [basename(x) for x in glob('*.*')] filelistData = ui.ListDataSource(imagefiles) filelistData.delete_enabled=False filelist = ui.TableView(frame=(10, 10 ,150, 280), data_source=filelistData, name='filelist') filelist.row_height = 24 filelist.action = self.fileSelected # This does not work... self.add_subview(filelist) filePreview = ui.ImageView(frame=(170,10,120,100)) filePreview.background_color = 'black' self.add_subview(filePreview) loadButton = ui.Button(name='Load', frame=(170,120,64,32), title='Load') loadButton.background_color = 'white' loadButton.action = self.loadAction self.add_subview(loadButton) ui.delay(self.x,0.01) def x(self): print ('Loader superview type = ', type(self.superview)) def loadAction(self,sender): selectedFile = self['filelist'].data_source.items[self['filelist'].selected_row[1]] print ('Selected ' + selectedFile + ' from sender: ' + sender.name) # Sends the selected image to the pixel Editor self.superview['File Label'].text = selectedFile def fileSelected(self,sender): # This does not work.. print('Gonk!!') filePreview.background_color = 'red' class pixelEditor(ui.View): def __init__(self, width=640, height=480): self.bg_color = 'grey' fileButton = ui.Button(name='File', frame=(10,80,64,32), title='File:') fileButton.background_color = 'white' fileButton.action = self.openFileWindow self.add_subview(fileButton) fileLabel = ui.Label(frame=(100, 80, 300, 32), font=('HelveticaNeue-Light', 32), text='___') fileLabel.name = 'File Label' self.add_subview(fileLabel) print(self.superview) def openFileWindow(self, sender): fv = fileWindow() self.add_subview(fv) #fv.present() print('File window opened.') v = pixelEditor() v.present('fullscreen')
-
Cool, saw your answer now!! I keep replying to fast :)
-
@superrune to see images like jpg, ping, gif,
Put in init
filePreview.content_mode = ui.CONTENT_SCALE_ASPECT_FIT
And in selection
def tableview_did_select(self, tableview, section, row): # Called when a row was selected. try: fil = tableview.data_source.items[row] self['filePreview'].background_color = 'red' self['filePreview'].image = ui.Image.named(fil) except Exception as e: # not an image #print(e) pass
-
@superrune Of course, these 3 lines may be commented, they have been added only to show you that self.superview is known only after init...
ui.delay(self.x,0.01) def x(self): print ('Loader superview type = ', type(self.superview))
-
@superrune If you want a preview of a lot of files types, not only images, do
from os.path import basename,abspath import sys
and
filePreview = ui.ImageView(frame=(170,10,120,100)) filePreview.name = 'filePreview' filePreview.background_color = 'black' filePreview.content_mode = ui.CONTENT_SCALE_ASPECT_FIT self.add_subview(filePreview) textview = ui.TextView(name='textview') textview.frame = filePreview.frame self.add_subview(textview) webview = ui.WebView(name='webview') webview.frame = filePreview.frame self.add_subview(webview)
and
def tableview_did_select(self, tableview, section, row): # Called when a row was selected. try: fil = tableview.data_source.items[row] i = fil.rfind('.') ext = fil.lower()[i:] if ext in ['.txt','.dat','.py','.md']: with open(fil,'r',encoding='utf-8') as f: t = f.read() self['textview'].text = t self['textview'].bring_to_front() elif ext in ['.png','.jpg','.jpeg','.bmp','.tif','.tiff']: self['filePreview'].image = ui.Image.named(fil) self['filePreview'].bring_to_front() elif ext in ['.gif','.htm','.html','.webarchive','.pdf','.mov','.mp3','.wav','.m4a','.mp4','.avi','.doc','.docx','.xls','.xlsx','.pps','.ppt','.gmap']: f = sys.argv[0] # folder of script i = fil.rfind('/') self['webview'].load_url(abspath(f[:i+1]+fil)) self['webview'].bring_to_front() else: self['filePreview'].background_color = 'red' self['filePreview'].bring_to_front() except Exception as e: # not an image print(e) pass
-
Thanks @cvp! This is great!
-
@superrune 👍
Don't hesitate for any new questionYou have to know that there is a FilePicker from omz and other ones....