diff --git a/learning_python3.md b/learning_python3.md index 535dd5f..24669c5 100644 --- a/learning_python3.md +++ b/learning_python3.md @@ -3238,7 +3238,11 @@ class MainWindow(Frame): def copy_items(self): global root try: - self.login_amount.insert(END, "Your username is {} and your password is {} \n".format(self.username,self.password)) + Item = self.login_list.item(self.login_list.focus()) + FunctionValue = Item.values() + Array = list(FunctionValue) + Login = Array[2] + self.login_amount.insert(END, "Username : {} \nPassword : {} \n".format(Login[0],Login[1])) except: Msg = Toplevel(root) @@ -3263,85 +3267,77 @@ Now let's assume the generate username and password function take some calculati We'll add in a fake `time.sleep` to simulate. ```python -import wx +from tkinter import * +from tkinter import ttk import login_generator import time -class MainWindow(wx.Dialog): +class MainWindow(Frame): def __init__(self): - wx.Dialog.__init__(self, None, title="Login Generator") - self.full_window = wx.Panel(self) - self.full_window_sizer = wx.BoxSizer(wx.VERTICAL) - self.full_window_sizer.Add(self.create_top_panel(), 0, wx.EXPAND | wx.ALL, 20) - self.full_window_sizer.Add(self.create_bottom_panel(), 0, wx.EXPAND, 0) - self.full_window.SetSizer(self.full_window_sizer) - self.Show() + Frame.__init__(self) + self.full_window = PanedWindow(self,orient=VERTICAL) + self.full_window.pack(fill=BOTH,expand=True) + self.create_top_panel() + self.create_bottom_panel() + self.pack(fill=BOTH, expand=True) def create_bottom_panel(self): - bottom_panel = wx.Panel(self.full_window) - bottom_panel_sizer = wx.BoxSizer(wx.HORIZONTAL) - self.login_list = wx.ListCtrl(bottom_panel, style=wx.LC_REPORT) - self.login_list.Bind(wx.EVT_RIGHT_UP, self.show_popup) - self.login_list.InsertColumn(0, 'username', width=200) - self.login_list.InsertColumn(1, 'password', width=200) - bottom_panel_sizer.Add(self.login_list, 0, wx.EXPAND | wx.ALL, 200) - return bottom_panel + bottom_panel = PanedWindow(self.full_window, bg="green") + bottom_panel.pack(fill=BOTH, side=BOTTOM) + #List + self.login_list = ttk.Treeview(bottom_panel, columns=["username", "password"], show="headings", height=6) + self.login_list.pack() + self.login_list.heading("username", text="Username") + self.login_list.heading("password", text="Password") + self.login_list.bind("", lambda e: self.show_popup()) def create_top_panel(self): - top_panel = wx.Panel(self.full_window) - top_panel_sizer = wx.BoxSizer(wx.HORIZONTAL) - self.login_amount = wx.TextCtrl(top_panel) - self.login_complex = wx.CheckBox(top_panel, label="complex") - self.login_create = wx.Button(top_panel, label="Create") - self.login_create.Bind(wx.EVT_BUTTON, self.add_login) - top_panel_sizer.Add(self.login_amount, 1, wx.EXPAND|wx.ALL,0) - top_panel_sizer.Add(self.login_complex, 1, wx.EXPAND|wx.ALL,0) - top_panel_sizer.Add(self.login_create, 1, wx.EXPAND|wx.ALL,0) - top_panel.SetSizer(top_panel_sizer) - return top_panel - - def show_popup(self, event): - menu = wx.Menu() - menu.Append(1, "Copy selected items") - menu.Bind(wx.EVT_MENU, self.copy_items, id=1) - self.PopupMenu(menu) + top_panel = PanedWindow(self.full_window, bg="red") + self.login_amount = Text(top_panel, height=5, width=52) + self.login_amount.place(x=10,y=10) + self.complex = BooleanVar() + self.complex.set(False) + self.login_complex = Checkbutton(top_panel, text="complex",var=self.complex) + self.login_complex.place(x=10,y=100) + self.login_create = Button(top_panel, text="Create", command=lambda: self.add_login()) + self.login_create.place(x=100,y=100) + top_panel.pack(expand=True, fill=BOTH) - def copy_items(self, event): - selected_items = [] - for i in range(self.login_list.GetItemCount()): - if self.login_list.IsSelected(i): - username = selected_items.append( - self.login_list.GetItem(i, 0).GetText() - ) - password = selected_items.append( - self.login_list.GetItem(i, 1).GetText() - ) - clipdata = wx.TextDataObject() - clipdata.SetText("\n".join(selected_items)) - wx.TheClipboard.Open() - wx.TheClipboard.SetData(clipdata) - wx.TheClipboard.Close() + def show_popup(self): + global root + menu = Menu() + menu.add_command(label="Copy selected items", command=lambda : self.copy_items()) + root.config(menu=menu) - def add_login(self, event): - amount = self.login_amount.GetValue() - complex_bool = self.login_complex.GetValue() + def copy_items(self): + global root try: - amount = int(amount) - except: - amount = 1 - for i in range(0, amount): - username = login_generator.generate_username() - password = login_generator.generate_password(12, complex_bool) + Item = self.login_list.item(self.login_list.focus()) + FunctionValue = Item.values() + Array = list(FunctionValue) + Login = Array[2] + self.login_amount.insert(END, "Username : {} \nPassword : {} \n".format(Login[0],Login[1])) time.sleep(1) - index = self.login_list.InsertItem(0, username) - self.login_list.SetItem(index, 1, password) + + except: + Msg = Toplevel(root) + l1 = Label(Msg, text="You have to take something") + l1.pack(fill=BOTH, expand=True) + + def add_login(self): + self.username = login_generator.generate_username() + self.password = login_generator.generate_password(12,self.complex.get()) + self.login_list.insert('', END,values=[self.username, self.password]) if __name__ == "__main__": - app = wx.App() + root = Tk() + root.title("Login Generator") + root.geometry("500x300") win = MainWindow() - app.MainLoop() + root.mainloop() + ``` A clear separation of responsabilities can be acchieved via an MVC pattern and a login *library*. @@ -3418,7 +3414,7 @@ And now the GUI code nicely split up in a **model**, **controller** and a **view The overhead is quite large but it makes the code a lot more scalable. ```python -import wx +from tkinter import * import login_generator import time import threading