简体   繁体   中英

Python Tkinter threading

I need help with a program which uses tkinter. So I try to use a button to cycle trough some pictures, but there are two cycles and when i press one button either disappears or goes back to the first one (it's kinda hard to explain). I was asking myself if I could use some threading here and I need help since I've never used it before.

This is the part of the code:

    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)

The whole program is in a class (if you are wondering...)

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")

You have a lot of redundant code above. The idea is to first store the images in a list and then cycle through the list. You can have 2 buttons that call 2 different functions (one for each list), 2 lists of images, and 2 counters to keep your place in each list, and then change the image on a Label from whichever list you want (I use a button as below, and note that only one button is created-never destroyed, and just the image is changed each time). Or you can use one class, and 2 instances of the class, passing a different list of images to be loaded to each instance. This is just a simple proof of concept program. Click on the button/image to change it.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM