网格窗口的编写

from random import choice        #导入choice模块
from tkinter import *  #导入tkinter
root=Tk()
root.geometry("500x200+300+200")   #制作窗口大小
root.title("虚拟窗口")               #窗口名称
root.config(bg="purple")           #背景颜色

for i in range(1,12):
    if i==10:           #数字0
        bnt0 = Button(root, text="0", width=5, bg="red")
        bnt0.grid(row=3, column=0, columnspan=2, sticky=E + W)  # 占据两个格子大小    并且左右贴紧
    elif i==11:          #  字符 “."
        bntl = Button(root, text=".", width=5, bg="white")
        bntl.grid(row=3, column=2, sticky=E + W)
    else:            #数字  1-9
        color1 = ["red", "yellow", "green", "pink", "purple"]
        color2 = choice(color1)                        #随选取一个颜色
        bnt=Button(root,text=str(i),width=5,bg=color2)
        bnt.grid(row=(i-1)//3, column=(i-1)%3)          #  设置每个数的列与行

root.mainloop()

 

你可能感兴趣的:(java,html,javascript)