简体   繁体   English

暂停 for 循环以等待按钮按下

[英]Pause a for loop to wait for a button press

I am trying to build an image classifier and I need to manually assign classifications for a training dataset, so I build a file browser /media window app in tkinter to display images and assign them classifications via a button click.我正在尝试构建一个图像分类器,并且我需要为训练数据集手动分配分类,因此我在 tkinter 中构建了一个文件浏览器/媒体 window 应用程序来显示图像并通过单击按钮为其分配分类。 To iterate over the files, I am using a for loop, but I need it to pause and wait for that input.为了遍历文件,我使用了一个 for 循环,但我需要它暂停并等待该输入。 Here is my code:这是我的代码:

def setKnownData(self):
    training_sample = self.dataset.sample(frac = .1)
    print(training_sample)                                                                                
    for i, row in training_sample.iterrows():                                              
        print(row)                                                                                
        global img                                                                                       
        file = Image.open(row['ID'])                                                            
        resized = file.resize((500,600))                                                            
        img = ImageTk.PhotoImage(resized)                                                  
        self.media_window.create_image(0,0, anchor = 'nw', image = img)                           
        self.event_var.get()                                                                    
        while True:                                              
            if self.event_var.get() == 0:                                                         
                print(self.event_var.get())                                                          
                return                                        
            if self.event_var.get() == 1:                                                      
                training_sample.loc[row]['class'] = 'cartoon'                                            
                break                                                                     
            elif self.event_var.get() ==2:                                                       
                training_sample.loc[row]['class'] = 'photo'                                   
                break
            self.event_var.set(0)
        
def stateSwitch(self, action):
    print('state switching....')
    if action == 'toon':
        print(self.event_var.get())
        self.event_var.set(1)
        print('classification: TOON', self.event_var.get())
    elif action == 'photo':
        self.event_var.set(2)
        print('classification: PHOTO')

I've exausted every combination of IntVar, tkinter, and for loop searches and can't find a viable solution, so I apologize if this is a repeat question.我已经用尽了 IntVar、tkinter 和循环搜索的所有组合,但找不到可行的解决方案,所以如果这是一个重复的问题,我深表歉意。 How can I pause this for loop, wait for a putton press, and then proceed to the next image in the list?如何暂停这个 for 循环,等待按下按钮,然后继续到列表中的下一个图像?

You need to shift your thinking away from pausing the loop.你需要把你的想法从暂停循环上移开。 That's procedural programming, but GUIs are work much better as "event driven programming", where the entire program is just endlessly waiting for an event (like a button press) to happen.那是过程式编程,但 GUI 作为“事件驱动编程”工作得更好,其中整个程序只是无休止地等待事件(如按下按钮)发生。 The means no loops, besides the tkinter mainloop.这意味着没有循环,除了 tkinter 主循环。 And it means making a new function for every event.这意味着为每个事件制作一个新的 function。

def setKnownData(self):
    training_sample = self.dataset.sample(frac = .1)
    print(training_sample)
    self.training_sample = training_sample.iterrows()

def on_button_click(self):                                                      
    i, row = next(self.training_sample)                            
    print(row)                                                                                
    global img                                                                                       
    file = Image.open(row['ID'])                                                            
    resized = file.resize((500,600))                                                            
    img = ImageTk.PhotoImage(resized)                                                  
    self.media_window.create_image(0,0, anchor = 'nw', image = img)                           
    self.event_var.get()                                                                    
    while True:                                              
        if self.event_var.get() == 0:                                                         
            print(self.event_var.get())                                                          
            return                                        
        if self.event_var.get() == 1:                                                      
            training_sample.loc[row]['class'] = 'cartoon'                                            
            break                                                                     
        elif self.event_var.get() ==2:                                                       
            training_sample.loc[row]['class'] = 'photo'                                   
            break
        self.event_var.set(0)

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

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