Python:棋盘覆盖问题
程序员文章站
2022-05-22 12:37:51
...
利用Python的tkinter库,实现棋盘覆盖的GUI界面
import tkinter as tk
class chess:
def __init__(self):
window = tk.Tk()
window.title('Chess')
self.mark =0
self.cv = tk.Canvas(window,bg = 'white',width = 522, height = 522)
#创建frame的框架,窗口window为这个框架的父容器
frame = tk.Frame(window)
frame.pack()
#控件设置
self.BoardSet = tk.Button(frame,text = 'SetBoard', command = self.ChessBoardSet)
self.Set = tk.Button(frame,text = 'Start', command = self.SpecialPointSet)
self.entrySize = tk.Entry(frame)
self.entryCd1 = tk.Entry(frame)
self.entryCd2 = tk.Entry(frame)
#控件布局
self.entrySize.grid(row = 500,column = 50)
self.BoardSet.grid(row = 500,column = 60)
self.entryCd1.grid(row = 500,column = 100)
self.entryCd2.grid(row = 500,column = 150)
self.Set.grid(row = 500,column = 200)
window.mainloop()
#设置棋盘
def ChessBoardSet(self):
number = int(self.entrySize.get())
singlelength = 512//number
for i in range(number):
for j in range(number+1):
self.cv.create_rectangle(10+j*singlelength,10+i*singlelength,10+singlelength,10+i*singlelength+singlelength)
self.cv.pack()
#设置特殊点
def SpecialPointSet(self):
number = int(self.entrySize.get())
singlelength = 512//number
prow = int(self.entryCd1.get())
pcolumn = int(self.entryCd2.get())
sprt = self.cv.create_rectangle(10+prow*singlelength,10+pcolumn*singlelength,10+singlelength+prow*singlelength,10+pcolumn*singlelength+singlelength)
self.cv.itemconfig(sprt, fill="black")
self.cv.pack()
self.Arrange(0,0,prow,pcolumn,int(self.entrySize.get()))
def colorfill(self,prow,pcolumn,count):
color = ['#FFB6C1','#A9A9A9','#DC143C','#FFF0F5','#800080','#40E0D0','#4B0082','#F8F8FF','#0000FF','#D2B48C','#87CEEB',
'#A52A2A','#40E0D0','#98FB98','#008000','#9ACD32','#FFFFF0','#FFFF00','#FFD700','#F5DEB3','#FFEFD5','#FF0000',
'#F5F5F5','#C0C0C0','#808080']
select = int((count%10))
number = int(self.entrySize.get())
singlelength = 512//number
sprt = self.cv.create_rectangle(10+prow*singlelength,10+pcolumn*singlelength,10+singlelength+prow*singlelength,10+pcolumn*singlelength+singlelength)
self.cv.itemconfig(sprt, fill=color[select])
self.cv.pack()
#放入骨牌
def Arrange(self,trow,tcolumn,prow,pcolumn,size):
global mark
self.mark +=1
count = self.mark
if size == 1:
return
half = size//2
if prow<trow+half and pcolumn<tcolumn+half:
self.Arrange(trow,tcolumn,prow,pcolumn,half)
else:
self.Arrange(trow,tcolumn,trow+half-1,tcolumn+half-1,half)
self.colorfill(trow+half-1,tcolumn+half-1,count)
if prow<trow+half and pcolumn>=tcolumn+half:
self.Arrange(trow,tcolumn+half,prow,pcolumn,half)
else:
self.Arrange(trow,tcolumn+half,trow+half-1,tcolumn+half,half)
self.colorfill(trow+half-1,tcolumn+half,count)
if prow>=trow+half and pcolumn<tcolumn+half:
self.Arrange(trow+half,tcolumn,prow,pcolumn,half)
else:
self.Arrange(trow+half,tcolumn,trow+half,tcolumn+half-1,half)
self.colorfill(trow+half,tcolumn+half-1,count)
if prow>=trow+half and pcolumn>=tcolumn+half:
self.Arrange(trow+half,tcolumn+half,prow,pcolumn,half)
else:
self.Arrange(trow+half,tcolumn+half,trow+half,tcolumn+half,half)
self.colorfill(trow+half,tcolumn+half,count)
start = chess()
start.__init__()
以下为实验结果图
经过测试,本程序最多只能在规格为256*256的棋盘上完成,当规格为512*512及其以上级别时,程序将会报错。
上一篇: Java基础类库
下一篇: 55_综合练习_画同心圆_和棋盘
推荐阅读
-
python抓取并保存html页面时乱码问题的解决方法
-
完美解决Python matplotlib绘图时汉字显示不正常的问题
-
解决python测试opencv时imread导致的错误问题
-
解决Python plt.savefig 保存图片时一片空白的问题
-
在arcgis使用python脚本进行字段计算时是如何解决中文问题的
-
python学习笔记之调用eval函数出现invalid syntax错误问题
-
浅谈Python 多进程默认不能共享全局变量的问题
-
解决PySide+Python子线程更新UI线程的问题
-
浅谈Python中的全局锁(GIL)问题
-
Python的Flask框架中SQLAlchemy使用时的乱码问题解决