我正在尝试在python中创建一个GUI程序来生成随机的彩票号码。我想有一个菜单栏来选择Powerball或MegaMillion。选择后,会有按钮让用户选择要显示的门票数量。但我无法使该方法有效。我想在单击按钮时显示不同的数字列表行,但它确实显示了任何内容。我还没有完成,因为它没有工作。我是Python和编程的新手,请帮忙!谢谢!
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()
答案 0 :(得分:1)
我认为如果您更改此行,您的代码应该可以使用:
Label(frame, text=self.showResults).pack()
到此:
Label(frame, textvariable=self.showResults).pack()
答案 1 :(得分:1)
完成的代码只是让有兴趣的人知道我在这里尝试做什么。
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()
答案 2 :(得分:0)
打印self.usingLst以确保它包含某些内容,并
self.showResults=Label(frame, text="no choices made")
self.showResults.pack()
然后在runLottery
self.showResults["text"]="\n".join(self.usingLst)