【问题标题】:How to remove white space from a python's tkinter canvas?如何从 python 的 tkinter 画布中删除空格?
【发布时间】: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()

Attached Image

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

【问题讨论】:

  • 您正在调整窗口大小以占据整个屏幕,而画布不足以填充您指定的窗口大小。因此,额外的空间被您提到的“空白”填充。
  • @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


【解决方案1】:

我不涉及 FigureCanvasTkAgg 的事情,但只要它基于 tk.Canvas 这应该可以工作:

在 tk.Canvas 中你只是使用

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

尝试在 FigureCanvasTkAgg 调用中添加以下参数:bd、highlightthickness。

希望我能帮上忙!

【讨论】:

  • 感谢您的回复。我发现要删除空白,需要更改子图的属性。
  • 这不是问题。看看他链接的图片。
  • 对不起,我的错误。我保留我的回复,以便在 tk.Canvas 上搜索时可以找到它。
  • @pinogun 是的,兄弟!帮助了我!
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2022-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多