December 16th, 2012, 02:21 PM
Lottery Number Generator
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:
numList = 
while len(numList) != 6:
number = random.randint(1,49)
if number in numList:
counter = 0
lineNum = int(input("How many lines of numbers do you want: "))
if lineNum < 1:
print "Invalid selection. Please try again... "
while counter != lineNum:
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.
December 17th, 2012, 05:18 AM
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.
Originally Posted by ibanezmatt13
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.
# -*- coding: UTF-8 -*-
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))
btnGenerate = wx.Button(self, label='Generate', pos=(100, 40))
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', \
app = wx.App()
LotteryApp(None, -1, 'Lottery App')
My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (work laptop), Trisquel GNU/Linux 6.0.1 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
December 17th, 2012, 11:22 AM
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.