Forum Archive

Need help with buttons

pelowtown24

Im trying to create buttons so this action can be pass after. I’m new at this sorry to a pain. Here is my scrip without the buttons:

```
import random

def workerFunction():
while True:
try: x=input()

    except EOFError: pass
    if x.lower().startswith('q'):
        break


    elif x == '1': print( 'Megabucks',factory.factoryLogic ( 1, 46, 6 ) )
    elif x == '2': print( 'Lucky For Life',factory.factoryLogic (1, 48, 5 ),factory.factoryLogic( 1, 18, 1 ) )
    elif x == '3': print( 'Powerball',factory.factoryLogic (1, 69, 5 ),factory.factoryLogic( 1, 26, 1 ) )
    elif x == '4': print( 'Mega Millions',factory.factoryLogic (1, 70, 5 ),factory.factoryLogic( 1, 25, 1 ) )

class factory:

def __init__( self ): self.a = 0


def factoryLogic( startPosi, endPosi, interateNumber ):
    a = random.sample( range( startPosi, endPosi+1 ), interateNumber )
    a.sort()
    return a

if name == 'main':
workerFunction() ```

cvp

@pelowtown24 please, first of all, use the button above and insert your code between the two generated lines

pelowtown24

All set thanks

cvp

@pelowtown24 said:

Im trying to create buttons so this action can be pass after

Sorry, but I don't understand. Do you want one button or one per different x

pelowtown24

@cvp one per each one which is 4 of them. Anything will help

cvp

@pelowtown24 very very quick and dirty if I correctly understood

import random
import ui

def workerFunction():
    v = ui.View()
    v.background_color = 'white'
    for i in range(1,5):
        b = ui.Button()
        b.frame = (50+i*80,10,32,32)
        b.border_width = 1
        b.title = str(i)
        b.action = b_action
        v.add_subview(b)
    l = ui.Label(name='l')
    l.frame = (50,50,400,32)
    v.add_subview(l)
    v.present()

def b_action(sender):
    x = sender.title
    l = sender.superview['l']
    if x == '1': l.text = 'Megabucks'+str(factory.factoryLogic ( 1, 46, 6 ) )
    elif x == '2': l.text = 'Lucky For Life'+str(factory.factoryLogic (1, 48, 5 ))+str(factory.factoryLogic( 1, 18, 1 ) )
    elif x == '3': l.text = 'Powerball'+str(factory.factoryLogic (1, 69, 5 ))+str(factory.factoryLogic( 1, 26, 1 ) )
    elif x == '4': l.text = 'Mega Millions'+str(factory.factoryLogic (1, 70, 5 ))+str(factory.factoryLogic( 1, 25, 1 ) )


class factory:
    def __init__( self ): 
        self.a = 0

    def factoryLogic( startPosi, endPosi, interateNumber ):
        a = random.sample( range( startPosi, endPosi+1 ), interateNumber )
        a.sort()
        return a

if __name__ == '__main__':
    workerFunction() 
pelowtown24

@cvp awesome thank you šŸ™šŸ»

cvp

@pelowtown24 never mind but not nice at all, believe me. Hoping for you that a more skilled guy posts a more professional script

mikael

@pelowtown24, is there a story behind the workers and factory? These patterns are rarely seen (or needed) in Python, which makes me think that either you are planning something very big & complicated, or you are coming from a language like Java.

pelowtown24

@mikael it came from java

mikael

@pelowtown24, I came from Java, as well, years ago. I highly recommend learning the unique features of Python, you might end up loving them.

If you do not have time for a full course or a book, you might get a taste with something like the 30 Python tricks.

pelowtown24

@mikael will do look into it thanks

cvp

@mikael Thanks, 4 on 30 were unknown for me. I've never learned Python, only started from some examples in Pythonista (but 40 years of experience in 12 other languages)