2016-03-02 130 views
1

我需要一个使用tkinter的程序的帮助。所以我尝试用一​​个按钮循环播放一些图片,但是有两个循环,当我按下一个按钮或者消失或者返回到第一个按钮(这很难解释)。我在问自己,如果我可以在这里使用一些线程,并且我需要帮助,因为我以前从未使用它。Python Tkinter线程

这是代码的一部分:

square1 = Image.open("Square1.jpg") 
    square1r = ImageTk.PhotoImage(square1) 
    self.square1img = Label(self.windowSq, image=square1r) 
    self.square1img.image = square1r 
    self.square1img.place(x=30, y=100) 
    square1 = Image.open("Square1a.jpg") 
    square1r = ImageTk.PhotoImage(square1) 
    self.square1img = Label(self.windowSq, image=square1r) 
    self.square1img.image = square1r 
    self.square1img.place(x=435, y=100) 

    next = Button(self.windowSq, text="Next", font=self.customFont, relief=GROOVE, command=self.next, cursor='hand2') 
    next.place(x=185, y=380) 
    self.num = 0 
    next1 = Button(self.windowSq, text="Next", font=self.customFont, relief=GROOVE, command=self.next1, cursor='hand2') 
    next1.place(x=600, y=380) 
    self.num1 = 0 

def next(self): 

    self.num = self.num + 1 

    if self.num == 1: 

     self.square1img.destroy() 
     square2 = Image.open("Square2.jpg") 
     square2r = ImageTk.PhotoImage(square2) 
     self.square2img = Label(self.windowSq, image=square2r) 
     self.square2img.image = square2r 
     self.square2img.place(x=30, y=100) 

    elif self.num == 2: 

     self.square2img.destroy() 
     square3 = Image.open("Square3.jpg") 
     square3r = ImageTk.PhotoImage(square3) 
     self.square3img = Label(self.windowSq, image=square3r) 
     self.square3img.image = square3r 
     self.square3img.place(x=30, y=100) 

    elif self.num == 3: 

     self.square3img.destroy() 
     square4 = Image.open("Square4.jpg") 
     square4r = ImageTk.PhotoImage(square4) 
     self.square4img = Label(self.windowSq, image=square4r) 
     self.square4img.image = square4r 
     self.square4img.place(x=30, y=100) 

    elif self.num == 4: 

     self.square4img.destroy() 
     square5 = Image.open("Square5.jpg") 
     square5r = ImageTk.PhotoImage(square5) 
     self.square5img = Label(self.windowSq, image=square5r) 
     self.square5img.image = square5r 
     self.square5img.place(x=30, y=100) 

    elif self.num == 5: 
     self.square5img.destroy() 
     square1 = Image.open("Square1.jpg") 
     square1r = ImageTk.PhotoImage(square1) 
     self.square1img = Label(self.windowSq, image=square1r) 
     self.square1img.image = square1r 
     self.square1img.place(x=30, y=100) 
     self.num = 0 
     self.windowSq.after(50000, self.next) 

def next1(self): 

    self.num1 = self.num1 + 1 

    if self.num1 == 1: 

     self.square1img.destroy() 
     square2 = Image.open("Square2a.jpg") 
     square2r = ImageTk.PhotoImage(square2) 
     self.square2img = Label(self.windowSq, image=square2r) 
     self.square2img.image = square2r 
     self.square2img.place(x=435, y=100) 

    elif self.num1 == 2: 

     self.square2img.destroy() 
     square3 = Image.open("Square3a.jpg") 
     square3r = ImageTk.PhotoImage(square3) 
     self.square3img = Label(self.windowSq, image=square3r) 
     self.square3img.image = square3r 
     self.square3img.place(x=435, y=100) 

    elif self.num1 == 3: 

     self.square3img.destroy() 
     square4 = Image.open("Square4a.jpg") 
     square4r = ImageTk.PhotoImage(square4) 
     self.square4img = Label(self.windowSq, image=square4r) 
     self.square4img.image = square4r 
     self.square4img.place(x=435, y=100) 

    elif self.num1 == 4: 

     self.square4img.destroy() 
     square5 = Image.open("Square5a.jpg") 
     square5r = ImageTk.PhotoImage(square5) 
     self.square5img = Label(self.windowSq, image=square5r) 
     self.square5img.image = square5r 
     self.square5img.place(x=435, y=100) 

    elif self.num1 == 5: 
     self.square5img.destroy() 
     square1 = Image.open("Square1a.jpg") 
     square1r = ImageTk.PhotoImage(square1) 
     self.square1img = Label(self.windowSq, image=square1r) 
     self.square1img.image = square1r 
     self.square1img.place(x=435, y=100) 
     self.num1 = 0 
     self.windowSq.after(50000, self.next1) 

