因此,我正在尝试创建一个程序,当按下按钮时,该程序会在输入字段中创建一个随机数(使用randint)。但是我不知道该怎么做。
import tkinter
from tkinter import Entry, END, E
from random import randint
root = tkinter.Tk()
root.title('Number Generator')
e = Entry(root, font=("LEMON MILK Bold", 24), width=15, borderwidth=10)
e.grid(row=0, column=0, columnspan=3, padx=50, pady=50)
e.delete(0, END)
e.insert(0, number)
number=print(randint(0, 100))
#Definitions
def button_generate():
print(number)
#Buttons
button_generate = tkinter.Button(root, text="Random number", font=("LEMON MILK Bold", 24), padx=10,
pady=10, command=button_generate)
button_exit = tkinter.Button(root, text="Exit", font=("LEMON MILK Bold", 14), padx=5, pady=5,
command=root.quit)
#Grid
button_generate.grid(row=1, column=0, columnspan=3)
button_exit.grid(row=2, column=2, sticky=E)
root.mainloop()
那么基本上我该如何在输入字段中打印随机数? (由于我的错误代码,目前无法在任何地方打印)
答案 0 :(得分:1)
请检查一下
from tkinter import *
from random import randint
def set_text():
number=randint(0, 100)
e.delete(0,END)
e.insert(0,number)
print(number)
win = Tk()
e = Entry(win,width=10)
e.pack()
b1 = Button(win,text="Gen",command=set_text)
b1.pack()
win.mainloop()