Python Tkinter线程

时间:2016-03-02 14:10:10

标签: python multithreading tkinter

我需要帮助使用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")

1 个答案:

答案 0 :(得分:0)

上面有很多冗余代码。我们的想法是首先将图像存储在列表中,然后在列表中循环。您可以使用2个按钮调用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()