<filename>UI.py
import tkinter as tk
import xml.etree.ElementTree as ET
import os

os.chdir(os.path.split(os.sys.argv[0])[0])
APPDATA = 'appdata.xml'


class App(object):

    def __init__(self, master):
        self.frame = tk.Frame(master)
        self.frame.pack()
        self._create_widget()

        # 准备数据
        if not os.path.isfile(APPDATA):
            self._create_appdata()
        self._applyappdata()

    def _create_widget(self):
        '''
        创建组件
        '''
        label1 = tk.Label(self.frame, text='输入要禁止的进程名')
        # 用于输入新的禁止进程名
        self.process_name_input = tk.Entry(
            self.frame, show=None, font=('Arial', 14))
         # 用于添加禁止进程到禁止名单
        self.add_button = tk.Button(
            self.frame, text='add', command=self.add_process)

        label2 = tk.Label(self.frame, text='已禁止的进程')
        # 用于显示已禁止的进程名
        self.forbidden_listbox = tk.Listbox(self.frame)
        self.button_frame = tk.Frame(self.frame)
        # 用于删除在禁止名单中的项目
        self.del_button = tk.Button(
            self.button_frame, text='del', command=self.del_process)
        # 保存键
        self.save_button = tk.Button(
            self.button_frame, text='save', command=self.save_data)
        # 用于清空在禁止名单中的项目
        self.clear_button = tk.Button(
            self.button_frame, text='clear', command=self.clear_process)
        # 退出键
        self.quit_button = tk.Button(
            self.button_frame, text='quit', fg='red', command=self.frame.quit)

        # pack
        label1.grid(row=0, column=0, sticky=tk.W)
        self.process_name_input.grid(row=1, column=0, sticky=tk.W)
        self.process_name_input.focus_set()
        self.add_button.grid(row=1, column=1)
        label2.grid(row=2, column=0, sticky=tk.W)
        self.forbidden_listbox.grid(row=3, column=0, sticky=tk.W)
        self.button_frame.grid(row=3, column=1)
        self.del_button.pack(fill=tk.X)
        self.save_button.pack(fill=tk.X)
        self.clear_button.pack(fill=tk.X)
        self.quit_button.pack(fill=tk.X)

    def add_process(self):
        '''
        添加禁止进程
        '''
        name = self.process_name_input.get()
        if name:
            self.forbidden_listbox.insert(tk.END, name)
            # 写入DOM
            if not self._appdata.find('.//forbidden_list'):
                ET.SubElement(self._appdata.find('.//data'), 'forbidden_list')
            forbidden_item = ET.SubElement(self._appdata.find(
                './/forbidden_list'), 'forbidden_item')
            forbidden_item.text = name
        self.process_name_input.delete(0, tk.END)

    def del_process(self):
        '''
        删除禁止进程
        '''
        name = self.forbidden_listbox.get(tk.ACTIVE)
        self.forbidden_listbox.delete(tk.ACTIVE)
        forbidden_list = self._appdata.find('.//forbidden_list')
        for forbidden_item in forbidden_list:
            if forbidden_item.text == name:
                forbidden_list.remove(forbidden_item)
                break

    def clear_process(self):
        '''
        清空进程
        '''
        msg = tk.Toplevel(self.frame)

        msg.title('Message')
        label = tk.Label(msg, text='确定清空？')
        confirm = tk.Button(msg, text='confirm')
        cancel = tk.Button(msg, text='cancel')
        label.pack()
        confirm.pack(side=tk.LEFT)
        cancel.pack(side=tk.RIGHT)
        msg.mainloop()

    def save_data(self):
        '''
        将数据保存到xml文件中
        '''
        self._appdata.write(APPDATA)

    def _create_appdata(self):
        '''
        不存在数据文件时，创建数据文件
        '''
        f = open(APPDATA, 'w')
        f.write(
            '''
            <?xml version="1.0"?>
                <appdata>
                    <data></data>
                    <configure></configure>
                </appdata>''')
        f.close()

    def _applyappdata(self):
        '''
        从数据文件中获取数据
        包括:
        forbidden_list
        '''
        self._appdata = ET.parse(APPDATA)
        forbidden_list = self._appdata.find('.//forbidden_list')
        if not forbidden_list:
            return
        for forbidden_item in list(forbidden_list):
            self.forbidden_listbox.insert(tk.END, forbidden_item.text)


root = tk.Tk()
root.title('ad_killer')
app = App(root)
root.mainloop()
root.destroy()

<filename>UI.py
import tkinter as tk
import xml.etree.ElementTree as ET
import os

os.chdir(os.path.split(os.sys.argv[0])[0])
APPDATA = 'appdata.xml'


