在Tkinter画布上控制嵌入的图形尺寸?

时间:2017-08-17 17:55:39

标签: python-3.x matplotlib tkinter fullscreen

我似乎无法完全控制Tkinter画布上嵌入图形的图形大小。

继承人我想做的事。也许你有另一个建议,而不是使用嵌入式数字。

我试图制作一个简单的脚本来制作一些视觉内容。现在它只是一个随机颜色的下降方块的像素映射。

我的问题是我需要它全屏,我可以为我的生活不知道如何。

我认为主要是关于这段代码:

fig = plt.figure(figsize=(40,40))
im = plt.imshow(top.img) # later use a.set_data(new_data)
plt.tick_params(
    axis='both',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom='off',      # ticks along the bottom edge are off
    top='off',         # ticks along the top edge are off
    left='off',
    right='off',
    labelleft='off',
    labelbottom='off') # labels along the bottom edge are off


# a tk.DrawingArea
canvas = FigureCanvasTkAgg(fig, master=top)
canvas.show()
canvas.get_tk_widget().pack(side=gui.TOP , fill=gui.BOTH, expand=1)

似乎figsize对它的大小有限制。

下面是所有代码:

import matplotlib
import numpy as np
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import figure
import matplotlib.pyplot as plt
import tkinter as gui
from math import floor
import time

class FullScreenApp(object):
    def __init__(self, master, **kwargs):
        self.master=master
        pad=3
        self._geom='200x200+0+0'
        master.geometry("{0}x{1}+0+0".format(
            master.winfo_screenwidth()-pad, master.winfo_screenheight()-pad))
        master.bind('<Escape>',self.toggle_geom)            
    def toggle_geom(self,event):
        geom=self.master.winfo_geometry()
        print(geom,self._geom)
        self.master.geometry(self._geom)
        self._geom=geom

def flashBox(color,oy,ox):
    global j1, j2
    top.img[0+oy:j2+oy,0+ox:j1+ox] = color
    im.set_data(top.img)
    canvas.draw();
    time.sleep(t)
    top.img[0+oy:j2+oy,0+ox:j1+ox] = [0,0,0]
    im.set_data(top.img)
    canvas.draw();
    return top.img

def drawBox(color,oy,ox):
    global j1, j2
    top.img[0+oy:j2+oy,0+ox:j1+ox] = color
    im.set_data(top.img)
    canvas.draw();
    time.sleep(t)
    return top.img

def resetBox(oy,ox):
    global j1, j2
    top.img[0+oy:j2+oy,0+ox:j1+ox] = [0,0,0]
    im.set_data(top.img)
    canvas.draw();
    return top.img

def drawColumn(color,u):
    global gridsize, j1, j2
    for l in range(gridsize):
        im.set_data(flashBox(color,j2*l,j1*u))
        time.sleep(t2)

top = gui.Tk()
t = 0.1
t2 = 0.00001
x = 40
y = 40
gridsize = 10
j1 = floor(x // gridsize)
j2 = floor(y // gridsize)

top.img = np.zeros([y,x,3],dtype=np.uint8)
top.img.fill(0) # or img[:] = 255


fig = plt.figure(figsize=(40,40))
im = plt.imshow(top.img) # later use a.set_data(new_data)
plt.tick_params(
    axis='both',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom='off',      # ticks along the bottom edge are off
    top='off',         # ticks along the top edge are off
    left='off',
    right='off',
    labelleft='off',
    labelbottom='off') # labels along the bottom edge are off


# a tk.DrawingArea
canvas = FigureCanvasTkAgg(fig, master=top)
canvas.show()
canvas.get_tk_widget().pack(side=gui.TOP , fill=gui.BOTH, expand=1)

#app=FullScreenApp(top)

while True:
    for n in range(gridsize):
        top.update()
        p = np.random.randint(0,99)        
        #drawColumn([np.random.random_integers(0,255),np.random.random_integers(0,255),np.random.random_integers(0,255)],np.random.random_integers(0,gridsize-1))
        if p > 10:
            flashBox([np.random.random_integers(0,255),np.random.random_integers(0,255),np.random.random_integers(0,255)],j1*np.random.random_integers(0,gridsize-1),j2*np.random.random_integers(0,gridsize-1))
        else:
            flashBox([0,0,0],0,0)

0 个答案:

没有答案