In questi giorni mi sto cimentando con wxPython, una splendida libreria grafica per il linguaggio di programmazione python. Ebbene, ho deciso, dopo aver studiato solo le basi, di scrivere un programmino che riconoscesse una password. Al terzo errore, il programma si deve chiudere. Lo script che è venuto fuori è questo:
#!/usr/bin/env python
import wx
PASSWORD = "12345"
class PswdRecognizer(wx.App):
def OnInit(self):
frame = MainFrame("Password Recognizer")
frame.Show(True)
self.SetTopWindow(frame)
return True
class MainFrame(wx.Frame):
def __init__(self, title):
wx.Frame.__init__(self, None, title=title, size=(200, 66))
# Variables
self.errors = 0
# Panel
panel = wx.Panel(self)
# TextCtrl
self.line = wx.TextCtrl(panel, style=wx.TE_PASSWORD, size=(200, 30))
font = wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL, wx.NORMAL, u"Droid Sans")
self.line.SetFont(font)
self.line.SetFocus()
# Buttons
ok = wx.Button(panel, wx.ID_OK, label="OK", pos=(-1, 31))
ok.SetDefault()
self.Bind(wx.EVT_BUTTON, self.OnOK, ok)
def OnOK(self, e):
if self.line.GetValue() == PASSWORD:
self.errors = 0
dlg = wx.MessageDialog(self, "Right!", "Password", wx.OK)
dlg.ShowModal()
dlg.Destroy()
else:
self.errors += 1
if self.errors == 3:
dlg = wx.MessageDialog(self, "Third Error.\nBye!",
"Password", wx.ID_OK)
dlg.ShowModal()
dlg.Destroy()
self.Close()
else:
dlg = wx.MessageDialog(self, "NO!!!!!", "Password", wx.OK)
dlg.ShowModal()
dlg.Destroy()
self.line.SetFocus()
if __name__ == "__main__":
app = PswdRecognizer()
app.MainLoop()
Il programma è molto semplice, e utilizza i concetti di Frame, App, TextCtrl, MessageDialog e Panel. La guida (in inglese cui sto attingendo le informazioni è questa.
Buon wxPyhton!
PASSWORD = "12345"
class PswdRecognizer(wx.App):
def OnInit(self):
frame = MainFrame("Password Recognizer")
frame.Show(True)
self.SetTopWindow(frame)
return True
class MainFrame(wx.Frame):
def __init__(self, title):
wx.Frame.__init__(self, None, title=title, size=(200, 66))
# Variables
self.errors = 0
# Panel
panel = wx.Panel(self)
# TextCtrl
self.line = wx.TextCtrl(panel, style=wx.TE_PASSWORD, size=(200, 30))
font = wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL, wx.NORMAL, u"Droid Sans")
self.line.SetFont(font)
self.line.SetFocus()
# Buttons
ok = wx.Button(panel, wx.ID_OK, label="OK", pos=(-1, 31))
ok.SetDefault()
self.Bind(wx.EVT_BUTTON, self.OnOK, ok)
def OnOK(self, e):
if self.line.GetValue() == PASSWORD:
self.errors = 0
dlg = wx.MessageDialog(self, "Right!", "Password", wx.OK)
dlg.ShowModal()
dlg.Destroy()
else:
self.errors += 1
if self.errors == 3:
dlg = wx.MessageDialog(self, "Third Error.\nBye!",
"Password", wx.ID_OK)
dlg.ShowModal()
dlg.Destroy()
self.Close()
else:
dlg = wx.MessageDialog(self, "NO!!!!!", "Password", wx.OK)
dlg.ShowModal()
dlg.Destroy()
self.line.SetFocus()
if __name__ == "__main__":
app = PswdRecognizer()
app.MainLoop()
Buon wxPyhton!
0 commenti:
Posta un commento