3

だから、私はPythonを使って画像ギャラリーを作るためのオンラインコースの簡単なプロジェクトに取り組んでいます。問題は、次へ、前へ、終了の 3 つのボタンを作成することです。これまでのところ、終了ボタンは機能し、次は新しい画像をロードしますが、別のウィンドウで、私は Tkinter を使用した Python と GUI プログラミングにまったく慣れていないため、これは初心者向けコースの大きな部分です。

これまでのところ、私のコードは次のようになり、すべてが機能します。しかし、前のボタンと次のボタンを作成する方法について助けが必要です。これまで NEW ステートメントを使用してきましたが、別のウィンドウで開きます。単純に 1 つの画像を表示してから、単純なテキストで次の画像をクリックしたいだけです。

import Image
import ImageTk
import Tkinter

root = Tkinter.Tk();
text = Tkinter.Text(root, width=50, height=15);
myImage = ImageTk.PhotoImage(file='nesta.png');

def new():
wind = Tkinter.Toplevel()
wind.geometry('600x600')
imageFile2 = Image.open("signori.png")
image2 = ImageTk.PhotoImage(imageFile2)
panel2 = Tkinter.Label(wind , image=image2)
panel2.place(relx=0.0, rely=0.0)
wind.mainloop()

master = Tkinter.Tk()
master.geometry('600x600')

B = Tkinter.Button(master, text = 'Previous picture', command = new).pack()

B = Tkinter.Button(master, text = 'Quit', command = quit).pack()

B = Tkinter.Button(master, text = 'Next picture', command = new).pack()

master.mainloop()
4

2 に答える 2

5

画像アイテムを設定して画像を変更:Label['image'] = photoimage_obj

import Image
import ImageTk
import Tkinter

image_list = ['1.jpg', '2.jpg', '5.jpg']
text_list = ['apple', 'bird', 'cat']
current = 0

def move(delta):
    global current, image_list
    if not (0 <= current + delta < len(image_list)):
        tkMessageBox.showinfo('End', 'No more image.')
        return
    current += delta
    image = Image.open(image_list[current])
    photo = ImageTk.PhotoImage(image)
    label['text'] = text_list[current]
    label['image'] = photo
    label.photo = photo


root = Tkinter.Tk()

label = Tkinter.Label(root, compound=Tkinter.TOP)
label.pack()

frame = Tkinter.Frame(root)
frame.pack()

Tkinter.Button(frame, text='Previous picture', command=lambda: move(-1)).pack(side=Tkinter.LEFT)
Tkinter.Button(frame, text='Next picture', command=lambda: move(+1)).pack(side=Tkinter.LEFT)
Tkinter.Button(frame, text='Quit', command=root.quit).pack(side=Tkinter.LEFT)

move(0)

root.mainloop()
于 2013-07-06T17:17:56.700 に答える