我希望在堆叠的tkinter框架中连续显示两张图像,就像幻灯片一样。我听说,我可以用后方法做到这一点,但我该如何使用这种方法呢?对不起,如果我的英语不好。感谢。
# -*- coding: cp1252 -*-
import sys
import Tkinter as tk
from Tkinter import *
from functools import partial
import random
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title("test")
self.state('zoomed')
container= tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames={}
for F in (fenster, mode1):
frame= F(container, self)
self.frames[F]=frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(fenster)
def show_frame(self, c):
frame=self.frames[c]
frame.tkraise()
class fenster(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label=tk.Label(self, text="Das ist die Startseite")
label.pack()
button=tk.Button(self, text="Start",
command=lambda: controller.show_frame(mode1))
button.pack()
class mode1(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label1=tk.Label(self, text=" ")
label1.pack()
def ok(label1):
def do_a():
image1 = PhotoImage(file = 'test.gif')
photo1=Label(image=image1)
photo1.image=image1
photo1.pack()
image2 = PhotoImage(file = 'tet.gif')
photo2=Label(image=image2)
photo2.image=image2
photo2.pack()
def do_b():
print("ngfijnwgfekö")
WORDS={"test1":do_a,
"test2":do_b}
choice=random.choice(list(WORDS))
label1['text']=choice
WORDS[choice]()
button1=tk.Button(self, text='Knopf', command=partial(ok, label1))
button1.pack()
if __name__== "__main__":
app=SampleApp()
app.mainloop()
答案 0 :(得分:1)
不确定要将幻灯片添加到代码的位置,所以这里是一个简单的Tkinter GUI的最小工作示例,使用after
到cycle尽管所有当前目录中的图像found。
from Tkinter import Tk, PhotoImage, Label
from glob import glob
from itertools import cycle
# this method calls itself again after 1000 milliseconds
def show_next():
image.configure(file=next(images))
root.after(1000, show_next)
# cycle though gif images found in working directory
images = cycle(glob("*.gif"))
# build minimal test GUI
root = Tk()
image = PhotoImage()
Label(root, image=image).pack()
# start slide show and GUI main loop
show_next()
root.mainloop()