Code: Alles auswählen
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
import random
import string
string_seletion = (string.ascii_uppercase, string.ascii_lowercase, string.digits, string.punctuation)
def generate_password(lenght_password, select_text):
auswahl = ""
for ic, e in zip(select_text,string_seletion):
if ic:
auswahl += e
return "".join(random.choice(auswahl) for _ in range(lenght_password))
class Application(Frame):
def print_value(self, val):
if self.groß.get() + self.klein.get() + self.zahl.get() == 0 + self.sonder.get() == 0:
self.g.select()
self.groß.set(1)
selection = (self.groß.get(), self.klein.get(), self.zahl.get(),self.sonder.get())
text_password = generate_password(int(val), selection)
self.Ausgabe.config(state='normal')
self.Ausgabe.delete(0, 'end')
self.Ausgabe.insert('end', text_password)
def move_scroll(self, *L):
op, howMany = L[0], L[1]
if op == "scroll":
units = L[2]
self.Ausgabe.xview_scroll(howMany, units)
elif op == "moveto":
self.Ausgabe.xview_moveto(howMany)
def createWidgets(self):
#Erstellung des Label Frames
labelfr = LabelFrame(self, text="Bitte gewünschte Zeichen Auswählen", padx=10, pady=10)
labelfr.grid()
#Erstellung der Checkbuttons
self.groß = IntVar()
self.g = Checkbutton(labelfr, text="Großbuchstaben", variable=self.groß).grid(row=3, column=0, padx=5, pady=5)
self.klein = IntVar()
self.k = Checkbutton(labelfr, text="Kleinbuchstaben", variable=self.klein).grid(row=4, column=0, padx=5, pady=5)
self.zahl = IntVar()
self.z = Checkbutton(labelfr, text="Zahlen", variable=self.zahl).grid(row=3, column=1, padx=5, pady=5, sticky=W)
self.sonder = IntVar()
self.s = Checkbutton(labelfr, text="Sonderzeichen", variable=self.sonder).grid(row=4, column=1, padx=5, pady=5)
# Einstellung der Passwortlänge
self.slider = Scale(self, from_=1, to=200, orient='horizontal', length="3i", label="Passwortlänge",
command=self.print_value).grid(row=4, column=0, padx=5, pady=5)
# Ausgabefenster
Ausgabe = Entry(self, width=50, state="disable").grid(row=5, column=0, padx=5, pady=0)
#Scrollleiste der Ausgabe
Ausgabescrollen = Scrollbar(self, orient="horizontal",command = self.move_scroll).grid(row=6, column=0, padx=5, pady=0, sticky= EW )
# quit button
self.Beenden = Button(self, text="Beenden", fg="red", command=quit).grid(row=8, column=0, padx=5, pady=10)
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack(expand="YES")
self.createWidgets()
root = Tk()
root.title("Passwortgenerator")
app = Application(master=root)
app.mainloop()