Just the kind of pointer I needed thanks. I now have two pyui’s of which one is a tableview. I had to use global to enable the selected_row value to be used outside the function, is this the norm? Anyway thanks very much for your help.
Regards
Gary
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.
Posts made by GaryGadget
-
RE: UI Understanding Help
-
UI Understanding Help
Hi,
I am learning how to use Pythonista and am looking for advice on the best method to implement a simple idea for a script I have. I have a GUI created with the designer to set up up a crossword grid. A cell is tapped to turn it either black or white (these are buttons). However, I want another option available when tapped to produce another GUI in the form of a Tableview with a list of numbers to select (up to 26).
My problem is that I do not understand how to load the tableview I created with the designer, into view. Is it a new view requiring a frame or should it be loaded as a subview into my existing frame? All the examples I have found seem to be adding buttons to existing frames. Whereas I think I am trying to add a view which seems different. It wont load using subview with error ‘expecting’ view.
Basically I’m trying to add another GUI called NumberSelector.pyui into the process when a cell is tapped.
Is this the right way to implement my idea or should it be a different approach. If not, what am I doing wrong please?
Thanks.
## AlphaPuzzle Solver by G A ## import ui b = ui.Button() def button_tapped(sender): '@type sender: ui.Button' # Get the button's title for the following logic: t= sender.name b = sender #print(t) #b.background_color = 'black' if (str(b.background_color)) != '(1.0, 1.0, 1.0, 1.0)': b.background_color = 'white' else: b.background_color = 'black' #print(b.background_color) v = ui.load_view('AlphaGrid') if min(ui.get_screen_size()) >= 768: # iPad v.frame = (0, 0, 360, 400) v.present('sheet') else: # iPhone v.present(orientations=['portrait'])
-
RE: Button Background Colour
Don’t know how I missed that. Thanks for your help.
-
RE: Button Background Colour
Thanks for that, I did search the class library for something like this but couldn’t/can’t find it. Am I looking in the wring place?
-
Button Background Colour
Hi,
Quick one I hope.Is it possible to change the background colour of a button from within code, i.e. I want to make the colour dynamic so I can make it all black or white like a crossword. Or should I utilise the image to match my needs?
Thanks for your time.
Regards
Gary -
Crossword/Codeword grid query
Hi, I'm not used to coding with Python so using Pythonista to help learn the ways on my mobile device for convenience. I'm trying to rewrite a codeword solver I wrote in Visual Basic for Excel. I used the spreadsheet grid for the user interface but not sure of the best method to adopt in Pythonista. The idea is to draw a crossword type grid (codeword) which can be edited and updated for each time a letter Is substituted in the grid.
Not sure whether to use layers, user interface or separate tiles etc.Still learning so any pointers or examples for crossword type interface coding help would be greatly appreciated.
Basically my program allows the user to see the effect of a particular letter when it is substituted in the grid. Thus helping to see if other areas of the grid look feasible. If not happy the then the operation can be corrected. Much quicker than pencil and paper apart from the initial set up of the grid.
Thanks for any help.
Regards
Gary -
RE: Crossword/Codeword grid query
Thanks to you all for your help so far. I have made some progress after the last post, if only a small step. I have now managed to get to grips a bit with switching 'views' at the touch of a button based on ccc's idea thanks.
The idea is still to switch between 3 different class types for my codeword, number grid and letter grid. This I have some idea of how I will tackle it but can somebody please explain the use of the 'frame' command in the context of switching between views. i.e. I don't know how to size or position my grids to fill the screen on iPhone/iPad. I have tried different ways from what I have seen but none of them seem to work. In this particular use my classes will be the grids made up of buttons that I want to fill the screen as best they fit.
Oh and I now have a bluetooth keyboard connected to my iPhone so it is much better for typing the lines of code in.
Thanks
Garyimport ui, console def button_tapped(sender): sender.superview.send_to_back() print sender.name class FirstClass(ui.View): def __init__(self): self.background_color = 'blue' button = ui.Button(frame = (0, 0, 1, 1), title = 'Class 1') button.name = button.title button.bg_color = 'white' button.flex = 'WH' self.add_subview(button) button.action = button_tapped class SecondClass(ui.View): def __init__(self): self.background_color = 'red' button = ui.Button(frame = (0, 0, 1, 1), title = 'Class 2') button.name = button.title button.bg_color = 'white' button.flex = 'WH' self.add_subview(button) button.action = button_tapped parent_view = ui.View() grids = [FirstClass(), SecondClass()] for i, b in enumerate(grids): #b.frame = (0, 0, parent_view.width, parent_view.height) b.flex = 'WH' parent_view.add_subview(b) b.send_to_back() parent_view.present('full_screen')
-
RE: Crossword/Codeword grid query
Thanks guys,
I'm just trying to understand how all of the parts will work for my codeword GUI. Once I grasp that I'll be able to put them all together in a neater version.
Basically I wanted to swap between my 3 main views: Codeword grid, number selection grid , and letter selection grid. But keep coming up against beginner type problems with my code. In this latest problem it was getting the SecondClass object (a selection grid) to appear full screen and pass a value back.
I'll look more closely later at your posts but just wanted to thank you all for you prompt and helpful responses. It always helps speed things up but believe me I have spent ages on each stage just trying to get what looks like only a few lines of code.
As I said before 'Baby Steps' for me but learning is satisfying!
Regards
Gary -
RE: Crossword/Codeword grid query
I'm trying to get the buttons to switch between views. How can I pass the size to child_view so it fills the screen like its parent?
Thanks in advance
Garyimport ui # test of view interchanging def button1_tapped(sender): child_view = SecondClass() parent_view.add_subview(child_view) def button2_tapped(sender): print 'button2 tapped' class FirstClass(ui.View): def __init__(self): self.present() self.background_color = 'black' button1 = ui.Button(frame = (1, 1, self.width, self.height)) print 'parent_view started at ', self.width button1.title = 'First Class' self.add_subview(button1) button1.action = button1_tapped class SecondClass (ui.View): def __init__(self): print 'parent_view now = ', parent_view.width self.background_color = 'white' button2 =ui.Button(frame = (1, 1, parent_view.width, parent_view.height)) button2.title = 'Second Class' parent_view.add_subview(button2) button2.action = button2_tapped parent_view = FirstClass()
-
RE: Crossword/Codeword grid query
Well spotted. Yes it is a mistake. I was getting errors when trying to use 'white' in equality tests so I used the tuple values instead. I couldn't remember what the values for black were but didn't implement it in the end. I'll reuse your line when I rewrite, once I know how all the parts work. The main obstacles are my inexperience with GUI's & Classes but also trying to learn how to use these techniques on my iPhone in my spare time. I am really impressed with the shear power of Pythonista so far though. Bit fiddly editing on the phone at times though. Thanks for your help.
-
RE: Crossword/Codeword grid query
Spot on. Thanks for that JonB. I've been having a look at the subview approach because I want it on iPhone (no popover iI believe). Managed to get it to work after a lot of trial and error.
Once I'm happy with how all the parts work I will rewrite because at this stage I am taking baby steps with the GUI programming within Pythonista so am really great full for all your help. -
RE: Crossword/Codeword grid query
Hi chaps,
I'm still playing around with buttons for my codeword app. Struggling now with changing the view from one view to another. How is it done please or have I got the wrong end of the stick here?The idea is to firstly select which cells are black. Click done. Then selecting a white cell should present my second class as a view to choose number values from a grid. Trouble is the grid becomes too small when I call it from anywhere but the line not indented!
import console, ui, time crossword_rows = 4 crossword_cols = crossword_rows gap = 1 tupWhite = tuple([1.0,1.0,1.0,1.0]) # white values tupBlack = tuple([1.0,1.0,1.0,1.0]) # black status = 1 def button_tapped1(square): global status #print 'status = '+ str(status) #tup = tuple([1.0,1.0,1.0,1.0]) # white values if status==1: #print 'status = '+ str(status) if str(tupWhite) == str(square.bg_color): square.bg_color = 'black' else: square.bg_color = 'white' elif status==2: #button_tapped2() edit_cell_number(square) print 'step 2' #pass def button_tapped2(button): #add square number global status #print 'status = '+ str(status) status = 2 button.title = 'Colours set.' #print 'status = '+ str(status) #pass def button_tapped3(self, square): pass def edit_cell_number(square): #cyvle through 1 - 26 for cell number if str(tupWhite) == str(square.bg_color): #square.title = str(26) #NumberSelectionGrid() pass class CrosswordView(ui.View): def __init__(self): self.present(hide_title_bar=False ) self.background_color = (0, 1.0, 2.0, 0.4) min_dimension = min(self.width / crossword_cols, self.height / crossword_rows) #self.squares = [] for i in xrange(crossword_cols): x = i * min_dimension for j in xrange(crossword_rows): time.sleep(.05) y = j * min_dimension square = ui.Button(frame = (x, y, min_dimension-gap, min_dimension-gap)) #square.alignment = #ui.ALIGN_CENTER square.bg_color = 'white' #square.title = 'X' self.add_subview(square) square.action = button_tapped1 #button = ui.Button(frame = (x-150, y+80, min_dimension*2, min_dimension*2)) button = ui.Button(frame = (0,0, min_dimension*4, min_dimension*2)) self.add_subview(button) button.title = 'Done' button.background_color = 'white' button.center = (self.width * 0.5, self.height -50) button.flex = 'LRTB' button.action = button_tapped2 #print 'another visit' class NumberSelectionGrid(ui.View): def __init__(self): self.present(hide_title_bar=False ) self.background_color = (0, 1.0, 2.0, 0.4) min_dimension = min(self.width / 5, self.height / 6) #self.squares = [] squareNumber = 1 for i in xrange(5): x = i * min_dimension for j in xrange(6): #time.sleep(.05) y = j * min_dimension square = ui.Button(frame = (x, y, min_dimension-gap, min_dimension-gap)) #square.alignment = #ui.ALIGN_CENTER square.bg_color = 'white' square.title = str(squareNumber) self.add_subview(square) squareNumber = squareNumber +1 square.action = button_tapped3 CrosswordView() NumberSelectionGrid() #NumberSelectionGridd().present(hide_title_bar=False) #NumberSelectionGrid().send_to_back() #print 'status = '+ str(status)
-
RE: Crossword/Codeword grid query
Thanks for the code examples. I'm sure they will help.
-
RE: Crossword/Codeword grid query
JonB,
I had been trying Global but it wasn't working but since you confirmed my thoughts I succeed thanks. I didn't realise it had to be declared global in each block!Here's my attempt so far as test code for setting up the matrix black/white and to include it in this message properly.
Thanks for your help guys.
import console, ui, time crossword_rows = 4 crossword_cols = 4 gap = 1 status = 1 def button_tapped1(square): global status print 'status = '+ str(status) tup = tuple([1.0,1.0,1.0,1.0]) # white values if status==1: #print 'status = '+ str(status) if str(tup) == str(square.bg_color): square.bg_color = 'black' else: square.bg_color = 'white' elif status==2: #button_tapped2() pass def button_tapped2(button): #add square number global status print 'status = '+ str(status) status = 2 button.title = 'Colours set.' #print 'status = '+ str(status) #pass class CrosswordView(ui.View): def __init__(self): self.present(hide_title_bar=False ) self.background_color = (0, 1.0, 2.0, 0.4) min_dimension = min(self.width / crossword_cols, self.height / crossword_rows) #self.squares = [] for i in xrange(crossword_cols): x = i * min_dimension for j in xrange(crossword_rows): time.sleep(.05) y = j * min_dimension square = ui.Button(frame = (x, y, min_dimension-gap, min_dimension-gap)) #square.alignment = #ui.ALIGN_CENTER square.bg_color = 'white' #square.title = 'X' self.add_subview(square) square.action = button_tapped1 button = ui.Button(frame = (x-150, y+80, min_dimension*2, min_dimension*2)) self.add_subview(button) button.title = 'Done' button.background_color = 'white' button.action = button_tapped2 #print 'another visit' CrosswordView() #print 'status = '+ str(status)
-
RE: Crossword/Codeword grid query
Thanks for the reply. I've managed this so far but not sure how to manage the scope for my Constant 'status'.
Once the 'Done' button is tapped 'status' should be set to 2 and then stop the black/white toggle code using the 'if' block. I think scope is the problem here but not sure how to cure it. Any help appreciated.
Also don't know how to show my code like others do on the message sorry.import console, ui, time
crossword_rows = 4
crossword_cols = 4
gap = 1
status = 1def button_tapped1(square):
#print 'status = '+ str(status)
tup = tuple([1.0,1.0,1.0,1.0]) # white values
if status==1:
#print 'status = '+ str(status)
if str(tup) == str(square.bg_color):
square.bg_color = 'black'
else:
square.bg_color = 'white'
elif status==2:
#button_tapped2()
passdef button_tapped2(button):
#add square number
#print 'status = '+ str(status)
status = 2
button.title = 'ok'
#print 'status = '+ str(status)
passclass CrosswordView(ui.View):
def init(self):
self.present(hide_title_bar=False )
self.background_color = (0, 1.0, 2.0, 0.4)
min_dimension = min(self.width / crossword_cols, self.height / crossword_rows)
#self.squares = []
for i in xrange(crossword_cols):
x = i * min_dimension
for j in xrange(crossword_rows):
time.sleep(.05)
y = j * min_dimension
square = ui.Button(frame = (x, y, min_dimension-gap, min_dimension-gap))
#square.alignment = #ui.ALIGN_CENTER
square.bg_color = 'white'
#square.title = 'X'
self.add_subview(square)
square.action = button_tapped1button = ui.Button(frame = (x-150, y+80, min_dimension*2, min_dimension*2)) self.add_subview(button) button.title = 'Done' button.background_color = 'white' button.action = button_tapped2 #print 'another visit'
CrosswordView()
print 'status = '+ str(status) -
RE: Crossword/Codeword grid query
I've been having a play around and trying to populate a matrix with buttons but can't find much in the docs to help. I have got a button to swap from black to white at the touch of the screen but not sure how to populate with more than one button. When I try 2 they end up on top of each other. Dynamic matrix preferred so not using the builder. Can't see anything for the button class attributes relating to position/size or a constructor. Is there a view required for each object or view? Please can someone explain how it is done?
-
RE: Crossword/Codeword grid query
Thanks for these chaps.
JonB my grids usually 13 or 15 cells depending on which newspaper/source for the puzzle. I suppose the cells could remain the same size as long as grid can be viewed clearly.Brumm, I think I follow. A black or white background for the buttons running a matrix grid?
I'll have a study of the UI module. Can a UI be pinched/zoomed? Hope to view on iPhone as well as iPad you see.
Thanks again
Gary -
RE: Flappy Bird on Pythonista
Found it thanks although I could only find it on my iPad and not on my iPhone!
Great find this app and forum, hopefully now I can help my school kids a bit more at my convenience if you know what I mean.
Regards
Gary