下面是我的Tkinter GUI,我使用linux终端运行代码。
GUI正在将random.sample
的结果输出到终端,但我希望它将random.sample
的结果输出到按钮上方的output
文本框。
我尝试将def x():
更改为输出而不是打印,我尝试将输出上的文字更改为command=x
,x
和random.sample
。
from Tkinter import *
import random
import webbrowser
url = 'http://www.google.co.uk'
# open browser and display url at line 5
def OpenUrl():
webbrowser.open(url)
# define x
def x():
print(random.sample(range(1, 60),6))
# Make window
window = Tk()
window.title("UK Lotto Killa")
window.geometry("275x275")
# define title, nym, year
l1 = Label(window, text=" UK Lotto Number Generator", font="bold")
l1.grid(row=1, column=1, sticky=W)
l2 = Label(window, text=" bitsnpcs (2018)", fg="grey", font="none 8")
l2.grid(row=13, column=1, sticky=W)
# output box
output = Text(window, width=23, height=2)
output.grid(row=3, column=1, sticky=W)
# spacing
l3 = Label(window, text=" ")
l3.grid(row=2, column=1, sticky=W)
l3 = Label(window, text=" ")
l3.grid(row=4, column=1, sticky=W)
l3 = Label(window, text=" ")
l3.grid(row=0, column=1, sticky=W)
l3 = Label(window, text=" ")
l3.grid(row=10, column=1, sticky=W)
l3 = Label(window, text=" ")
l3.grid(row=6, column=1, sticky=W)
l3 = Label(window, text=" ")
l3.grid(row=12, column=1, sticky=W)
# You have options
l3 = Label(window, text=" You have Options - ")
l3.grid(row=7, column=1, sticky=W)
l3 = Label(window, text=" Donate % of winnings to favorite project.")
l3.grid(row=8, column=1, sticky=W)
l3 = Label(window, text=" Donate stake to favorite project.")
l3.grid(row=9, column=1, sticky=W)
# button to generate numbers
Button(window, text="Generate Numbers", width=16, command = x).grid(row=4, column=1, sticky=W)
# open source donation button
Button(window, text="Donate", width=6, command=OpenUrl).grid(row=11, column=1, sticky=W)
# all code goes above here
# finish making window
window.mainloop()
答案 0 :(得分:0)
将x()函数更新为: -
def x():
sampleList = random.sample(range(1, 60),6)
print(sampleList)
sampleRandomNumberListAsString = ','.join(str(x) for x in sampleList)
output.delete(1.0, END)
output.insert(END, sampleRandomNumberListAsString)
此处输出是对Text()对象的引用,其文本必须显示随机数列表。 sampleRandomNumberListAsString是random()
选择的随机数的String表示形式