#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Lottery Number Generator


    Hi,

    I have recently written a project in Python which creates a number of lines of random generations; this number is specified by the user upon launch.

    At the moment, the program runs in console and does not have a graphical user interface, despite numerous attempts of using wx python to achieve this. Here is my code:

    import random

    numList = []

    def generate_numbers():

    del numList[:]
    while len(numList) != 6:
    number = random.randint(1,49)
    if number in numList:
    pass
    else:
    numList.append(number)
    numList.sort()

    def Go():

    counter = 0
    lineNum = int(input("How many lines of numbers do you want: "))
    print("")
    if lineNum < 1:
    print "Invalid selection. Please try again... "
    Go()
    else:
    while counter != lineNum:
    generate_numbers()
    print(numList)
    counter +=1

    Go()



    The issue is, I would like to be able to create a graphical user interface for this app. I wish to do this using wx python. I need to create a window, add a few buttons (one to generate, one to quit perhaps) and some static text (or a textbox depending on which is more appropriate).

    I understand how to create windows and add buttons and so on, although I do not understand exactly how I can accommodate my Lottery code above into a graphical interface version.

    Any help on this will be greatly appreciated. Cheers.

    Ibanezmatt13
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    439
    Rep Power
    67
    Originally Posted by ibanezmatt13
    I understand how to create windows and add buttons and so on, although I do not understand exactly how I can accommodate my Lottery code above into a graphical interface version.

    Any help on this will be greatly appreciated. Cheers.
    Well, here’s a simple example. A spin control is used to control the number of rows because this way you don’t have to check the validity of the input (user can’t input anything but integers in the predetermined range). A separate message box is used to show the results because this way wxPython automatically resizes the box according to the number of rows generated. In actual practice this of course is a bad solution: copying to clipboard is difficult and the output is ugly. A real fancy solution would be to use a listbox but I leave that to you.

    Also, it would be best to do the actual generation of the numbers elsewhere, maybe outside the class or even in a separate .py file, so that this class would only handle the user interface.

    Code:
    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-
    
    import wx
    
    class LotteryApp(wx.Frame):
        def __init__(self, parent, id, title):
            wx.Frame.__init__(self, parent, id, title, size=(200, 100))
            panel = wx.Panel(self)
            wx.StaticText(self, label='Number of rows to generate:', pos=(10, 20))
            self.Counter = wx.SpinCtrl(self, value='1', pos=(10, 40), size=(50, -1))
            self.Counter.SetRange(1, 1000)
            btnGenerate = wx.Button(self, label='Generate', pos=(100, 40))
            btnGenerate.Bind(wx.EVT_BUTTON, self.generate_numbers)
            self.Center()
            self.Show(True)
    
        def generate_numbers(self, event):
            # Just an example “row”, this is what you generate
            lottery_numbers = [1, 2, 3]
            contents = []
            for row in range(self.Counter.GetValue()):
                contents.append(', '.join([str(num) for num in lottery_numbers]))
            wx.MessageBox('\n'.join(contents), 'Lottery numbers', \
                          wx.OK|wx.ICON_INFORMATION)
    
    app = wx.App()
    LotteryApp(None, -1, 'Lottery App')
    app.MainLoop()
    My armada: openSUSE 13.2 (home desktop, work desktop), openSUSE 13.1 (home laptop), Debian GNU/Linux 7.7.0 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Smile Thanks Super Oscar


    Thanks for your reply,

    The only issue with this code is that when I run it and select the number of lines, it produces the right number of lines but with all the same random numbers.

    I'll continue to work on it as it does help me to understand where the coding fits in to the window creation.

    Many Thanks
    Matt

IMN logo majestic logo threadwatch logo seochat tools logo