【发布时间】:2016-04-15 00:58:56
【问题描述】:
我尝试在互联网上搜索很多解决方案,但找不到任何解决方案。我正在尝试删除画布上绘图周围的空白区域,如图所示,但到目前为止我还没有成功。
我写的简单代码是:
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()
如果社区中的任何成员对如何解决此问题有任何想法,请给我一些指导。
【问题讨论】:
-
您正在调整窗口大小以占据整个屏幕,而画布不足以填充您指定的窗口大小。因此,额外的空间被您提到的“空白”填充。
-
@RobertR,感谢您的意见。你能建议我在使用
canvas = FigureCanvasTkAgg(f, self)时如何增加画布的大小吗? -
当您通过调用
.grid(row=1, columnspan=2)将其网格化到屏幕上时,请尝试将sticky="nswe"添加到调用中。所以它应该看起来像.grid(row=1, columnspan=2, sticky="nswe")。不完全确定这是否会起作用。更好的解决方案是不增加窗口的大小,让 tk 自动为您调整大小。这样它就适合包含的小部件(画布)的大小,没有任何空白。除非出于某种原因您希望它成为屏幕的全尺寸,否则可以吗? -
@RobertR,我尝试按照您的建议进行操作,但未能找到解决方案。但是,我发现要删除空白,需要更改子图的属性。
-
不确定“子图的属性”是什么意思,但您是否尝试过不调整窗口大小?我的意思是不要在代码底部附近调用
GUIplot1.geometry("%dx%d+0+0" % (w, h))。该行告诉 tk “我希望窗口为 this 大小”,并将关闭窗口的自动调整大小以适合其子项的大小。
标签: python tkinter tkinter-canvas