1

I am trying to create a GUI program in python to generate random Lottery numbers. I want to have a menu bar for choosing Powerball or MegaMillion. And after choosing it, there will be buttons to let user select how many tickets to show. But I cannot make the method work. I want to show different lines of number lists when I click the button, but it does show anything. I'm not finished yet since it doesn't work. I am new to Python and programming, please help! Thank you !

from tkinter import *
import random

class lotteryNum:
    def __init__(self):
        window=Tk()
        window.title("Lottery Number Generator")

        menubar = Menu(window)
        window.config(menu=menubar)

        self.pbLst = [x for x in range(1,70)]
        self.pbLst2=[x for x in range(1,27)]
        self.mmLst = [x for x in range(1,76)]
        self.mmLst2=[x for x in range(1,16)]
        self.usingLst=["*"]*6



        #Type Menu
        typeMenu = Menu(menubar, tearoff = 0)
        menubar.add_cascade(label = "Which Lottery", menu = typeMenu)
        typeMenu.add_command(label="Powerball", command= self.powerBall)
        typeMenu.add_command(label="Mega Milion", command= self.megaMillion)


        #Exit menu
        exitmenu = Menu(menubar, tearoff = 0)
        menubar.add_cascade(label = "Exit", menu = exitmenu)
        exitmenu.add_command(label = "Quit", command = window.quit)

        #Welcome label
        Label(window, text="Welcome to Lottery Generator!").pack()

        frame=Frame(window)
        frame.pack()

        #Text Label
        self.showResults = StringVar()
        Label(frame, text=self.showResults).pack()




        Button(window, text="$2", command=self.runLottery()).pack(side=LEFT)

        window.mainloop()


    def powerBall(self):
        # random.shuffle(self.pbLst)
        # random.shuffle(self.pbLst2)
        self.usingLst=self.pbLst
        self.usingLst2=self.pbLst2

    def megaMillion(self):
        # random.shuffle(self.mmLst)
        # random.shuffle(self.mmLst2)
        self.usingLst=self.mmLst
        self.usingLst2=self.mmLst2

    def runLottery(self):
        random.shuffle(self.usingLst)
        random.shuffle(self.usingLst2)
        self.usingLst[:5].extend(self.usingLst2[0])
        self.showResults.set(self.usingLst)





lotteryNum()

3 Answers 3

1

I think your code should work if you change this line:

Label(frame, text=self.showResults).pack()

to this:

Label(frame, textvariable=self.showResults).pack()
Sign up to request clarification or add additional context in comments.

Comments

1

Finished code just let whoever interested know what I'm try to do here.

from tkinter import *
import random

class lotteryNum:
    def __init__(self):
        window=Tk()
        window.title("Lottery Number Generator")

        menubar = Menu(window)
        window.config(menu=menubar)

        self.pbLst = [x for x in range(1,70)]
        self.pbLst2=[x for x in range(1,27)]
        self.mmLst = [x for x in range(1,76)]
        self.mmLst2=[x for x in range(1,16)]
        self.usingLst=[0]*6
        self.usingLst2=[0]*6

        #Type Menu
        typeMenu = Menu(menubar, tearoff = 0)
        menubar.add_cascade(label = "Which Lottery", menu = typeMenu)
        typeMenu.add_command(label="Powerball", command= self.powerBall)
        typeMenu.add_command(label="Mega Milion", command= self.megaMillion)


        #Exit menu
        exitmenu = Menu(menubar, tearoff = 0)
        menubar.add_cascade(label = "Exit", menu = exitmenu)
        exitmenu.add_command(label = "Quit", command = window.quit)

        #Welcome label
        Label(window, text="Welcome to Lottery Generator!").pack()

        frame=Frame(window)
        frame.pack()

        #Text Label to show numbers
        self.showResults = StringVar()
        self.showResults5=StringVar()
        oneTicket=Label(frame, textvariable=self.showResults).pack()
        fiveTicket=Message(frame,textvariable=self.showResults5).pack()

        #Buttons to generate tickets
        Button(window, text="one ticket", command=self.showTicket).pack(side=LEFT)
        Button(window, text="five tickets", command=self.showTicket5).pack(side=LEFT)

        #Button to draw the tickets
        Button(window, text="Draw tickets", command=self.drawTickets).pack()

        #The process to draw each ticket
        self.covered=False





        window.mainloop()


    def powerBall(self):
        self.usingLst=self.pbLst
        self.usingLst2=self.pbLst2
        self.runLottery()
        self.winingNumbers = self.showLst

    def megaMillion(self):
        self.usingLst=self.mmLst
        self.usingLst2=self.mmLst2
        self.runLottery()
        self.winingNumbers = self.showLst

    def runLottery(self):
        random.shuffle(self.usingLst)
        random.shuffle(self.usingLst2)
        self.showLst=self.usingLst[:5]
        self.showLst.sort()
        self.showLst.append(self.usingLst2[0])

    def showTicket(self):
        self.runLottery()
        if (self.showLst==self.winingNumbers):
            self.covered = True
        self.carry1=""
        for i in range(6):
            self.carry1+=str(self.showLst[i])+"  "
        self.showResults.set(self.carry1)

    def showTicket5(self):
        self.showTicket()
        self.carry5=""
        for i in range(4):
            self.runLottery()
            if(self.showLst==self.winingNumbers):
                self.covered=True
            for k in range(6):
                self.carry5+=str(self.showLst[k])+"  "
            self.carry5+="\n"
        self.showResults5.set(self.carry5)

    def drawTickets(self):
        top=Toplevel()
        top.title("Draw Result")
        label1=Label(top, text="The wining numbers are ").pack()
        var1=StringVar()
        label2 = Label(top, textvariable=var1).pack()
        var1.set(self.winingNumbers)
        var2=StringVar()
        Label3 = Label(top, textvariable=var2).pack()
        if (self.covered==True):
            var2.set("You Win!!")
        else:
            var2.set("You Lose!!")


lotteryNum()

Comments

0

Print self.usingLst to make sure it contains something, and

self.showResults=Label(frame, text="no choices made")
self.showResults.pack()

then in runLottery

self.showResults["text"]="\n".join(self.usingLst)

1 Comment

Thank you! I tired a different way and it works. I will post the answer below

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.