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

2024-07-04 13:35:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我在网上找了很多解决办法,但都没找到。我试图删除画布上的空白区域,如图所示,但迄今为止我没有成功。在

我写的简单代码是:

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

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


Tags: fromimportselfmatplotlibstyleusemyrandom
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()

我不想和FigureCanvasTkAgg做生意,但只要它基于tk.帆布这应该是有效的:

在tk.帆布你只要用

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

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

希望我帮了忙!在

相关问题 更多 >

    热门问题