如何从python的tkinter画布中删除空格?

时间:2016-04-15 00:58:56

标签: python tkinter tkinter-canvas

我尝试在互联网上搜索很多解决方案,但却找不到任何解决方案。我试图在画布上移除绘图周围的空白区域,如图所示,但到目前为止我没有成功。

我写的简单代码是:

import tkinter as tk
import matplotlib
from matplotlib import style
style.use('seaborn-darkgrid')
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,    NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import random

class GUIplot(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        printButton = tk.Button(self, text="Plot1", command=lambda: self.printMessage(canvas,a))
        printButton.grid(row=0, column=0, sticky='w') 
        f = Figure(figsize=(24,12))
        a = f.add_subplot(111, axisbg='r')
        canvas = FigureCanvasTkAgg(f, self)
        canvas.get_tk_widget().grid(row=1, columnspan=2)
        #canvas._tkcanvas.config(bg='blue')
        canvas.show()
        canvas.blit()        

    def printMessage(self,canvas,a):
        a.clear()
        my_random_x = random.sample(range(100),10)
        my_random_y = random.sample(range(100),10)
        a.plot(my_random_x,my_random_y,'*')
        canvas.draw()
        print("Wow")

GUIplot1 = GUIplot()
GUIplot1.title('PlotFunction')
w, h = GUIplot1.winfo_screenwidth(), GUIplot1.winfo_screenheight()
GUIplot1.geometry("%dx%d+0+0" % (w, h))
GUIplot1.mainloop()

Attached Image

我会要求社区中的任何成员知道如何解决这个问题,请给我一些指导。

2 个答案:

答案 0 :(得分:1)

我没有使用FigureCanvasTkAgg,但只要它基于tk.Canvas,这应该可行:

在tk.Canvas中你只需使用

canv=tk.Canvas(master=root, bd=0, highlightthickness=0)

尝试将以下参数添加到FigureCanvasTkAgg调用中:bd,highlightthickness。

希望我帮忙!

答案 1 :(得分:1)

这段代码实现了我的要求。感谢所有回复的人:

import tkinter as tk
import matplotlib
from matplotlib import style
style.use('seaborn-darkgrid')
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,      NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import random

class GUIplot(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        printButton = tk.Button(self, text="Plot1", command=lambda:  self.printMessage(canvas,a))
        printButton.grid(row=0, column=0, sticky='w') 
        f = Figure()
        f.subplots_adjust(left=0.03, bottom=0.07, right=0.98, top=0.97, wspace=0, hspace=0)
        a = f.add_subplot(111, axisbg='r')
        canvas = FigureCanvasTkAgg(f, self)
        canvas.get_tk_widget().grid(row=1, columnspan=2)
        #canvas._tkcanvas.config(bg='blue')
        canvas.show()
        canvas.blit()        

    def printMessage(self,canvas,a):
        a.clear()
        my_random_x = random.sample(range(100),10)
        my_random_y = random.sample(range(100),10)
        a.plot(my_random_x,my_random_y,'*')
        canvas.draw()
        print("Wow")

GUIplot1 = GUIplot()
GUIplot1.title('PlotFunction')
w, h = GUIplot1.winfo_screenwidth(), GUIplot1.winfo_screenheight()
GUIplot1.geometry("%dx%d+0+0" % (w, h))
GUIplot1.mainloop()