class DbGui: db = None current_table = None def __init__(self): # init window self.root = Tk() self.root.title("dbTools") self.root.geometry('960x480') self.left = Treeview(self.root) self.right = Treeview(self.root) self.main_menu = Menu(self.root) self.init_left() self.init_right() self.init_main_menu() self.root.config(menu=self.main_menu) self.root.bind('Button-3', self.popup_menu) def connect_db(self, database=None): if database is None: self.db = GetMysqlTableComments() else: self.db = GetMysqlTableComments(database=database) self.set_left(self.db.get_tables()) def start(self): self.connect_db() self.root.mainloop() self.close() def init_left(self): self.left["columns"] = ("1", "2") self.left["show"] = "headings" self.left.column("1", width=50, anchor='center') self.left.column("2", width=50, anchor='center') self.left.heading("1", text="table") self.left.heading("2", text="comment") self.left.insert('', 'end', values=('1', '2')) self.left.place(relx=0, rely=0, relwidth=0.2, relheight=1) self.left.bind('<ButtonRelease-1>', self.left_click) def right_click(self, event): if self.current_table is None: print('please choose table!') return selection = [] for item in self.right.selection(): item_text = self.right.item(item, "values") selection.append(item_text[0]) self.current_table.set_selects(selection) def left_click(self, event): for item in self.left.selection(): item_text = self.left.item(item, "values") sql_columns = self.db.get_columns(item_text[0]) fields = get_fields_from_sql_columns(sql_columns) self.current_table = Table(item_text, fields) self.set_right(get_columns_from_fields(fields)) break def init_right(self): self.right["columns"] = ("1", "2", "3", "4", "5", "6") self.right["show"] = "headings" self.right.column("1", width=50, anchor='center') self.right.column("2", width=50, anchor='center') self.right.column("3", width=50, anchor='center') self.right.column("4", width=50, anchor='center') self.right.column("5", width=50, anchor='center') self.right.column("6", width=50, anchor='center') self.right.heading("1", text="column") self.right.heading("2", text="key") self.right.heading("3", text="java type") self.right.heading("4", text="sql type") self.right.heading("5", text="max size") self.right.heading("6", text="comment") self.right.place(relx=0.2, rely=0, relwidth=0.8, relheight=1) self.right.bind('<ButtonRelease-1>', self.right_click) def set_left(self, values): replace_tree_view(self.left, values) def set_right(self, values): replace_tree_view(self.right, values) def init_main_menu(self): # 被注释代码为在菜单嵌入子菜单 # menu_run = Menu(self.main_menu) # self.main_menu.add_cascade(label="运行", menu=menu_run) self.main_menu.add_command(label="生成", command=self.generate) # menu_run.add_separator() self.main_menu.add_command(label="退出", command=self.root.destroy) def close(self): if self.db.connected: self.db.close_db() print('---closed---') def generate(self): if self.current_table is None: print('please choose table!') return print('---start generate---') self.current_table.generate() print('---end generate---') def popup_menu(self, event): self.main_menu.post(event.x_root, event.y_root)
ct.pack() # 下拉选择框 """ label: 前面要显示的字 editable: 控制是否可更改 insert(): 给下拉选择框添加选项 """ cb = ComboBox(root, label='Type:', editable=True) for animal in ('dog', 'cat', 'hamster', 'python'): cb.insert(END, animal) cb.pack() # 菜单选项 menubar = Menu(root) root.config(menu=menubar) filemenu = Menu(menubar, tearoff=0) # 给menu添加一个选项 menubar.add_cascade(label='文件', menu=filemenu) # 添加子选项 filemenu.add_command(label='新建...', command=click()) filemenu.add_command(label='打开...', command=click()) filemenu.add_command(label='保存...', command=click()) filemenu.add_command(label='关闭填写...', command=root.quit) # frame相当于一个局部的窗体,可以用来装载其他控件 frame1 = Frame(root) frame1.pack(fill=X) label1 = Label(frame1, text='您的花名:') label1.grid(row=1, column=0)