我正在尝试用 python 创建一个 GUI 程序来生成随机彩票号码。我想要一个菜单栏来选择强力球或 super 百万。选择后,会有按钮让用户选择显示多少张门票。但我无法使该方法发挥作用。我想在单击按钮时显示不同行的数字列表,但它确实显示了任何内容。我还没有完成,因为它不起作用。我是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()
最佳答案
我认为如果您更改此行,您的代码应该可以工作:
Label(frame, text=self.showResults).pack()
对此:
Label(frame, textvariable=self.showResults).pack()
关于python - 如何在 Tkinter 中显示字符串变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35995948/