随机显示,还加了圆圈,这样感觉更好点。

from django.test import TestCase

# Create your tests here.
import random
import json
import time
import tkinter as tk
from tkinter import filedialog
from tkinter import LabelFrame
from tkinter import StringVar
from PIL import Image, ImageTk

# 方块大小
card_size = 120
# 间隙大小
gap_size = 2
# 绘图起点坐标
start_x = 400
start_y = 100
# 7 * 5 方格
x_grid = 7
y_grid = 5

# 打开文件,载入json文件
def open_file():
    xxx_name = filedialog.askopenfilename(title='打开文件',
                                          filetypes=[('json', '*.json'),
                                                     ('All Files', '*')])
    # 更新Label text变量
    var.set(xxx_name)
    with open(xxx_name, 'r') as load_f:
        global card_dict
        card_dict = json.load(load_f)

# 画圆
def draw_circle(cvs, x, y, r, **kwargs):
    return cvs.create_oval(x-r, y-r, x+r, y+r, **kwargs)

# 绘图
def draw_card():
    # 这个打开文件,用的是回调函数,我暂时也不知道如何不用这个全局变量
    global card_dict
    imgs_list = []
    for i in range(y_grid):
        y1 = start_y + i * card_size
        for j in range(x_grid):
            x1 = start_x + j * card_size
            # 解析json里对应的文件名
            image_num = card_dict[str(i+1)][str(j+1)]
            image_path = "images/{}.png".format(image_num[0].upper())
            # 载入图片
            img = Image.open(image_path)
            # 重定义大小
            img = img.resize((card_size-gap_size, card_size-gap_size), Image.BILINEAR)
            # 重定义旋转
            img = img.rotate(image_num[1])
            imgs = ImageTk.PhotoImage(img)
            # 先形成一个大列表,便于后面打乱了显示或成行成列显示
            card_pos = [x1 + gap_size, y1 + gap_size, imgs]
            imgs_list.append(card_pos)
    # 为了效果好,随机显示
    random.shuffle(imgs_list)
    for item in imgs_list:
        # 以nw左上角为基准点, 先大图出现,提示一下重点,加点透明GIF,就完美了
        # img_big = ImageTk.PhotoImage(item[3])
        # cv.create_image((item[0], item[1]), anchor='nw', image=img_big, tag='tmp_resize')
        cv.create_image((item[0], item[1]), anchor='nw', image=item[2])
        draw_circle(cv, item[0] + card_size / 2, item[1] + card_size / 2, 20, width=', outline="green",
                    tag='tmp_circle')
        cv.update()
        # 停一下
        time.sleep(0.5)
        # 删除圆圈,只作动画
        cv.delete('tmp_circle')
        # 不调用update,不会更新画布
        cv.update()
    time.sleep(10)

win = tk.Tk()
win.title('tkinter')
w = win.winfo_screenwidth()
h = win.winfo_screenheight()
win.geometry("%dx%d" % (w, h))
cv = tk.Canvas(win, bg='silver', width=w, height=h)

# 要更新label的text,要用var.set方法才行
var = StringVar()
var.set("...")
lab_fra = LabelFrame(win, height=200, width=300, text='选择文件')
lab_fra.pack(side='top', fill='both', expand=True)
btn_open = tk.Button(lab_fra, text='打开文件', command=open_file)
btn_open.grid(row=0, column=0)
btn_render = tk.Button(lab_fra, text='开始渲染', command=draw_card)
btn_render.grid(row=0, column=1)
text_label = tk.Label(lab_fra, textvariable=var)
text_label.grid(row=0, column=2)

# 画格子, 要算好横纵坐标
for i in range(y_grid):
    y1 = start_y + i * card_size
    for j in range(x_grid):
        x1 = start_x + j * card_size
        cv.create_rectangle(x1, y1, x1 + card_size, y1 + card_size)

cv.pack()
win.mainloop()

