Hi!
I am trying to get better in Python/Pythonista by doing a pixel paint app. I have some problems understanding communication between views, nested and otherwise, and I was hoping you might have some suggestion to how I can make my code more elegant.
My paint app is built up from views, there is an editor view at the "root" holding the image and tool buttons. What I want to do, is to open a file selector view on top of this, that does stuff with the editor view "below". For that I need the file view to access variables and functions that belong to the root editor.
Right now, I have nested everything. The editor functions are inside the editor class, and the file view is a function of the editor class. Also, the file view functions are nested inside the file view function itself. But this feels real messy to me, and I'd rather have the file window as a separate class that is outside of the editor. But I'm not sure how to let the communicate back to the editor!
I have made a super-simplified example of how my app works. Can anybody take a quick look and give me some hints?
#!python3
import ui
from glob import glob
from os.path import basename
class pixelEditor(ui.View):
def fileWindow(self, sender):
fileWindow = ui.View(frame=(100, 150, 300, 300), name='File window', border_width=2)
imagefiles = [basename(x) for x in glob('*.*')]
def loadAction(sender):
# Dummy for the function that loads the image into the editor
selectedFile = imagefiles[filelist.selected_row[1]]
print ('Selected ' + selectedFile + ' from sender: ' + sender.name)
# Sends the selected image to the pixel Editor
self.subviews[1].text = selectedFile
def fileSelected(sender):
filePreview.background_color = 'red'
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 = fileSelected # Does not work...
fileWindow.add_subview(filelist)
filePreview = ui.ImageView(frame=(170,10,120,100))
filePreview.background_color = 'black'
fileWindow.add_subview(filePreview)
loadButton = ui.Button(name='Load', frame=(170,120,64,32), title='Load')
loadButton.background_color = 'white'
loadButton.action = loadAction
fileWindow.add_subview(loadButton)
self.add_subview(fileWindow)
print('File window opened.')
def __init__(self, width=640, height=480):
self.bg_color = 'grey'
fileButton = ui.Button(name='File', frame=(10,80,64,32), title='File Window')
fileButton.background_color = 'white'
fileButton.action = self.fileWindow
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)
v = pixelEditor()
v.present('fullscreen')