2016-05-04 7 views
1

私はこのコードを持っています。python Tkinterは他のウィジェットと等しくイメージを占めます

class App(object): 
    def __init__(self): 
     self.root = Tk() 
     self.root.attributes('-zoomed', True) 
     self.root.grid_rowconfigure(0, weight=1) 
     self.root.grid_columnconfigure(0, weight=1) 
     self.root.grid_columnconfigure(1, weight=1) 

     f1 = Frame(self.root, bd=1, bg="green") 
     f3 = Frame(self.root, bd=1, bg="blue") 

     self.image = Image.open("default.png") 
     self.photo = ImageTk.PhotoImage(self.image) 
     self.label = Label(image=self.photo) 
     self.label.grid(row=0, column=1, sticky="nsew") 

     f1.grid(row=0, column=0, sticky="nsew") 
     f3.grid(row=1, column=0, columnspan=2, sticky="ew") 

app = App() 
app.root.mainloop() 

と出力はどのように画像が左フレームに均等に占有することができます enter image description here

のですか?ソリューションを提供する前に

+0

しかし、イメージがすでに均等に左(緑)フレームに位置しているので、あなたの問題は何ですか? –

+0

問題は緑色の幅と画像の幅を同じにしたいのです。 –

答えて

0

は、私が強調したいだろういくつかの発言があります

  1. あなたは何のためのフレームf3を使用していませんでした。プログラムを実行しているときに表示されません。だから私は以下の私の解決策でそれを無視します。
  2. メインフレーム 'attributesには、属性-zoomedを設定します。私はあなたがそれを使って達成しようとしている目標を知らない。とにかく、WindowsとUbuntuのようないくつかのDebianディストリビューションだけが動作すると言われるスレッドがたくさんあります。私はこの問題を経験しませんでしたが、私が下で提供する最初のリンクを読むことができるように、それらの引数はプラットフォーム固有のものです。したがって、コードの移植性のために、代わりに-fullscreenを使用することができます。しかしここでも、特にあなたの場合は、GUIのタスクバーにも表示されないので、閉じるボタンをクリックしてプログラムを閉じることはできません。だから私の解答では、winfo_screenheight()winfo_screenwidth()を使ってself.rootをあなたのマシンの画面のサイズに合わせ、geometry()メソッドの中でそれらを呼び出します。

私たちは小さなものにあなたの問題を分割してみましょう:あなたが解決する必要が

  1. 最初の問題は、同じ幅に最初のフレームf1self.labelを設定する方法です?。これは、それぞれの幅オプションを画面の幅の半分に設定するだけです。width = self.root.winfo_screenwidth()/2

  2. これが完了したら、コメントで明らかになった2番目の問題を解決する必要があります。そのためには、イメージにresize()メソッドを適用して、PIL.Image.ANTIALIASのラベルのイメージであるイメージのサイズを渡して、self.labelのサイズに合わせてself.imageのサイズを変更する必要があります。だからここ

全プログラム

は完全なプログラムです:

''' 
Created on May 5, 2016 

@author: billal begueradj 
''' 
import Tkinter as Tk 
import PIL.Image 
import PIL.ImageTk 

class App(object): 
    def __init__(self): 
     self.root = Tk.Tk()  
     self.root.grid_rowconfigure(0, weight=1) 
     self.root.grid_columnconfigure(0, weight=1) 
     self.root.grid_columnconfigure(1, weight=1)   
     # Get width and height of the screen 
     self.h= self.root.winfo_screenheight() 
     self.w= self.root.winfo_screenwidth() 
     # Set the GUI's dimensions to the screen size 
     self.root.geometry(str(self.w) + "x" + str(self.h)) 
     # Set the width of the frame 'f1' to the half of the screen width 
     self.f1 = Tk.Frame(self.root, bd=1, bg="green", width= self.w/2) 
     self.f1.grid(row=0, column=0, sticky="nsew")   
     # Load the image 
     self.image = PIL.Image.open("/home/Begueradj/mahomet_pedophile.jpg")  
     # Resize the image to fit self.label width 
     self.photo = PIL.ImageTk.PhotoImage(self.image) 
     self.label = Tk.Label(self.root, image=self.photo, width= self.w/2) 
     self.label.grid(row=0, column=1, sticky="nsew") 


# Launch the main program  
if __name__ == '__main__': 
    app = App() 
    app.root.mainloop() 

デモ

enter image description here

注意ベーネあり

あなたのコメントを通じて少し不正確です。全体の画像がフレームf1と同じサイズで表示されるようにするか、またはラベルが追加されるラベル(self.label)を表示したいとは限りません。これはあなたが何をしたいのかである場合には、ちょうど前のプログラムでは、この行を無視します:

self.image = self.image.resize((self.w/2, self.h), PIL.Image.ANTIALIAS) 

デモ

出力は、(画像は単にの中心に位置している。このようになります。ラベル):

]

関連する問題