Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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如何为Unittests抑制tkinter mainloop()_Python_Python 3.x_Unit Testing_Tkinter - Fatal编程技术网

Python如何为Unittests抑制tkinter mainloop()

Python如何为Unittests抑制tkinter mainloop(),python,python-3.x,unit-testing,tkinter,Python,Python 3.x,Unit Testing,Tkinter,早上好 我将测试下面的课程 class Sample(object): def __init__(self, dictionary_with_all_button_data, path_to_trunk): self.dictionary_with_all_button_data = dictionary_with_all_button_data self.path_to_trunk = path_to_trunk self.error_main = None

早上好

我将测试下面的课程

class Sample(object):

def __init__(self, dictionary_with_all_button_data, path_to_trunk):
    self.dictionary_with_all_button_data = dictionary_with_all_button_data
    self.path_to_trunk = path_to_trunk
    self.error_main = None
    self.create_mainframe()
    self.create_label()
    self.create_buttons()
    self.start_mainloop()

def create_mainframe(self):
    self.error_main = NewMainFrame()
    self.error_main.get_mainframe().title('Hinweis')
    self.error_main.get_mainframe().geometry(self.get_geometry())

def create_label(self):
    self.note = """        Der Trunk ist noch nicht Commitet, wenn sie fortfahren, 
    erstellen Sie ein Tag das nicht die aktuelle version des Trunks enthält

    Wollen sie einen Automatischen Commit ausführen ? 
    dann drücken sie Ja, 
    drücken Sie Nein um ohne commit fortzufahren, 
    zum Beenden drücken Sie abbrechen"""
    self.hinweis = Label(self.error_main.get_mainframe(), text=self.note, justify=LEFT)
    self.hinweis.place(width=400, height=150)

def create_buttons(self):
    self.yes_button = NewButton(self.error_main.get_mainframe(), 'Ja', 9, 1, 55, 150, '#0A4C82', 'White',
                                ExitButtonEvent().start_Event)
    self.no_button = NewButton(self.error_main.get_mainframe(), 'Nein', 9, 1, 155, 150, '#0A4C82', 'White',
                               OpenFolder(self.path_to_trunk, self.error_main).start_Event)
    self.cancel_button = NewButton(self.error_main.get_mainframe(), 'Abbrechen', 9, 1, 255, 150, '#0A4C82', 'White',
                                   CancelEvent(self.error_main).start_Event)

def start_mainloop(self):
    self.error_main.get_mainframe().mainloop()

def get_geometry(self):
    return '400x200+' + str(self.get_mainframe_horizontal_position()) + '+' + str(
        self.get_mainframe_vertical_position())

def get_mainframe_horizontal_position(self):
    for element in self.dictionary_with_all_button_data['MainFrame']:
        return self.dictionary_with_all_button_data['MainFrame']['mainframe'].get_mainframe().winfo_x()

def get_mainframe_vertical_position(self):
    for element in self.dictionary_with_all_button_data['MainFrame']:
        return self.dictionary_with_all_button_data['MainFrame']['mainframe'].get_mainframe().winfo_y()

现在我将对它进行单元测试,但是我如何在不初始化mainloop()的情况下抑制mainloop()函数来运行测试并通过它,而当mainloop()启动时,它停止测试,当我关闭窗口时,我将成为SystemExit()的一个错误非常简单:从构造函数中删除
start\u mainloop
调用(无论如何都不应该出现的地方)

是的,我以前就有过,但是没有其他方法可以抑制主循环()?当然,你可以monkeypatch类,使这个方法成为一个noop,但是当在构造函数中调用这个函数是一个如此明显的设计缺陷并且无论如何都必须修复时,为什么还要麻烦呢?我还在学习python,如果mainloop()不在构造函数中,我宁愿把它释放出来