整个程序是在一个类(如果你想知道...)

class Window(Frame): 

def __init__(self, master): 

    Frame.__init__(self, master) 
    self.master = master 
    self.master.resizable(0, 0) 
    master.title("Arcade Games") 
    master.geometry("800x600+560+240") 
+0

对不起那里我的意思当我按下按钮1图片的其他周期消失.. – ToucaN

+1

你不需要的线程将照片之间切换。 –

回答

0

你有很多冗余代码以上。这个想法是先将图像存储在一个列表中,然后在列表中循环。您可以使用2个按钮调用2个不同的功能(每个列表一个),2个图像列表和2个计数器,以便在每个列表中保留您的位置,然后根据您需要的列表更改标签上的图像(我使用按钮,并注意只有一个按钮被创建 - 永远不会被破坏,并且每次只更改图像)。或者,您可以使用一个类和两个实例类,将不同的图像列表传递给每个实例。这只是一个简单的概念验证程序。点击按钮/图像进行更改。

import sys 
if sys.version_info[0] < 3: 
    import Tkinter as tk  ## Python 2.x 
else: 
    import tkinter as tk  ## Python 3.x 

class ChangeImage(): 
    def __init__(self, root): 
     self.photos=[] 
     self.load_images() 

     self.image_num=0 
     self.btn = tk.Button(root, image=self.photos[self.image_num], command=self.next_image) 
     self.btn.grid(row=0) 
     tk.Button(root, text="Exit", bg="orange", command=root.quit).grid(row=1) 


    def next_image(self): 
     self.image_num += 1 
     if self.image_num >= len(self.photos): 
      self.image_num=0 

     ## pipe the next image to be displayed to the button 
     self.btn["image"]=self.photos[self.image_num] 

    def load_images(self): 
     """ copy data images to a list that is an instance variable 
     """ 
     ladybug_gif_b64='''\ 
R0lGODlhIAAgALMAAP///wAAADAwMP99Hf8AAP+lAP//AMopUwAAAAAAAAAAAAAAAAAAAAAAAAAA 
AAAAACH5BAAAAAAALAAAAAAgACAAAwTHEMhJq714hp3lDh0GiqH2UWOVAt96pUIsBLKglWg87Dwv 
4xMBj0Asxgg+XKxwLBJrxUGsI5TKnARoVHoLDp5XbNP5cwmNAqwa/aOc13ByrfKOw2UGw1SSxrb+ 
AWIxeXsAaX92UDQ1Nh6BdneMhQIHkHGSjYaVlmt4epmacp19YAKEoJRspKWrjBapWWGqcm1uB5tj 
ok4HZa+3m5wEt5kuhpTAcb+FVL/NzspAfDHPysslMJjEIS0oLygnOMVd0SwcHeDk6errEQA7 
''' 

     grape_gif='''\ 
R0lGODlhIAAgALMAAAAAAAAAgHCAkC6LV76+vvXeswD/ANzc3DLNMubm+v/6zS9PT6Ai8P8A//// 
/////yH5BAEAAAkALAAAAAAgACAAAAS00MlJq7046803AF3ofAYYfh8GIEvpoUZcmtOKAO5rLMva 
0rYVKqX5IEq3XDAZo1GGiOhw5rtJc09cVGo7orYwYtYo3d4+DBxJWuSCAQ30+vNTGcxnOIARj3eT 
YhJDQ3woDGl7foNiKBV7aYeEkHEignKFkk4ciYaImJqbkZ+PjZUjaJOElKanqJyRrJyZgSKkokOs 
NYa2q7mcirC5I5FofsK6hcHHgsSgx4a9yzXK0rrV19gRADs= 
''' 

     house='''R0lGODdhFQAVAPMAAAQ2PESapISCBASCBMTCxPxmNCQiJJya/ISChGRmzPz+/PxmzDQyZDQyZDQy 
ZDQyZCwAAAAAFQAVAAAElJDISau9Vh2WMD0gqHHelJwnsXVloqDd2hrMm8pYYiSHYfMMRm53ULlQ 
HGFFx1MZCciUiVOsPmEkKNVp3UBhJ4Ohy1UxerSgJGZMMBbcBACQlVhRiHvaUsXHgywTdycLdxyB 
gm1vcTyIZW4MeU6NgQEBXEGRcQcIlwQIAwEHoioCAgWmCZ0Iq5+hA6wIpqislgGhthEAOw== 
''' 

     ## could also be a list of files passed to the class 
     for photo in (ladybug_gif_b64, grape_gif, house): 
      self.photos.append(tk.PhotoImage(data=photo)) 

root=tk.Tk() 
CI=ChangeImage(root) 
root.mainloop()