Advertisement
furas

Python - example - Tkinter

May 26th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.52 KB | None | 0 0
  1. #coding=latin-1
  2.  
  3. from __future__ import print_function # use print() in Python 2
  4. try:
  5.     import Tkinter as tk # Python 2
  6. except:  
  7.     import tkinter as tk # Python 3
  8.    
  9.    
  10. class Principal():
  11.  
  12.     def __init__(self):
  13.        
  14.         self.you_shall_not_pass = ['de', 'o','a', 'do', 'da', 'em', 'e', 'que', 'ser', 'um', 'uns', 'uma', 'umas', 'por','para', 'nao', 'com', 'se', 'ter', 'seu', 'sua', 'seus', 'suas','como',
  15.  'estar','sim', '-', 'os', 'as', 'no', 'ao', 'na', '.', 'mas', 'era', 'lhe', 'ele', 'mais','dos', 'ela', 'ele', 'muito','ja','eu','sobre','das','tinha','quando','sem','la','num',
  16.  'depois','entao','nos','esta','ainda','numa','onde','foi','tao','estava','entre','dum','so','grande','disse','todo','pela','agora','pelo','me','toda', 'havia', 'tudo', 'ou','ate',
  17.  'ha','nem','logo','ia','sempre','duas','tambem','mesmo','ali','aos','tem','fora','aquele','tu','meu','duma','aquela','e,','outra','essa','outro','isto','esse','assim','quem','ele,',
  18.  'apenas','todos','pouco','nas','diante','ai','sob','tinham','coisas','aqui','pois','porque','ir','te','nunca','depois','lado','tres','mao','vez','sim,','desde','isso','fazer','este']
  19.  
  20.         self.livro1 = 'erro'
  21.         self.livro2 = 'batata.txt'
  22.        
  23.         self.dicionario1 = {}
  24.         self.dicionario2 = {}
  25.  
  26.  
  27.     def read_dictionario(self, filename):
  28.        
  29.         result = {}
  30.        
  31.         lista = open(filename, 'r', encoding='latin-1').read().split()
  32.        
  33.         for word in lista:
  34.             if word in self.you_shall_not_pass:
  35.                 continue
  36.             if word in result:
  37.                 result[word] += 1
  38.             else:
  39.                 result[word] = 1
  40.        
  41.         return result
  42.        
  43.        
  44.     def palavras_repetidas(self):    
  45.        
  46.         self.dicionario1 = self.read_dictionario(self.livro1)
  47.        
  48.         sorted_dict = [(k,v) for v,k in sorted([(v,k) for k,v in self.dicionario1.items()], reverse=True)]
  49.        
  50.         with open('teste.txt', 'w') as f:
  51.             for item in sorted_dict:
  52.                 f.write(str(item))
  53.                
  54.         print(self.dicionario1)
  55.        
  56.         return self.dicionario1
  57.  
  58.  
  59.     def palavras_totais(self, filename):
  60.        
  61.         f = open(filename, 'r').read().split()
  62.        
  63.         total = 0
  64.        
  65.         for word in f:
  66.             total += 1
  67.         #total = len(f)
  68.        
  69.         print(total)
  70.        
  71.         return total
  72.  
  73.  
  74.     def comparar(self):
  75.        
  76.         self.dicionario2 = self.read_dictionario(self.livro2)
  77.         print(self.dicionario2)
  78.  
  79.         result = []
  80.  
  81.         limit = 10
  82.        
  83.         for key in self.dicionario1:
  84.             if key in self.dicionario2:
  85.                 if (self.dicionario1[key] > limit and self.dicionario2[key] > limit):
  86.                     txt = '{}: {}, {}'.format(key, self.dicionario1[key], self.dicionario2[key])
  87.                     result.append(txt)
  88.                    
  89.         return result            
  90.        
  91.        
  92. class MainWindow():
  93.  
  94.     def __init__(self):
  95.  
  96.         self.master = tk.Tk()
  97.         self.master.title('App')
  98.        
  99.         self.e1 = tk.Entry(self.master)
  100.         self.e1.pack()
  101.        
  102.         self.e2 = tk.Entry(self.master)
  103.         self.e2.pack()
  104.        
  105.         self.b1 = tk.Button(self.master, text='Submeter', command=self.submeter)
  106.         self.b1.pack()
  107.        
  108.         self.b2 = tk.Button(self.master, text='Comparar', command=self.comparar)
  109.         self.b2.pack()
  110.  
  111.         self.txt1 = tk.Text(self.master)
  112.         self.txt1.pack()
  113.        
  114.         self.txt2 = tk.Text(self.master)
  115.         self.txt2.pack()
  116.  
  117.         self.master.minsize(width=666, height=666)
  118.         self.master.maxsize(width=666, height=666)
  119.  
  120.         self.principal = Principal()
  121.        
  122.     def run(self):
  123.         self.master.mainloop()
  124.  
  125.     def submeter(self):
  126.         self.principal.livro1 = self.e1.get() +'.txt'
  127.  
  128.         result = self.principal.palavras_repetidas()
  129.  
  130.         for line in result.items():
  131.             self.txt1.insert('end', line)
  132.             self.txt1.insert('end', "\n")
  133.  
  134.     def comparar(self):
  135.         print("MainWindow: comparar")
  136.  
  137.         self.principal.livro1 = self.e1.get() +'.txt'
  138.         self.principal.livro2 = self.e2.get() +'.txt'
  139.        
  140.         result = self.principal.comparar()
  141.        
  142.         for line in result:
  143.             self.txt2.insert('end', line)
  144.             self.txt2.insert('end', "\n")
  145.  
  146. # ---------------------------------------------------------------------
  147.  
  148. MainWindow().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement