Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Parole - applicazione.py

applicazione.py

Caricato da: Poggi Marco
Scarica il programma completo

  1. import tkinter
  2. import random
  3.  
  4. class App(tkinter.Frame):
  5.  
  6.     def __init__(self, radice):
  7.         """Costruttore"""
  8.         super().__init__(radice)
  9.         self.radice=radice
  10.         self.pack()
  11.         fLogico=open('italiano.txt', 'r')
  12.         self.elencoParole=fLogico.readlines()
  13.         fLogico.close()
  14.         self.inizializzaComponenti()
  15.  
  16.     def inizializzaComponenti(self):
  17.         """Crea i componenti nella finestra"""
  18.         mioFont='courier 12'
  19.         #lab1
  20.         self.lab1=tkinter.Label(text='Quante parole ? ',height=1, width=18, font= mioFont, justify='left')
  21.         self.lab1.place(x=5, y=12)
  22.         #textQuanteParole
  23.         self.textQuanteParole=tkinter.Text(width=6, height=1, font=mioFont)
  24.         self.textQuanteParole.place(x=180, y=12)
  25.         self.textQuanteParole.insert('1.0', '1')
  26.         #btnGenera
  27.         self.btnGenera=tkinter.Button(text='Genera', width=8, height=1, font=mioFont, command=self.generaParole)
  28.         self.btnGenera.place(x=280, y=10)
  29.         #labParole
  30.         self.labParole=tkinter.Label(text='', font=mioFont, justify='left')
  31.         self.labParole.place(x=5, y=45);
  32.         self.radice.title('Generatore casuale di parole')
  33.         self.radice.geometry('420x450')
  34.  
  35.  
  36.     def generaParole(self):
  37.         """Genera parole casuale"""
  38.         try:
  39.             parole=int(self.textQuanteParole.get('1.0', tkinter.END))
  40.         except:
  41.             self.labParole['text']='Sono ammessi solo numeri interi.\n'
  42.             self.textQuanteParole.delete('1.0', tkinter.END)
  43.             return
  44.         if (parole <= 0) or (parole >20):
  45.             self.labParole['text']='\nE\' possibile generare fino a 20 parole.'
  46.             self.textQuanteParole.delete('1.0', tkinter.END)
  47.             return
  48.         self.labParole['text']=''
  49.         for i in range(0, parole):
  50.             self.labParole['text']="%s%s" %(self.labParole['text'], self.elencoParole[random.randint(0, len(self.elencoParole))])