First login generator finish

This commit is contained in:
Yousri 2022-04-21 16:39:41 +02:00
parent 1246e905d3
commit d807841535
1 changed files with 50 additions and 62 deletions

View File

@ -3192,83 +3192,71 @@ Note that the actual calculation is done inside the window itself.
This is not a good idea because we should separate responsibilities into classes! This is not a good idea because we should separate responsibilities into classes!
```python ```python
import wx from tkinter import *
from tkinter import ttk
import login_generator import login_generator
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_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): top_panel = PanedWindow(self.full_window, bg="red")
menu = wx.Menu() self.login_amount = Text(top_panel, height=5, width=52)
menu.Append(1, "Copy selected items") self.login_amount.place(x=10,y=10)
menu.Bind(wx.EVT_MENU, self.copy_items, id=1) self.complex = BooleanVar()
self.PopupMenu(menu) 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): 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 = self.login_complex.GetValue()
try: try:
amount = int(amount) self.login_amount.insert(END, "Your username is {} and your password is {} \n".format(self.username,self.password))
except:
amount = 1
for i in range(0, amount):
username = login_generator.generate_username()
password = login_generator.generate_password(12, complex)
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()
``` ```
Now let's assume the generate username and password function take some calculation time. Now let's assume the generate username and password function take some calculation time.