2016-04-15 9 views
2

私はインターネット上で多くのソリューションを探してみましたが、何も見つけることができませんでした。画像のようにキャンバス上のプロットの周りの空白を削除しようとしていますが、これまでのところ成功していません。Pythonのtkinterキャンバスから空白を削除するには?

私が書いた簡単なコードは次のとおりです。

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

コミュニティのメンバーのいずれかが私を提供喜ばせるために、この問題を解決する方法についての任意のアイデアを持っている場合、私は要求しますいくつかのガイダンスで。あなただけ

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

にしてみ使用tk.Canvasで

を:私はFigureCanvasTkAggの事とのビジネスではないんだけど、限り、それはtk.Canvasに基づいて、これは動作するはず

+1

あなたは、画面全体を占めるようにウィンドウのサイズを変更しています。キャンバスは指定したウィンドウのサイズを埋めるのに十分な大きさではありません。したがって余分なスペースは、あなたが言及した「空白」で埋められています。 – RobertR

+0

@RobertR、入力いただきありがとうございます。 'canvas = FigureCanvasTkAgg(f、self)'を使うときにキャンバスのサイズをどのように増やすことができますか? –

+0

'.grid(row = 1、columnspan = 2)'の呼び出しで画面にグリッドを描くときに 'sticky =" nswe "'を呼び出してみてください。したがって、 '.grid(row = 1、columnspan = 2、sticky =" nswe ")'のようになります。それがうまくいくかどうかは完全にはわかりません。より良い解決策は、あなたのウィンドウのサイズを増やさず、tkがあなたのためにそれを自動的にサイズさせることです。この方法では、ウィジェット(キャンバス)のサイズに合わせて、空白がなくなります。それが画面のフルサイズになりたいという理由がない限り、そこにはありますか? – RobertR

答えて

-1

次のパラメータをFigureCanvasTkAggコールに追加します:bd、highlightthickness。

私は助けました!

+0

ホワイトスペースを削除するには、サブプロットの属性を変更する必要があることがわかりました。 –

+1

これは問題ではありません。彼がリンクしている写真を見てください。 – RobertR

+0

申し訳ありませんが、私の間違いです。私はtk.Canvasでgoogleingときに見つけることができるように私の返信を維持しています。 – pinogun

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() 
関連する問題