3

tkinter のコードに画像ファイルを追加しましたが、基本的にはフレーム全体を埋めるので、可能であれば、これを行う方法を示したり説明したりするチュートリアルをお勧めできます....ここで私を見せてもらえない限り.

完全なコードは追加していませんが、python ディレクトリに保存すると、以下のコードでテスト イメージが表示されるはずです。

別の画像を含む新しいフレームを開く「次へ」ボタンを作成したいと思います。

from Tkinter import *

root = Tk()
ButtonImage = PhotoImage(file='test.gif')
testButton = Button(root, image=ButtonImage)
testButton.pack()
root.mainloop()
4

2 に答える 2

0

次のようなことを試すことができます:

from Tkinter import *
from glob import glob

class ImageFrame(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.images = glob("*.gif")
        self.cur = 0
        # label showing the image
        self.image = PhotoImage()
        imagelabel = Label(self, image=self.image)
        imagelabel.grid(row=1, column=1)
        # button cycling through the images
        button = Button(self, text="NEXT", command=self.show_next)
        button.grid(row=2, column=1)
        # layout and show first image
        self.grid()
        self.show_next()

    def show_next(self):
        self.cur = (self.cur + 1) % len(self.images)
        self.image.configure(file=self.images[self.cur])

ImageFrame().mainloop()

いくつかの説明:

  • glob現在のディレクトリ内の特定のパターンに一致するすべてのファイルのリストを取得するために使用されます
  • gridは、Tkinter 用のシンプルですが非常に柔軟なレイアウト マネージャーです ( Tkinter リファレンスを参照) 。
  • ボタンにバインドされているshow_nextメソッドは、画像を循環し、新しい画像をPhotoImageusingにバインドします。configure

その結果、大きな画像とボタンを表示する単純なフレームがgif作成され、現在のディレクトリ内の画像が循環します。

于 2013-03-13T10:20:46.260 に答える