简体   繁体   English

Python Tkinter线程

[英]Python Tkinter threading

I need help with a program which uses tkinter. 我需要使用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). 您可以有2个按钮来调用2个不同的功能(每个列表一个),2个图像列表和2个计数器,以将您的位置保留在每个列表中,然后从所需的任何列表中更改Label上的图像(我使用按钮,请注意,仅创建了一个按钮-从未销毁按钮,每次都只更改了图片)。 Or you can use one class, and 2 instances of the class, passing a different list of images to be loaded to each instance. 或者,您可以使用一个类和该类的2个实例,将不同的图像列表传递给每个实例。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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