class App(object):

    def __init__(self, master):
        self.frame = tk.Frame(master)
        self.frame.pack()
        self._create_widget()

        # 准备数据
        if not os.path.isfile(APPDATA):
            self._create_appdata()
        self._applyappdata()

    def _create_widget(self):
        '''
        创建组件
        '''
        label1 = tk.Label(self.frame, text='输入要禁止的进程名')
        # 用于输入新的禁止进程名
        self.process_name_input = tk.Entry(
            self.frame, show=None, font=('Arial', 14))
         # 用于添加禁止进程到禁止名单
        self.add_button = tk.Button(
            self.frame, text='add', command=self.add_process)

        label2 = tk.Label(self.frame, text='已禁止的进程')
        # 用于显示已禁止的进程名
        self.forbidden_listbox = tk.Listbox(self.frame)
        self.button_frame = tk.Frame(self.frame)
        # 用于删除在禁止名单中的项目
        self.del_button = tk.Button(
            self.button_frame, text='del', command=self.del_process)
        # 保存键
        self.save_button = tk.Button(
            self.button_frame, text='save', command=self.save_data)
        # 用于清空在禁止名单中的项目
        self.clear_button = tk.Button(
            self.button_frame, text='clear', command=self.clear_process)
        # 退出键
        self.quit_button = tk.Button(
            self.button_frame, text='quit', fg='red', command=self.frame.quit)

        # pack
        label1.grid(row=0, column=0, sticky=tk.W)
        self.process_name_input.grid(row=1, column=0, sticky=tk.W)
        self.process_name_input.focus_set()
        self.add_button.grid(row=1, column=1)
        label2.grid(row=2, column=0, sticky=tk.W)
        self.forbidden_listbox.grid(row=3, column=0, sticky=tk.W)
        self.button_frame.grid(row=3, column=1)
        self.del_button.pack(fill=tk.X)
        self.save_button.pack(fill=tk.X)
        self.clear_button.pack(fill=tk.X)
        self.quit_button.pack(fill=tk.X)

    def add_process(self):
        '''
        添加禁止进程
        '''
        name = self.process_name_input.get()
        if name:
            self.forbidden_listbox.insert(tk.END, name)
            # 写入DOM
            if not self._appdata.find('.//forbidden_list'):
                ET.SubElement(self._appdata.find('.//data'), 'forbidden_list')
            forbidden_item = ET.SubElement(self._appdata.find(
                './/forbidden_list'), 'forbidden_item')
            forbidden_item.text = name
        self.process_name_input.delete(0, tk.END)

    def del_process(self):
        '''
        删除禁止进程
        '''
        name = self.forbidden_listbox.get(tk.ACTIVE)
        self.forbidden_listbox.delete(tk.ACTIVE)
        forbidden_list = self._appdata.find('.//forbidden_list')
        for forbidden_item in forbidden_list:
            if forbidden_item.text == name:
                forbidden_list.remove(forbidden_item)
                break

    def clear_process(self):
        '''
        清空进程
        '''
        msg = tk.Toplevel(self.frame)

        msg.title('Message')
        label = tk.Label(msg, text='确定清空？')
        confirm = tk.Button(msg, text='confirm')
        cancel = tk.Button(msg, text='cancel')
        label.pack()
        confirm.pack(side=tk.LEFT)
        cancel.pack(side=tk.RIGHT)
        msg.mainloop()

    def save_data(self):
        '''
        将数据保存到xml文件中
        '''
        self._appdata.write(APPDATA)

    def _create_appdata(self):
        '''
        不存在数据文件时，创建数据文件
        '''
        f = open(APPDATA, 'w')
        f.write(
            '''
            <?xml version="1.0"?>
                <appdata>
                    <data></data>
                    <configure></configure>
                </appdata>''')
        f.close()

    def _applyappdata(self):
        '''
        从数据文件中获取数据
        包括:
        forbidden_list
        '''
        self._appdata = ET.parse(APPDATA)
        forbidden_list = self._appdata.find('.//forbidden_list')
        if not forbidden_list:
            return
        for forbidden_item in list(forbidden_list):
            self.forbidden_listbox.insert(tk.END, forbidden_item.text)


root = tk.Tk()
root.title('ad_killer')
app = App(root)
root.mainloop()
root.destroy()

# 准备数据 创建组件 # 用于输入新的禁止进程名 # 用于添加禁止进程到禁止名单 # 用于显示已禁止的进程名 # 用于删除在禁止名单中的项目 # 保存键 # 用于清空在禁止名单中的项目 # 退出键 # pack 添加禁止进程 # 写入DOM 删除禁止进程 清空进程 将数据保存到xml文件中 不存在数据文件时，创建数据文件 <?xml version="1.0"?>                 <appdata>                     <data></data>                     <configure></configure>                 </appdata> 从数据文件中获取数据         包括:         forbidden_list