前言:
Python GUI(用户图形界面)创建可以使用Python自带的Tkinter包,不需要额外装包。下面通过随机数的产生的例子进行初步了解。
源代码
windows系统、python3.7
#!/usr/bin/python
# -*- coding:UTF-8 -*-
#edited by yangli at 2019-09-17
from tkinter import *
import random
window = Tk()
window.title("GET RANDOM NUBER")
window.geometry('350x200')
lbl = Label(window,text = "Enter a number")
lbl.grid(column=0,row=0)
lb = Label(window)
txt = Entry(window,width=10)
txt.grid(column=1,row=0)
def clicked():
number = range(1,int(txt.get()))
lb.configure(text = "The random number is " + str(random.choices(number)))
lb.grid(column=0,row=1)
btn = Button(window,fg="black",bg="green",text="Start",command=clicked)
btn.grid(column=10,row=0)
window.mainloop()
运行结果