第一款:浔川AI翻译v3.0
正式代码:
# -*- coding: utf-8 -*-
import tkinter as tk
import tkinter.messagebox
import pickle
import random
# 窗口
window = tk.Tk()
window.title('AI翻译登录界面')
window.geometry('450x200')
# 画布放置图片
# canvas=tk.Canvas(window,height=300,width=500)
# imagefile=tk.PhotoImage(file='qm.png')
# image=canvas.create_image(0,0,anchor='nw',image=imagefile)
# canvas.pack(side='top')
# 标签 用户名密码
Verification_Code = random.randint(1000, 9999)#设置一个随机的四位数
Verification_Code = str(Verification_Code)#把类型转换为str型
print(type(Verification_Code))
tk.Label(window, text='用户名:').place(x=100, y=30)
tk.Label(window, text='密码:').place(x=100, y=70)
tk.Label(window, text='验证码').place(x=100, y=110)
tk.Label(window, text=Verification_Code).place(x=320, y=110)
# 用户名输入框
var_usr_name = tk.StringVar()
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
entry_usr_name.place(x=160, y=30)
# 密码输入框
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
entry_usr_pwd.place(x=160, y=70)
#验证码输入框
var_usr_yzm = tk.StringVar()
entry_usr_yzm = tk.Entry(window, textvariable=var_usr_yzm)
entry_usr_yzm.place(x=160, y=110)
# 登录函数
def usr_log_in():
# 输入框获取用户名密码
usr_name = var_usr_name.get()
usr_pwd = var_usr_pwd.get()
usr_yzm = var_usr_yzm.get()
#测试类型
print(type(usr_yzm),type(Verification_Code))
# 从本地字典获取用户信息,如果没有则新建本地数据库
try:
with open('usr_info.pickle', 'rb') as usr_file:
usrs_info = pickle.load(usr_file)
except FileNotFoundError:
with open('usr_info.pickle', 'wb') as usr_file:
usrs_info = {'admin': 'admin'}
pickle.dump(usrs_info, usr_file)
# 判断验证码是否正确用户名和密码是否匹配
if usr_yzm == Verification_Code:
if usr_name in usrs_info:
if usr_pwd == usrs_info[usr_name]:
tk.messagebox.showinfo(title='welcome',
message='欢迎您:' + usr_name)
else:
tk.messagebox.showerror(message='密码错误')
# 用户名密码不能为空
elif usr_name == '' or usr_pwd == '':
tk.messagebox.showerror(message='用户名或密码为空')
# 不在数据库中弹出是否注册的框
else:
is_signup = tk.messagebox.askyesno('欢迎', '您还没有注册,是否现在注册')
if is_signup:
usr_sign_up()
elif usr_yzm == '':
tk.messagebox.showerror(message='验证码不能为空')
else:
tk.messagebox.showerror(message='验证码有误!')
# 注册函数
def usr_sign_up():
# 确认注册时的相应函数
def signtowcg():
# 获取输入框内的内容
nn = new_name.get()
np = new_pwd.get()
npf = new_pwd_confirm.get()
# 本地加载已有用户信息,如果没有则已有用户信息为空
try:
with open('usr_info.pickle', 'rb') as usr_file:
exist_usr_info = pickle.load(usr_file)
except FileNotFoundError:
exist_usr_info = {}
# 检查用户名存在、密码为空、密码前后不一致
if nn in exist_usr_info:
tk.messagebox.showerror('错误', '用户名已存在')
elif np == '' or nn == '':
tk.messagebox.showerror('错误', '用户名或密码为空')
elif np != npf:
tk.messagebox.showerror('错误', '密码前后不一致')
# 注册信息没有问题则将用户名密码写入数据库
else:
exist_usr_info[nn] = np
with open('usr_info.pickle', 'wb') as usr_file:
pickle.dump(exist_usr_info, usr_file)
tk.messagebox.showinfo('欢迎', '注册成功')
# 注册成功关闭注册框
window_sign_up.destroy()
# 新建注册界面
window_sign_up = tk.Toplevel(window)
window_sign_up.geometry('350x200')
window_sign_up.title('注册')
# 用户名变量及标签、输入框
new_name = tk.StringVar()
tk.Label(window_sign_up, text='用户名:').place(x=10, y=10)
tk.Entry(window_sign_up, textvariable=new_name).place(x=150, y=10)
# 密码变量及标签、输入框
new_pwd = tk.StringVar()
tk.Label(window_sign_up, text='请输入密码:').place(x=10, y=50)
tk.Entry(window_sign_up, textvariable=new_pwd, show='*').place(x=150, y=50)
# 重复密码变量及标签、输入框
new_pwd_confirm = tk.StringVar()
tk.Label(window_sign_up, text='请再次输入密码:').place(x=10, y=90)
tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*').place(x=150, y=90)
# 确认注册按钮及位置
bt_confirm_sign_up = tk.Button(window_sign_up, text='确认注册',
command=signtowcg)
bt_confirm_sign_up.place(x=150, y=130)
# 退出的函数
def usr_sign_quit():
window.destroy()
# 登录 注册按钮
bt_login = tk.Button(window, text='登录', command=usr_log_in)
bt_login.place(x=140, y=150)
bt_logup = tk.Button(window, text='注册', command=usr_sign_up)
bt_logup.place(x=210, y=150)
bt_logquit = tk.Button(window, text='退出', command=usr_sign_quit)
bt_logquit.place(x=280, y=150)
# 主循环
window.mainloop()
import tkinter as tk
import time
# 创建主窗口
window = tk.Tk()
window.title('AI翻译下载')
window.geometry('630x150')
# 设置下载进度条
tk.Label(window, text='下载进度:', ).place(x=50, y=60)
canvas = tk.Canvas(window, width=465, height=22, bg="white")
canvas.place(x=110, y=60)
# 显示下载进度
def progress():
# 填充进度条
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="green")
x = 500 # 未知变量,可更改
n = 465 / x # 465是矩形填充满的次数
for i in range(x):
n = n + 465 / x
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
time.sleep(0.02) # 控制进度条流动的速度
# 清空进度条
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="white")
x = 500 # 未知变量,可更改
n = 465 / x # 465是矩形填充满的次数
for t in range(x):
n = n + 465 / x
# 以矩形的长度作为变量值更新
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
time.sleep(0) # 时间为0,即飞速清空进度条
btn_download = tk.Button(window, text='开始下载', command=progress)
btn_download.place(x=400, y=105)
window.mainloop()
import tkinter as tk
import time
# 创建主窗口
window = tk.Tk()
window.title('AI翻译安装')
window.geometry('630x150')
# 设置下载进度条
tk.Label(window, text='安装进度:', ).place(x=50, y=60)
canvas = tk.Canvas(window, width=465, height=22, bg="white")
canvas.place(x=110, y=60)
# 显示下载进度
def progress():
# 填充进度条
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="green")
x = 500 # 未知变量,可更改
n = 465 / x # 465是矩形填充满的次数
for i in range(x):
n = n + 465 / x
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
time.sleep(0.02) # 控制进度条流动的速度
# 清空进度条
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="white")
x = 500 # 未知变量,可更改
n = 465 / x # 465是矩形填充满的次数
for t in range(x):
n = n + 465 / x
# 以矩形的长度作为变量值更新
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
time.sleep(0) # 时间为0,即飞速清空进度条
btn_download = tk.Button(window, text='开始安装', command=progress)
btn_download.place(x=400, y=105)
window.mainloop()
import tkinter as tk
import time
from tkinter import *
from tkinter import messagebox
import http.client
import hashlib
import json
import urllib.parse
import random
#AI翻译
# 创建窗口
root = Tk()
# 标题
root.title('浔川AI翻译3.0')
# 窗口大小
root.geometry('370x200')
# 获取屏幕宽
s_with = root.winfo_screenwidth()
# 获取屏幕高度
s_height = root.winfo_screenheight()
# 计算页面打开在屏幕中央的位置
l_x = str(round((s_with - 370) / 2))
l_y = str(round((s_height - 100) / 2))
root.geometry('+' + l_x + '+' + l_y)
# add by 0730
def setRadio1():
root.title("汉译英")
print(v.get())
def setRadio2():
root.title("英译汉")
print(v.get())
#可以通过传入特定参数直接和一个程序变量绑定, 这些参数包括: variable, textvariable, onvalue, offvalue, value.
v = IntVar() #保存一个整型变量, 默认值为0
radio1=Radiobutton(root,text="汉译英",variable=v,value=1,command=setRadio1)
radio1.grid()
# 输入控件
entry = Entry(root, font=('微软雅黑', 15))
# entry.grid(row=0, column=1)
radio2=Radiobutton(root,text="英译汉",variable=v,value=2,command=setRadio2)
radio2.grid()
entry = Entry(root, font=('微软雅黑', 15))
# entry.grid(row=0, column=2)
# 第一列标签
lable = Label(root, text='请输入内容:')
# 定位布局 grid网格式布局 pack包 place位置
lable.grid()
# 输入控件
entry = Entry(root, font=('微软雅黑', 15))
entry.grid(row=2, column=1)
res = StringVar() # 保存一个 string 类型变量, 默认值为""
# 翻译结果标签
lable1 = Label(root, text='翻译结果:')
lable1.grid(row=3, column=0)
# 翻译结果输入框
entry1 = Entry(root, font=('微软雅黑', 15), textvariable=res)
entry1.grid(row=3, column=1)
def baidu_translate():
#获取界面输入的内容
content = entry.get().strip()
# content = content.strip()
# 判断输入翻译的内容是否为空
if content == '':
messagebox.showinfo('提示', '请输入翻译内容')
else:
appid = '20151113000005349'
secretKey = 'osubCEzlGjzvw8qdQc41'
# httpClient = None
myurl = '/api/trans/vip/translate'
q = content
fromLang = 'zh' # 源语言
toLang = 'en' # 翻译后的语言
salt = random.randint(32768, 65536)
sign = appid + q + str(salt) + secretKey
sign = hashlib.md5(sign.encode()).hexdigest()#作为十六进制数据字符串值
if(v.get()==1):
myurl = myurl + '?appid=' + appid + '&q=' + urllib.parse.quote(
q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str(
salt) + '&sign=' + sign
else:
myurl = myurl + '?appid=' + appid + '&q=' + urllib.parse.quote(
q) + '&from=' + toLang + '&to=' + fromLang + '&salt=' + str(
salt) + '&sign=' + sign
try:
httpClient = http.client.HTTPConnection('api.fanyi.baidu.com')
httpClient.request('GET', myurl)
response = httpClient.getresponse()# response是HTTPResponse对象
jsonResponse = response.read().decode("utf-8") # 获得返回的结果,结果为json格式
js = json.loads(jsonResponse) # 将json格式的结果转换字典结构
print(js)
dst = str(js["trans_result"][0]["dst"]) # 取得翻译后的文本结果
print(dst) # 打印结果
# return dst
res.set(dst)
except Exception as e:
print('err:' + str(e))
finally:
if httpClient:
httpClient.close()
# 按钮
button = Button(root, text='翻译', width='10', command=baidu_translate)
# sticky 对齐方式 N S W E 上下左右
button.grid(row=4, column=0, sticky=W)
# 退出按钮 command是点击事件的方法
exit_button = Button(root, text='退出', width='10', command=root.quit)
exit_button.grid(row=4, column=1, sticky=E)
# 显示窗口 消息循环 接收对窗口的所有操作的消息
# 主事件循环
root.mainloop()
第二款:浔川画板v5.1
正式代码:
# -*- coding: utf-8 -*-
import tkinter as tk
import tkinter.messagebox
import pickle
import random
# 窗口
window = tk.Tk()
window.title('画板登录界面')
window.geometry('450x200')
# 画布放置图片
# canvas=tk.Canvas(window,height=300,width=500)
# imagefile=tk.PhotoImage(file='qm.png')
# image=canvas.create_image(0,0,anchor='nw',image=imagefile)
# canvas.pack(side='top')
# 标签 用户名密码
Verification_Code = random.randint(1000, 9999)#设置一个随机的四位数
Verification_Code = str(Verification_Code)#把类型转换为str型
print(type(Verification_Code))
tk.Label(window, text='用户名:').place(x=100, y=30)
tk.Label(window, text='密码:').place(x=100, y=70)
tk.Label(window, text='验证码').place(x=100, y=110)
tk.Label(window, text=Verification_Code).place(x=320, y=110)
# 用户名输入框
var_usr_name = tk.StringVar()
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
entry_usr_name.place(x=160, y=30)
# 密码输入框
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
entry_usr_pwd.place(x=160, y=70)
#验证码输入框
var_usr_yzm = tk.StringVar()
entry_usr_yzm = tk.Entry(window, textvariable=var_usr_yzm)
entry_usr_yzm.place(x=160, y=110)
# 登录函数
def usr_log_in():
# 输入框获取用户名密码
usr_name = var_usr_name.get()
usr_pwd = var_usr_pwd.get()
usr_yzm = var_usr_yzm.get()
#测试类型
print(type(usr_yzm),type(Verification_Code))
# 从本地字典获取用户信息,如果没有则新建本地数据库
try:
with open('usr_info.pickle', 'rb') as usr_file:
usrs_info = pickle.load(usr_file)
except FileNotFoundError:
with open('usr_info.pickle', 'wb') as usr_file:
usrs_info = {'admin': 'admin'}
pickle.dump(usrs_info, usr_file)
# 判断验证码是否正确用户名和密码是否匹配
if usr_yzm == Verification_Code:
if usr_name in usrs_info:
if usr_pwd == usrs_info[usr_name]:
tk.messagebox.showinfo(title='welcome',
message='欢迎您:' + usr_name)
else:
tk.messagebox.showerror(message='密码错误')
# 用户名密码不能为空
elif usr_name == '' or usr_pwd == '':
tk.messagebox.showerror(message='用户名或密码为空')
# 不在数据库中弹出是否注册的框
else:
is_signup = tk.messagebox.askyesno('欢迎', '您还没有注册,是否现在注册')
if is_signup:
usr_sign_up()
elif usr_yzm == '':
tk.messagebox.showerror(message='验证码不能为空')
else:
tk.messagebox.showerror(message='验证码有误!')
# 注册函数
def usr_sign_up():
# 确认注册时的相应函数
def signtowcg():
# 获取输入框内的内容
nn = new_name.get()
np = new_pwd.get()
npf = new_pwd_confirm.get()
# 本地加载已有用户信息,如果没有则已有用户信息为空
try:
with open('usr_info.pickle', 'rb') as usr_file:
exist_usr_info = pickle.load(usr_file)
except FileNotFoundError:
exist_usr_info = {}
# 检查用户名存在、密码为空、密码前后不一致
if nn in exist_usr_info:
tk.messagebox.showerror('错误', '用户名已存在')
elif np == '' or nn == '':
tk.messagebox.showerror('错误', '用户名或密码为空')
elif np != npf:
tk.messagebox.showerror('错误', '密码前后不一致')
# 注册信息没有问题则将用户名密码写入数据库
else:
exist_usr_info[nn] = np
with open('usr_info.pickle', 'wb') as usr_file:
pickle.dump(exist_usr_info, usr_file)
tk.messagebox.showinfo('欢迎', '注册成功')
# 注册成功关闭注册框
window_sign_up.destroy()
# 新建注册界面
window_sign_up = tk.Toplevel(window)
window_sign_up.geometry('350x200')
window_sign_up.title('注册')
# 用户名变量及标签、输入框
new_name = tk.StringVar()
tk.Label(window_sign_up, text='用户名:').place(x=10, y=10)
tk.Entry(window_sign_up, textvariable=new_name).place(x=150, y=10)
# 密码变量及标签、输入框
new_pwd = tk.StringVar()
tk.Label(window_sign_up, text='请输入密码:').place(x=10, y=50)
tk.Entry(window_sign_up, textvariable=new_pwd, show='*').place(x=150, y=50)
# 重复密码变量及标签、输入框
new_pwd_confirm = tk.StringVar()
tk.Label(window_sign_up, text='请再次输入密码:').place(x=10, y=90)
tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*').place(x=150, y=90)
# 确认注册按钮及位置
bt_confirm_sign_up = tk.Button(window_sign_up, text='确认注册',
command=signtowcg)
bt_confirm_sign_up.place(x=150, y=130)
# 退出的函数
def usr_sign_quit():
window.destroy()
# 登录 注册按钮
bt_login = tk.Button(window, text='登录', command=usr_log_in)
bt_login.place(x=140, y=150)
bt_logup = tk.Button(window, text='注册', command=usr_sign_up)
bt_logup.place(x=210, y=150)
bt_logquit = tk.Button(window, text='退出', command=usr_sign_quit)
bt_logquit.place(x=280, y=150)
# 主循环
window.mainloop()
import tkinter as tk
import time
# 创建主窗口
window = tk.Tk()
window.title('画板下载')
window.geometry('630x150')
# 设置下载进度条
tk.Label(window, text='下载进度:', ).place(x=50, y=60)
canvas = tk.Canvas(window, width=465, height=22, bg="white")
canvas.place(x=110, y=60)
# 显示下载进度
def progress():
# 填充进度条
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="green")
x = 500 # 未知变量,可更改
n = 465 / x # 465是矩形填充满的次数
for i in range(x):
n = n + 465 / x
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
time.sleep(0.02) # 控制进度条流动的速度
# 清空进度条
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="white")
x = 500 # 未知变量,可更改
n = 465 / x # 465是矩形填充满的次数
for t in range(x):
n = n + 465 / x
# 以矩形的长度作为变量值更新
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
time.sleep(0) # 时间为0,即飞速清空进度条
btn_download = tk.Button(window, text='开始下载', command=progress)
btn_download.place(x=400, y=105)
window.mainloop()
import tkinter as tk
import time
# 创建主窗口
window = tk.Tk()
window.title('画板安装')
window.geometry('630x150')
# 设置下载进度条
tk.Label(window, text='安装进度:', ).place(x=50, y=60)
canvas = tk.Canvas(window, width=465, height=22, bg="white")
canvas.place(x=110, y=60)
# 显示下载进度
def progress():
# 填充进度条
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="green")
x = 500 # 未知变量,可更改
n = 465 / x # 465是矩形填充满的次数
for i in range(x):
n = n + 465 / x
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
time.sleep(0.02) # 控制进度条流动的速度
# 清空进度条
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="white")
x = 500 # 未知变量,可更改
n = 465 / x # 465是矩形填充满的次数
for t in range(x):
n = n + 465 / x
# 以矩形的长度作为变量值更新
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
time.sleep(0) # 时间为0,即飞速清空进度条
btn_download = tk.Button(window, text='开始安装', command=progress)
btn_download.place(x=400, y=105)
window.mainloop()
"""使用tkinter的Canvas控件制作的画板程序, 支持新建、打开、编辑和保存文档.
A painter that made by tkinter in Python.It supports new, open, edit and save documents."""
import sys,os, pickle,json
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.filedialog as dialog
import tkinter.messagebox as msgbox
from tkinter.colorchooser import askcolor
from copy import deepcopy
try:
from PIL import ImageGrab
except:ImageGrab=None
_ver="1.5"
def _load_icon(window,filename):
#为window加载图标
for availble_path in sys.path+[os.path.split(__file__)[0]]:
try:
window.iconbitmap("%s\%s"%(availble_path,filename))
except tk.TclError:pass
def onerror(err,msg='',parent=None):
#显示错误消息
msgbox.showinfo(type(err).__name__,
msg+str(err),parent=parent)
class ScrolledCanvas(tk.Canvas):
"""可滚动的画布,用法与默认的tkinter.Canvas完全相同.
继承自tkinter.Canvas类"""
def __init__(self,master,**options):
self.frame=tk.Frame(master)
tk.Canvas.__init__(self,self.frame,**options)
self.hscroll=ttk.Scrollbar(self.frame,orient=tk.HORIZONTAL,
command=self.xview)
self.vscroll=ttk.Scrollbar(self.frame,command=self.yview)
self.configure(xscrollcommand=self.hscroll.set,
yscrollcommand=self.vscroll.set)
self._show_widgets()
self.bind("<Configure>",self.adjustscrolls)
self["scrollregion"]=(0,0,self["width"],self["height"])
def _show_widgets(self,repack=False):
if repack:self.pack_forget()
self.vscroll.pack(side=tk.RIGHT,fill=tk.Y)
self.hscroll.pack(side=tk.BOTTOM,fill=tk.X)
tk.Canvas.pack(self,side=tk.BOTTOM,expand=True,fill=tk.BOTH)
def pack(self,**options):
self.frame.pack(**options)
def grid(self,**options):
self.frame.grid(**options)
def place(self,**options):
self.frame.place(**options)
def forget(self):
self.frame.forget()
def adjustscrolls(self,event):
region=self["scrollregion"].split()
width,height=(int(region[2]),
int(region[3]))
if event.width>width:
self.hscroll.pack_forget()
else:self._show_widgets(repack=True)
if event.height>height:
self.vscroll.pack_forget()
else:self._show_widgets(repack=True)
class DictFile(dict):
def __init__(self,filename=None,mode='r',
encoding="utf-8",errors="strict",
error_callback=None):
dict.__init__(self)
self.filename=filename
if not filename:return
with open(filename,mode,encoding=encoding,errors=errors) as f:
while True:
try:
texts=f.readline().split(":")
if not texts[0]:break # 到文件结尾
if len(texts)>1:
self[texts[0]]=eval(texts[1],self)
except Exception as err:
if error_callback:error_callback(err)
else:raise
def save(self,filename=None,error_callback=None):
if not filename:
if not self.filename:return
filename=self.filename
try:
with open(filename,'w',encoding="utf_8") as f:
for key in self.keys():
f.write("{}:{}\n".format(key,repr(self[key])))
except Exception as err:
if error_callback:error_callback(err)
else:raise
def __iter__(self):
return (key for key in dict.keys(self) if not key.startswith("__"))
keys=__iter__
@classmethod
def fromdict(cls,dict,filename=None,*args,**kwargs):
new=cls(*args,**kwargs)
new.filename=filename
for key in dict.keys():
new[key]=dict[key]
return new
class PropertyWindow(tk.Toplevel):
#文档属性窗口
def __init__(self,master):
self.master_=master
self.master=master.master
self.init_window()
def init_window(self):
tk.Toplevel.__init__(self)
self.title("文档属性")
self.withdraw()
_load_icon(self,filename="property.ico")
self.focus_force()
#self.resizable(width=False,height=False)
# 当⽗窗⼝隐藏后,窗⼝也跟随⽗窗⼝隐藏
self.transient(self.master)
self.bind_all("<Key-Return>",self.confirm)
self.bind("<Destroy>",
lambda event:setattr(self.master_,'propertywindow',None))
self.create_widgets()
self.state('normal')
def create_widgets(self):
#创建控件
size=tk.Frame(self)
tk.Label(size,text="宽度(像素):").pack(side=tk.LEFT)
self.width=tk.StringVar(self)
bd=int(self.master_.cv["bd"]) or 2 #画布的边框宽度
self.width.set(self.master_.data.get("width",
self.master_.cv.winfo_width()-bd*2))
ttk.Entry(size,textvariable=self.width,width=10).pack(side=tk.LEFT)
tk.Label(size,text="高度(像素):").pack(side=tk.LEFT)
self.height=tk.StringVar(self)
self.height.set(self.master_.data.get("height",
self.master_.cv.winfo_height()-bd*2))
ttk.Entry(size,textvariable=self.height,width=10).pack(side=tk.LEFT)
size.pack(fill=tk.X,pady=2)
ttk.Separator(self).pack(fill=tk.X,pady=2)
backcolor=tk.Frame(self)
tk.Label(backcolor,text="背景颜色:").pack(side=tk.LEFT)
self.backcolor=tk.StringVar(self)
self.backcolor.set(self.master_.data.get("backcolor",''))
ttk.Entry(backcolor,textvariable=self.backcolor).pack(side=tk.LEFT)
ttk.Button(backcolor,text="...",width=2,
command=lambda:self.backcolor.set(
askcolor(master=self)[1] or self.backcolor.get())).pack(side=tk.LEFT)
backcolor.pack(fill=tk.X,pady=2)
strokecolor=tk.Frame(self)
tk.Label(strokecolor,text="画笔颜色:").pack(side=tk.LEFT)
self.strokecolor=tk.StringVar(self)
self.strokecolor.set(self.master_.data.get("strokecolor",''))
ttk.Entry(strokecolor,textvariable=self.strokecolor).pack(side=tk.LEFT)
ttk.Button(strokecolor,text="...",width=2,
command=lambda:self.strokecolor.set(
askcolor(master=self)[1] or self.strokecolor.get())).pack(side=tk.LEFT)
strokecolor.pack(fill=tk.X,pady=2)
pensize=tk.Frame(self)
tk.Label(pensize,text="画笔粗细(像素): ").pack(side=tk.LEFT)
self.pensize=ttk.Spinbox(pensize,from_=1,to=1000,width=16)
self.pensize.set(self.master_.data.get("pensize",1))
self.pensize.pack(side=tk.LEFT)
pensize.pack(fill=tk.X,pady=2)
self.setdefault=tk.IntVar(self)
ttk.Checkbutton(self,text="设为默认值",variable=self.setdefault).pack(
side=tk.LEFT,pady=2)
buttons=tk.Frame(self)
ttk.Button(buttons,text="确定",width=6,command=self.confirm).pack(
side=tk.LEFT,padx=12)
ttk.Button(buttons,text="取消",width=6,command=self.destroy).pack(
side=tk.LEFT,padx=12)
buttons.pack(side=tk.RIGHT,pady=2)
def confirm(self,event=None):
newproperty={}
newproperty["width"]=int(self.width.get())
newproperty["height"]=int(self.height.get())
newproperty["backcolor"]=self.backcolor.get()
newproperty["strokecolor"]=self.strokecolor.get()
newproperty["pensize"]=int(self.pensize.get())
self.master_.data.update(newproperty)
self.master_.draw()
self.master_.file_changed=True
if self.setdefault.get():
self.master_.config.update(newproperty)
self.master_.config.save()
self.destroy()
class Painter():
instances=[]
TITLE="画板 v"+_ver
CONFIGFILE=os.getenv("userprofile")+"\.painter\config.cfg"
#CONFIG:包含默认的工具栏位置,背景颜色,画笔颜色,画笔粗细
CONFIG={"backcolor":"white","strokecolor":"black","pensize":1}
FILETYPE=("vec矢量图文件 (*.vec)","*.vec")
_FILETYPES=(FILETYPE,("pickle文件 (*.pkl;*.pickle)","*.pkl;*.pickle"),
("所有文件","*.*"))
_SAVE_FILETYPES=(("vec矢量图文件 (*.vec)","*.vec"),
("bmp图像 (*.bmp)","*.bmp"),
("jpeg图像( *.jpg)","*.jpg"),
("gif图像 (*.gif)","*.gif"),
("png图像 (*.png)","*.png"))+_FILETYPES[1:]
def __init__(self,master=None,filename=""):
if master==None:
self.master=tk.Tk()
self.master.title(self.TITLE)
else:self.master=master
self.master.withdraw() # 暂时隐藏窗口,避免创建控件时窗口闪烁
self.master.focus_force()
_load_icon(self.master,filename="paint.ico")
self.master.bind("<Key>",self.onkey)
self.master.protocol("WM_DELETE_WINDOW",self.ask_for_save)
self.file_changed=False
self.propertywindow=None
self.filename=filename
Painter.instances.append(self)
self.load_config()
self.create_toolbar()
self.create_canvas()
self.create_menu()
self.master.wm_deiconify() # wm_deiconfy恢复被隐藏的窗口
if self.filename:
self.openfile(self.filename)
else:
# self.data 存放数据
self.data=DictFile()
self.setdefault()
self.draw()
def load_config(self):
#创建一个配置文件
try:self.config=DictFile(self.CONFIGFILE)
except:
#配置文件不存在时
try:
try:os.mkdir(os.getenv("userprofile")+"\.painter")
except FileExistsError:pass
open(self.CONFIGFILE,'w')
except OSError:
self.config=DictFile.fromdict(self.CONFIG)
else:
self.config=DictFile(self.CONFIGFILE)
self.config.update(self.CONFIG)
def create_canvas(self):
#创建画布
self.cv=ScrolledCanvas(self.master,bg="white",
cursor="pencil",relief=tk.GROOVE)
try:
self.cv["cursor"]="@pencil.cur"
except tk.TclError:self.cv["cursor"]="pencil"
self.cv.pack(side=tk.BOTTOM,expand=True,fill=tk.BOTH)
self.cv.bind("<Button-1>",self.mousedown)
self.cv.bind("<B1-Motion>",self.paint)
self.cv.bind("<B1-ButtonRelease>",self.mouseup)
self.cv.bind("<Button-3>",
lambda event:self.editmenu.post(event.x_root,event.y_root))
def create_menu(self):
#创建菜单
menu=tk.Menu(self.master)
filemenu=tk.Menu(self.master,tearoff=False)
filemenu.add_command(label="新建",
command=self.new,accelerator="Ctrl+N")
filemenu.add_command(label="打开",
command=self.ask_for_open,accelerator="Ctrl+O")
filemenu.add_command(label="保存",
command=self.save,accelerator="Ctrl+S")
filemenu.add_command(label="另存为",command=self.save_as)
filemenu.add_separator()
filemenu.add_command(label="退出",command=self.ask_for_save)
self.editmenu=tk.Menu(self.cv,tearoff=False)
self.editmenu.add_command(label="撤销",state=tk.DISABLED,
command=self.undo)
self.editmenu.add_command(label="清除",command=self.clear)
self.editmenu.add_separator()
self.editmenu.add_command(label="文档属性",
command=self.show_property_window)
helpmenu=tk.Menu(self.master,tearoff=False)
helpmenu.add_command(label="关于",command=self.about)
menu.add_cascade(label="文件",menu=filemenu)
menu.add_cascade(label="编辑",menu=self.editmenu)
menu.add_cascade(label="帮助",menu=helpmenu)
# 显示菜单
self.master.config(menu=menu)
def create_toolbar(self):
#创建工具栏
self.toolbar=tk.Frame(self.master,bg="gray92")
self.toolbar.pack(
side=self.config.get("toolbar",tk.BOTTOM),fill=tk.X)
self.create_toolbutton(self.toolbar)
def create_toolbutton(self,master):
#创建工具栏按钮
self.newbtn=ttk.Button(master,width=4,text="新建",command=self.new)
self.newbtn.pack(side=tk.LEFT)
self.openbtn=ttk.Button(master,width=4,text="打开",command=self.ask_for_open)
self.openbtn.pack(side=tk.LEFT)
self.savebtn=ttk.Button(master,width=4,text="保存",command=self.save)
self.savebtn.pack(side=tk.LEFT)
self.cleanbtn=ttk.Button(master,width=4,text="清除",command=self.clear)
self.cleanbtn.pack(side=tk.LEFT)
self.propertybtn=ttk.Button(master,width=8,text="文档属性",
command=self.show_property_window)
self.propertybtn.pack(side=tk.RIGHT)
# ------------------------Painter最核心的方法------------------------
def mousedown(self,event):
data=self.data["data"]
data.append([])#开始新的一根线
#将新绘制的点坐标附加在最近绘制的线末尾
data[-1].append((event.x,event.y))
def paint(self,event):
data=self.data["data"]
try:
x=data[-1][-1][0];y=data[-1][-1][1]
#在画布上创建新的线条
self.cv.create_line(x,y,event.x,event.y,joinstyle=tk.ROUND,
width=self.data["pensize"],
capstyle=tk.ROUND,fill=self.data["strokecolor"])
data[-1].append((event.x,event.y))
self.file_changed=True
self.editmenu.entryconfig(0,state=tk.NORMAL)
except IndexError:pass
def mouseup(self,event):
try:
if len(self.data["data"][-1])<=1:
del self.data["data"][-1]
except IndexError:pass
def _clearcanvas(self):
#清除画布
self.cv.delete("all")
def draw(self,data=None):
#根据self.data的内容绘制图形
if not data:data=self.data
self._clearcanvas()
self.config_canvas(data)
for line in data["data"]:
args=[]
for dot in line:
args.extend([dot[0],dot[1]])
self.cv.create_line(*args,joinstyle=tk.ROUND,
width=self.data["pensize"],
capstyle=tk.ROUND,fill=self.data["strokecolor"])
def config_canvas(self,data=None):
# 配置画布
if not data:data=self.data
if "width" in data and "height" in data:
self.cv.configure(width=self.data["width"],
height=self.data["height"],
scrollregion=(0,0,self.data["width"],self.data["height"]))
if "backcolor" in self.data:self.cv.config(bg=self.data["backcolor"])
def undo(self):
if self.data["data"]:#如果还能撤销
self._clearcanvas()
self.data["data"].pop()
self.draw()
self.file_changed=True
else:
self.master.bell()
self.editmenu.entryconfig(0,
state=(tk.NORMAL if self.data["data"] else tk.DISABLED))
def clear(self):
if not self.data["data"]:return
if msgbox.askyesno("提示","是否清除?",parent=self.master):
self._clearcanvas()
self.data["data"]=[]
self.file_changed=True
self.editmenu.entryconfig(0,state=tk.DISABLED)
# ------------------------------------------------------------------------
def onkey(self,event):
if event.state in (4,6,12,14,36,38,44,46): # 适应多种按键情况(Num,Caps,Scroll)
if event.keysym=='z':#如果按下Ctrl+Z键
self.undo()
elif event.keysym=='o':#按下Ctrl+O键
self.ask_for_open()
elif event.keysym=='s':#Ctrl+S
self.save()
elif event.keysym=='n': #Ctrl+N
self.new()
def setdefault(self):
# 将self.data,self.config设为默认,或填补其中不存在的键
self.data.setdefault("ver",_ver)
self.data.setdefault("data",[])
conffile=self.config.filename
config=deepcopy(self.CONFIG)
config.update(self.config)
self.config=DictFile.fromdict(config,filename=conffile)
data=deepcopy(self.config)
data.update(self.data)
self.data=DictFile.fromdict(data,filename=self.filename)
@classmethod
def new(cls):
# 新建一个文件(打开另一个画板窗口)
cls()
def ask_for_open(self):
returnval=self.ask_for_save(quit=False)
if returnval==0:return
#弹出打开对话框
filename=dialog.askopenfilename(master=self.master,
filetypes=self._FILETYPES)
if os.path.isfile(filename):
self.filename=filename
self._clearcanvas()
self.openfile(filename)
def openfile(self,filename):
try:
self.filename=filename
if filename.endswith(".pkl") or filename.endswith(".pickle"):
with open(filename,'rb') as f:
obj=pickle.load(f)
if type(obj) is dict:
self.data=DictFile.fromdict(obj)
elif type(obj) in (list,tuple):
if type(obj) is tuple:obj=list(obj)
self.data={"data":obj}
else:
onerror(TypeError("未知数据类型: %r"%type(obj)),
parent=self.master);return
else:
self.data=DictFile(filename,error_callback=onerror)
self.setdefault()
self.draw()
self.master.title("%s - %s" %(self.TITLE,self.filename))
self.editmenu.entryconfig(0, # 撤销
state=(tk.NORMAL if self.data["data"] else tk.DISABLED))
except Exception as err:onerror(err,msg="无法加载图像: "+str(err),
parent=self.master)
def ask_for_save(self,quit=True):
if self.file_changed:
retval=msgbox.askyesnocancel("文件尚未保存",
"是否保存{}的更改?".format(
os.path.split(self.filename)[1] or "当前文件"),
parent=self.master)
if not retval is None:
if retval==True:
if self.save()==0:return 0 #0:cancel
else:return 0
if quit:self.quit()
def save(self,filename=None):
if not filename:
if not self.filename:
filename=dialog.asksaveasfilename(master=self.master,
filetypes=self._SAVE_FILETYPES,defaultextension='.vec')
if not filename:return 0
self.filename=filename
try: # 设置边框宽度
bd=int(self.cv["bd"]) or 2
# 文档大小为画布的大小
self.data["width"]=int(self.cv.winfo_width())-bd*2
self.data["height"]=int(self.cv.winfo_height())-bd*2
except tk.TclError:pass
try:
if filename.endswith(".pkl") or filename.endswith(".pickle"):
with open(filename,'wb') as f:
pickle.dump(dict(self.data),f)
elif filename.endswith((".bmp",".png",".jpg",".gif")):
# 保存为图像
if ImageGrab==None:
msgbox.showinfo(
"无法保存为图像","请使用pip安装PIL库",parent=self.master)
bd=int(self.cv["bd"]) or 2 # 去除图像边框
x1=self.cv.winfo_rootx()+bd
y1=self.cv.winfo_rooty()+bd
x2=x1+self.cv.winfo_width()-bd*2
y2=y1+self.cv.winfo_height()-bd*2
pic = ImageGrab.grab(bbox=(x1,y1,x2,y2))
pic.save(filename)
else:
self.data.save(filename,error_callback=onerror)
self.file_changed=False
self.master.title("%s - %s" %(self.TITLE,self.filename))
except Exception as err:onerror(err,msg="抱歉, 无法保存文件: ",
parent=self.master)
def save_as(self):
filename=dialog.asksaveasfilename(master=self.master,
filetypes=self._SAVE_FILETYPES,defaultextension='.vec')
if filename:
self.save(filename)
self.filename=filename
def show_property_window(self):
if self.propertywindow is not None:
self.propertywindow.focus_force()
else:
self.propertywindow=PropertyWindow(self)
def quit(self):
Painter.instances.remove(self)
self.config.save()
self.master.destroy()
def about(self):
msgbox.showinfo("tkinter Canvas 设计功能齐全的画板程序\n版本",_ver,parent=self.master)
def main():
try:os.chdir(os.path.split(__file__)[0])
except OSError:pass
if len(sys.argv)>1:
for arg in sys.argv[1:]:
root=tk.Tk()
root.title(Painter.TITLE)
window=Painter(root,filename=arg)
else: Painter()
tk.mainloop()
if __name__=="__main__":main()
第三款:浔川AI五子棋v1.4
正式代码:
# -*- coding: utf-8 -*-
import tkinter as tk
import tkinter.messagebox
import pickle
import random
# 窗口
window = tk.Tk()
window.title('AI五子棋登录界面')
window.geometry('450x200')
# 画布放置图片
# canvas=tk.Canvas(window,height=300,width=500)
# imagefile=tk.PhotoImage(file='qm.png')
# image=canvas.create_image(0,0,anchor='nw',image=imagefile)
# canvas.pack(side='top')
# 标签 用户名密码
Verification_Code = random.randint(1000, 9999)#设置一个随机的四位数
Verification_Code = str(Verification_Code)#把类型转换为str型
print(type(Verification_Code))
tk.Label(window, text='用户名:').place(x=100, y=30)
tk.Label(window, text='密码:').place(x=100, y=70)
tk.Label(window, text='验证码').place(x=100, y=110)
tk.Label(window, text=Verification_Code).place(x=320, y=110)
# 用户名输入框
var_usr_name = tk.StringVar()
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
entry_usr_name.place(x=160, y=30)
# 密码输入框
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
entry_usr_pwd.place(x=160, y=70)
#验证码输入框
var_usr_yzm = tk.StringVar()
entry_usr_yzm = tk.Entry(window, textvariable=var_usr_yzm)
entry_usr_yzm.place(x=160, y=110)
# 登录函数
def usr_log_in():
# 输入框获取用户名密码
usr_name = var_usr_name.get()
usr_pwd = var_usr_pwd.get()
usr_yzm = var_usr_yzm.get()
#测试类型
print(type(usr_yzm),type(Verification_Code))
# 从本地字典获取用户信息,如果没有则新建本地数据库
try:
with open('usr_info.pickle', 'rb') as usr_file:
usrs_info = pickle.load(usr_file)
except FileNotFoundError:
with open('usr_info.pickle', 'wb') as usr_file:
usrs_info = {'admin': 'admin'}
pickle.dump(usrs_info, usr_file)
# 判断验证码是否正确用户名和密码是否匹配
if usr_yzm == Verification_Code:
if usr_name in usrs_info:
if usr_pwd == usrs_info[usr_name]:
tk.messagebox.showinfo(title='welcome',
message='欢迎您:' + usr_name)
else:
tk.messagebox.showerror(message='密码错误')
# 用户名密码不能为空
elif usr_name == '' or usr_pwd == '':
tk.messagebox.showerror(message='用户名或密码为空')
# 不在数据库中弹出是否注册的框
else:
is_signup = tk.messagebox.askyesno('欢迎', '您还没有注册,是否现在注册')
if is_signup:
usr_sign_up()
elif usr_yzm == '':
tk.messagebox.showerror(message='验证码不能为空')
else:
tk.messagebox.showerror(message='验证码有误!')
# 注册函数
def usr_sign_up():
# 确认注册时的相应函数
def signtowcg():
# 获取输入框内的内容
nn = new_name.get()
np = new_pwd.get()
npf = new_pwd_confirm.get()
# 本地加载已有用户信息,如果没有则已有用户信息为空
try:
with open('usr_info.pickle', 'rb') as usr_file:
exist_usr_info = pickle.load(usr_file)
except FileNotFoundError:
exist_usr_info = {}
# 检查用户名存在、密码为空、密码前后不一致
if nn in exist_usr_info:
tk.messagebox.showerror('错误', '用户名已存在')
elif np == '' or nn == '':
tk.messagebox.showerror('错误', '用户名或密码为空')
elif np != npf:
tk.messagebox.showerror('错误', '密码前后不一致')
# 注册信息没有问题则将用户名密码写入数据库
else:
exist_usr_info[nn] = np
with open('usr_info.pickle', 'wb') as usr_file:
pickle.dump(exist_usr_info, usr_file)
tk.messagebox.showinfo('欢迎', '注册成功')
# 注册成功关闭注册框
window_sign_up.destroy()
# 新建注册界面
window_sign_up = tk.Toplevel(window)
window_sign_up.geometry('350x200')
window_sign_up.title('注册')
# 用户名变量及标签、输入框
new_name = tk.StringVar()
tk.Label(window_sign_up, text='用户名:').place(x=10, y=10)
tk.Entry(window_sign_up, textvariable=new_name).place(x=150, y=10)
# 密码变量及标签、输入框
new_pwd = tk.StringVar()
tk.Label(window_sign_up, text='请输入密码:').place(x=10, y=50)
tk.Entry(window_sign_up, textvariable=new_pwd, show='*').place(x=150, y=50)
# 重复密码变量及标签、输入框
new_pwd_confirm = tk.StringVar()
tk.Label(window_sign_up, text='请再次输入密码:').place(x=10, y=90)
tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*').place(x=150, y=90)
# 确认注册按钮及位置
bt_confirm_sign_up = tk.Button(window_sign_up, text='确认注册',
command=signtowcg)
bt_confirm_sign_up.place(x=150, y=130)
# 退出的函数
def usr_sign_quit():
window.destroy()
# 登录 注册按钮
bt_login = tk.Button(window, text='登录', command=usr_log_in)
bt_login.place(x=140, y=150)
bt_logup = tk.Button(window, text='注册', command=usr_sign_up)
bt_logup.place(x=210, y=150)
bt_logquit = tk.Button(window, text='退出', command=usr_sign_quit)
bt_logquit.place(x=280, y=150)
# 主循环
window.mainloop()
#下载、安装
import tkinter as tk
import time
# 创建主窗口
window = tk.Tk()
window.title('进度条')
window.geometry('630x150')
# 设置下载进度条
tk.Label(window, text='下载进度:', ).place(x=50, y=60)
canvas = tk.Canvas(window, width=465, height=22, bg="white")
canvas.place(x=110, y=60)
# 显示下载进度
def progress():
# 填充进度条
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="green")
x = 500 # 未知变量,可更改
n = 465 / x # 465是矩形填充满的次数
for i in range(x):
n = n + 465 / x
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
time.sleep(0.02) # 控制进度条流动的速度
# 清空进度条
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="white")
x = 500 # 未知变量,可更改
n = 465 / x # 465是矩形填充满的次数
for t in range(x):
n = n + 465 / x
# 以矩形的长度作为变量值更新
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
time.sleep(0) # 时间为0,即飞速清空进度条
btn_download = tk.Button(window, text='开始下载', command=progress)
btn_download.place(x=400, y=105)
window.mainloop()
import tkinter as tk
import time
# 创建主窗口
window = tk.Tk()
window.title('进度条')
window.geometry('630x150')
# 设置下载进度条
tk.Label(window, text='安装进度:', ).place(x=50, y=60)
canvas = tk.Canvas(window, width=465, height=22, bg="white")
canvas.place(x=110, y=60)
# 显示下载进度
def progress():
# 填充进度条
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="green")
x = 500 # 未知变量,可更改
n = 465 / x # 465是矩形填充满的次数
for i in range(x):
n = n + 465 / x
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
time.sleep(0.02) # 控制进度条流动的速度
# 清空进度条
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="white")
x = 500 # 未知变量,可更改
n = 465 / x # 465是矩形填充满的次数
for t in range(x):
n = n + 465 / x
# 以矩形的长度作为变量值更新
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
time.sleep(0) # 时间为0,即飞速清空进度条
btn_download = tk.Button(window, text='开始安装', command=progress)
btn_download.place(x=400, y=105)
window.mainloop()
#五子棋
from tkinter import *
import tkinter.messagebox # 弹窗库
import numpy as np
root = Tk() #创建窗口
root.title("浔川五子棋") #窗口名字
w1 = Canvas(root, width=600,height=600,background='chocolate')
w1.pack()
for i in range(0, 15):
w1.create_line(i * 40 + 20, 20, i * 40 + 20, 580)
w1.create_line(20, i * 40 + 20, 580, i * 40 + 20)
w1.create_oval(135, 135, 145, 145,fill='black')
w1.create_oval(135, 455, 145, 465,fill='black')
w1.create_oval(465, 135, 455, 145,fill='black')
w1.create_oval(455, 455, 465, 465,fill='black')
w1.create_oval(295, 295, 305, 305,fill='black')
num=0
A=np.full((15,15),0)
B=np.full((15,15),'')
def callback(event):
global num ,A
for j in range (0,15):
for i in range (0,15):
if (event.x - 20 - 40 * i) ** 2 + (event.y - 20 - 40 * j) ** 2 <= 2 * 20 ** 2:
break
if (event.x - 20 - 40 * i) ** 2 + (event.y - 20 - 40 * j) ** 2 <= 2*20 ** 2:
break
if num % 2 == 0 and A[i][j] != 1:
w1.create_oval(40*i+5, 40*j+5, 40*i+35, 40*j+35,fill='black')
A[i][j] = 1
B[i][j] = 'b'
num += 1
if num % 2 != 0 and A[i][j] != 1 :
w1.create_oval(40*i+5, 40*j+5, 40*i+35, 40*j+35,fill='white')
A[i][j] = 1.
B[i][j] = 'w'
num += 1
f = [[-1, 0], [-1, 1], [0, 1], [1, 1]]
for z in range(0, 4):
a, b = f[z][0], f[z][1]
count1, count2 = 0, 0
x, y = i, j
while B[x][y] == B[i][j]:
count1 += 1
if x + a >= 0 and y + b >= 0 and x + a < 15 and y + b < 15 and B[x + a][y + b] == B[i][j]:
[x, y] = np.array([x, y]) + np.array([a, b])
else:
x, y = i, j
break
while B[x][y] == B[i][j]:
count2 += 1
if x - a < 15 and y - b < 15 and x - a >= 0 and y - b >= 0 and B[x - a][y - b] == B[i][j]:
[x, y] = np.array([x, y]) - np.array([a, b])
else:
break
if count1 + count2 == 6:
if B[i][j] == 'b':
tkinter.messagebox.showinfo('提示', '黑棋获胜')
else:
tkinter.messagebox.showinfo('提示', '白棋获胜')
w1.bind("<Button -1>",callback)
w1.pack()
def quit():
root.quit()
u=Button(root,text="退出",width=10,height=1,command=quit,font=('楷体',15))
u.pack()
mainloop()
大好消息:浔川AI助手将于2025年5月26日重磅上线!