Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用tkinter事件来';继续';或者暂停另一个while循环?_Python_Python 3.x_Tkinter - Fatal编程技术网

Python 如何使用tkinter事件来';继续';或者暂停另一个while循环?

Python 如何使用tkinter事件来';继续';或者暂停另一个while循环?,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我在主线程上运行tkinter GUI,在第二个线程上运行while循环,该循环在n秒的时间内更新要在GUI上显示的标签内容。它本身运行良好。 现在,我希望GUI上有一个>按钮,用于生成循环: 暂停当前迭代,使用上一项调用display方法,完成后继续迭代 中途跳过当前项目,转到下一个项目。 但我找不到如何生成循环中断事件。我不能调用continue或sleep,这取决于按钮生成的某些变量/db条目(如此处),因为中断可能发生在循环中的任何位置(而不是特定的if/else行) 我已经通过co

我在主线程上运行tkinter GUI,在第二个线程上运行while循环,该循环在n秒的时间内更新要在GUI上显示的标签内容。它本身运行良好。 现在,我希望GUI上有一个>按钮,用于生成循环:

  • 暂停当前迭代,使用上一项调用display方法,完成后继续迭代
  • 中途跳过当前项目,转到下一个项目。 但我找不到如何生成循环中断事件。我不能调用continue或sleep,这取决于按钮生成的某些变量/db条目(如此处),因为中断可能发生在循环中的任何位置(而不是特定的if/else行)
我已经通过codemy.com(当然还有谷歌)提到了查看器程序

下面是我试图做的一个片段。()

主线程运行GUI

guiObj = GuiWindow()
thread_list = []
thread = threading.Thread(target=worker, args=(guiObj,))
thread_list.append(thread)
thread.start()
guiObj.run_window_on_loop()
thread.join()
guiObj.quit_window()
线程的辅助方法具有以下特性:

 while len(files_not_viewed) != 0:
    chosen = random.choice(files_not_viewed)
    if is_not_viewed(chosen):
        pictureObj = Picture(chosen)
        # Display the chosen for <timer> seconds
        pictureObj.display(guiObj)
        time.sleep(timer)
        # Change the status of "chosen" to "viewed"
        mark_as_viewed(chosen)
    files_not_viewed = list(set(files_not_viewed) - set([chosen]))

使用tk.BooleanVar()操作变量,并在尝试暂停时跟踪状态,以便在暂停完成后可以继续。

如果没有所有代码,很难判断,但似乎是从另一个线程操作GUI,这通常是不允许的。在担心如何在线程之间进行通信之前,您可能需要对其进行修改。@DavisHerring是的,我有一个线程正在操作GUI。所以这一切都在发生。我可以看到由这个线程控制的新图像。现在我需要GUI中的一个按钮,它能够暂停线程的循环。如果您想查看,代码在这里:/:
class GuiWindow():
def __init__(self):
    self.root = Tk()
    self.screen_width = self.root.winfo_screenwidth()
    self.screen_height = self.root.winfo_screenheight()
    self.image_label = None
    self.image = None
    self.folder_path = None
    self.timer = None

def run_window_on_loop(self):
    button_exit = Button(self.root, text="Exit Program", command=self.root.quit)
    button_exit.grid(row=1, column=1)
    self.root.mainloop()

def quit_window(self):
    self.root.quit()

def resize(self, image_path):
    my_pic = Image.open(image_path)
    pic_height = my_pic.height
    pic_width = my_pic.width
    if my_pic.height > (self.screen_height - 100):
        new_height= self.screen_height - 100
        new_width = int(new_height / pic_height * pic_width)
        pic_height = new_height
        pic_width = new_width

    if pic_width > self.screen_width - 5:
        new_width = self.screen_height - 5
        new_height = int(new_width / pic_width * pic_height)
        pic_height = new_height
        pic_width = new_width

    resized_image = my_pic.resize((pic_width, pic_height), Image.ANTIALIAS)
    return resized_image

def add_image(self, image_path):
    resized_img = self.resize(image_path)
    image_obj = ImageTk.PhotoImage(resized_img)
    image_label = Label(self.root, image=image_obj,
                        height=resized_img.height,
                        width=resized_img.width)
    self.image = image_obj # DO NOT REMOVE - Garbage collector error
    if self.image_label is not None:
        self.remove_image()
    image_label.grid(row=0, column=0, columnspan=3)
    self.image_label = image_label

def remove_image(self):
    self.image_label.grid_forget()