【发布时间】:2018-05-10 21:43:47
【问题描述】:
我想在窗口中按任何特定数字,然后我的提交输出将请求def lotto(anzahl): 并给出如下输出:4,20,40; 如果我输入 3 个外汇我到底错过了什么 anzahl 仍然没有定义?
import Tkinter
window = Tkinter.Tk()
lot = Tkinter.Entry(window)
lot.pack()
anzahl = int(lot.get())
def lotto(anzahl):
for i in range(anzahl):
result_text = random.randint(1,45)
tkMessageBox.showinfo("Result", result_text)
submit = Tkinter.Button(window, text="Submit", command=lambda: lotto(anzahl))
submit.pack()
window.mainloop()
【问题讨论】:
-
anzahl = int(lot.get())在主循环开始之前执行,用户在那个阶段还没有机会在 Entry 小部件中输入任何内容。 -
将
anzahl = int(lot.get())移动到函数lotto()的第一行并删除参数。 -
如果这是为了生成乐透号码,您不需要循环,因为可以选择已经选择的号码。顺便说一句,该循环不断覆盖
result_text,因此只保存最后一个值。但正如我所说,您不需要循环,而是应该使用random.sample。另外,MessageBox 的消息参数必须是字符串,而不是数字。