The last is not finish (watch thread doc)

This commit is contained in:
Yousri 2022-04-22 16:20:59 +02:00
parent d807841535
commit 7a88f6a139
1 changed files with 60 additions and 64 deletions

View File

@ -3238,7 +3238,11 @@ class MainWindow(Frame):
def copy_items(self): def copy_items(self):
global root global root
try: 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: except:
Msg = Toplevel(root) 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. We'll add in a fake `time.sleep` to simulate.
```python ```python
import wx from tkinter import *
from tkinter import ttk
import login_generator import login_generator
import time import time
class MainWindow(wx.Dialog): class MainWindow(Frame):
def __init__(self): def __init__(self):
wx.Dialog.__init__(self, None, title="Login Generator") Frame.__init__(self)
self.full_window = wx.Panel(self) self.full_window = PanedWindow(self,orient=VERTICAL)
self.full_window_sizer = wx.BoxSizer(wx.VERTICAL) self.full_window.pack(fill=BOTH,expand=True)
self.full_window_sizer.Add(self.create_top_panel(), 0, wx.EXPAND | wx.ALL, 20) self.create_top_panel()
self.full_window_sizer.Add(self.create_bottom_panel(), 0, wx.EXPAND, 0) self.create_bottom_panel()
self.full_window.SetSizer(self.full_window_sizer) self.pack(fill=BOTH, expand=True)
self.Show()
def create_bottom_panel(self): def create_bottom_panel(self):
bottom_panel = wx.Panel(self.full_window) bottom_panel = PanedWindow(self.full_window, bg="green")
bottom_panel_sizer = wx.BoxSizer(wx.HORIZONTAL) bottom_panel.pack(fill=BOTH, side=BOTTOM)
self.login_list = wx.ListCtrl(bottom_panel, style=wx.LC_REPORT) #List
self.login_list.Bind(wx.EVT_RIGHT_UP, self.show_popup) self.login_list = ttk.Treeview(bottom_panel, columns=["username", "password"], show="headings", height=6)
self.login_list.InsertColumn(0, 'username', width=200) self.login_list.pack()
self.login_list.InsertColumn(1, 'password', width=200) self.login_list.heading("username", text="Username")
bottom_panel_sizer.Add(self.login_list, 0, wx.EXPAND | wx.ALL, 200) self.login_list.heading("password", text="Password")
return bottom_panel self.login_list.bind("<ButtonRelease-1>", lambda e: self.show_popup())
def create_top_panel(self): def create_top_panel(self):
top_panel = wx.Panel(self.full_window) top_panel = PanedWindow(self.full_window, bg="red")
top_panel_sizer = wx.BoxSizer(wx.HORIZONTAL) self.login_amount = Text(top_panel, height=5, width=52)
self.login_amount = wx.TextCtrl(top_panel) self.login_amount.place(x=10,y=10)
self.login_complex = wx.CheckBox(top_panel, label="complex") self.complex = BooleanVar()
self.login_create = wx.Button(top_panel, label="Create") self.complex.set(False)
self.login_create.Bind(wx.EVT_BUTTON, self.add_login) self.login_complex = Checkbutton(top_panel, text="complex",var=self.complex)
top_panel_sizer.Add(self.login_amount, 1, wx.EXPAND|wx.ALL,0) self.login_complex.place(x=10,y=100)
top_panel_sizer.Add(self.login_complex, 1, wx.EXPAND|wx.ALL,0) self.login_create = Button(top_panel, text="Create", command=lambda: self.add_login())
top_panel_sizer.Add(self.login_create, 1, wx.EXPAND|wx.ALL,0) self.login_create.place(x=100,y=100)
top_panel.SetSizer(top_panel_sizer) top_panel.pack(expand=True, fill=BOTH)
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)
def copy_items(self, event): def show_popup(self):
selected_items = [] global root
for i in range(self.login_list.GetItemCount()): menu = Menu()
if self.login_list.IsSelected(i): menu.add_command(label="Copy selected items", command=lambda : self.copy_items())
username = selected_items.append( root.config(menu=menu)
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 add_login(self, event): def copy_items(self):
amount = self.login_amount.GetValue() global root
complex_bool = self.login_complex.GetValue()
try: try:
amount = int(amount) Item = self.login_list.item(self.login_list.focus())
except: FunctionValue = Item.values()
amount = 1 Array = list(FunctionValue)
for i in range(0, amount): Login = Array[2]
username = login_generator.generate_username() self.login_amount.insert(END, "Username : {} \nPassword : {} \n".format(Login[0],Login[1]))
password = login_generator.generate_password(12, complex_bool)
time.sleep(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__": if __name__ == "__main__":
app = wx.App() root = Tk()
root.title("Login Generator")
root.geometry("500x300")
win = MainWindow() win = MainWindow()
app.MainLoop() root.mainloop()
``` ```
A clear separation of responsabilities can be acchieved via an MVC pattern and a login *library*. 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. The overhead is quite large but it makes the code a lot more scalable.
```python ```python
import wx from tkinter import *
import login_generator import login_generator
import time import time
import threading import threading