tkinter改进了随机显示图片的更多相关文章

  1. php 随机显示图片的函数(实例分享)

    转自:http://www.jbxue.com/article/12695.html 发布:thatboy   来源:Net     [大 中 小] 本文分享一个php实现的随机显示图片的函数,可以将 ...

  2. php 随机显示图片的函数(实例)

    转自:http://www.jbxue.com/article/12695.html   发布:thatboy   来源:Net     [大 中 小] 本文分享一个php实现的随机显示图片的函数,可 ...

  3. JS实现的随机显示图片

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  4. Python3 tkinter基础 Label imag显示图片

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  5. js随机显示图片

    定义一个图片数组,生成一个随机数对应数据索引. window.onload = choosePic; function choosePic() { var myPix = new Array(&quo ...

  6. jq实现随机显示部分图片在页面上(兼容IE5)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  7. PHP生成随机水印图片

    基于PHP的GD图形库,自己生成一张图片.仅限初识GD库,实例学习. 一.需求 网站的布局用到了类似慕课网课程列表的风格,每一个课程是一个banner图,图下面是标题加简介.因为课程的数量较大没有为所 ...

  8. Tensorflow显示图片

    Tensorflow在处理数据时,经常加载图像数据,有的时候是直接读取文件,有的则是读取二进制文件,为了更好的理解Tensorflow数据处理模式,先简单讲解显示图片机制,就能更好掌握是否读取正确了. ...

  9. 随机漂浮图片、右侧上下浮动快捷栏JS

    随机漂浮图片JS: //公共脚本文件 main.js function addEvent(obj,evtType,func,cap){ cap=cap||false; if(obj.addEventL ...

随机推荐

  1. [ ceph ] CEPH 部署完整版(CentOS 7 + luminous)

    1. 前言 拜读了 胖哥的(el7+jewel)完整部署 受益匪浅,目前 CEPH 已经更新到 M 版本,配置方面或多或少都有了变动,本博文就做一个 ceph luminous 版本完整的配置安装. ...

  2. 【Docker学习之四】Docker自定义容器镜像

    环境 docker-ce-19.03.1-3.el7.x86_64 centos 7 一.使用已有容器创建镜像-docker commit1.查看本地已有镜像 [root@node105 ~]# do ...

  3. 快排 PAT 1101

    1101 Quick Sort (25 分)   There is a classical process named partition in the famous quick sort algor ...

  4. Endpoint is unreachable and there is no snapshot available for offline browsing

    docker Portainer配置的时候出现这个错误:Endpoint is unreachable and there is no snapshot available for offline b ...

  5. string字符串成员函数

    string字符串成员函数 string str1="aaa"; char c='c'; str1.assign("ABCAAAAAAABBBBB");//替换 ...

  6. PB 修改datawindow 的背景色

    1.修改标题行的背景色 rgb(235, 235, 235) 2.修改选择行的背景色(即选择行高亮) if(currentrow() = getrow(), rgb(235,235,235), rgb ...

  7. 原!!Spring redis的Scan的坑,慎用!

    线上发现有机器,在发生某块业务大量请求时,后面就没有日志了,查看线程状态,如图1,发现很多线程被阻塞了,查看代码发现,用到了scan,如图2,百度之后,发现该操作不会自动释放redis连接,导致red ...

  8. 使用StringBuilder构建字符串

    使用StringBuilder构建字符串确实可以提高效率,比“+”要高效不少.但使用时也有一些坑: 首先,我们指定一个StringBuilder,并设置其长度. StringBuilder build ...

  9. Unity - 简单实现音频管理系统

    简单实现Unity音频管理脚本,便于长短音频的播放控制和音量控制等,核心方法为对象池的应用. 项目地址:AudioPlay - SouthBegonia 包含方法: 持有所有 AudioSource ...

  10. 《JAVA高并发编程详解》-并发编程有三个至关重要的特性:原子性,有序性,可见性