如何从python中删除空白';什么是特金特帆布?

如何从python中删除空白';什么是特金特帆布?,python,tkinter,tkinter-canvas,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 FigureCanvasTk

我试着在互联网上搜索了很多解决方案,但都没有找到。如图所示,我试图去除画布上绘图周围的空白,但迄今为止我没有成功

我编写的简单代码是:

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()


我请求社区中的任何成员对如何解决此问题有任何想法,请向我提供一些指导。

我与FigureCanvasTkAgg无关,但只要它基于tk。Canvas,这应该可以:

在tk.Canvas中,您只需使用

canv=tk.Canvas(master=root, bd=0, highlightthickness=0)
尝试向FigureCastKagg调用添加以下参数:bd,highlightthickness


希望我能帮忙

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

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()

您正在调整窗口大小以占据整个屏幕,而画布不够大,无法填充指定的窗口大小。因此,额外的空间将被您提到的“空白”填充。@RobertR,谢谢您的输入。当使用
canvas=FigureCanvasTkAgg(f,self)
时,您能建议我如何增加画布的大小吗?当您通过调用
.grid(row=1,columnspan=2)
将其网格化到屏幕时,请尝试将
sticky=“nswe”
添加到调用中。所以它应该看起来像
.grid(row=1,columnsspan=2,sticky=“nswe”)
。不过,不完全确定这是否有效。更好的解决方案是不增加窗口的大小,让tk自动为您调整窗口的大小。这样,它就适合所包含的小部件(画布)的大小,没有任何空白。除非有什么原因你想让它成为屏幕的全尺寸,是吗?@RobertR,我试着按照你的建议去做,但没能找到解决办法。但是,我发现要删除空白,子地块的属性需要更改。不确定“子地块的属性”是什么意思,但您是否尝试过不调整窗口大小?我的意思是不调用代码底部附近的
GUIplot1.geometry(“%dx%d+0+0”%(w,h))
。该行告诉tk“我希望窗口的大小为此”,并将关闭窗口的自动大小调整,以适合其子窗口的大小。感谢您的回复。我发现要删除空白,需要更改子批次的属性。这不是问题所在。看他链接的图片。对不起,我弄错了。我保留着我的回复,这样人们可以在tk.Canvas上搜索时找到它。@pinogun是的,兄弟!帮帮我!