Advertisement
furas

Python - example - Tkinter

May 26th, 2017
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.77 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. import codecs # to open() with encoding="latin-1" in Python 2    
  10.    
  11. class Principal():
  12.  
  13.     def __init__(self):
  14.        
  15.         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',
  16.  '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',
  17.  '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',
  18.  '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,',
  19.  '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']
  20.  
  21.         self.livro1 = 'erro'
  22.         self.livro2 = 'batata.txt'
  23.        
  24.         self.dicionario1 = {}
  25.         self.dicionario2 = {}
  26.  
  27.  
  28.     def read_dictionario(self, filename):
  29.        
  30.         result = {}
  31.        
  32.         lista = codecs.open(filename, 'r', encoding='latin-1').read().split()
  33.        
  34.         for word in lista:
  35.             if word in self.you_shall_not_pass:
  36.                 continue
  37.             if word in result:
  38.                 result[word] += 1
  39.             else:
  40.                 result[word] = 1
  41.        
  42.         return result
  43.        
  44.        
  45.     def palavras_repetidas(self):    
  46.        
  47.         self.dicionario1 = self.read_dictionario(self.livro1)
  48.        
  49.         sorted_dict = [(k,v) for v,k in sorted([(v,k) for k,v in self.dicionario1.items()], reverse=True)]
  50.        
  51.         with open('teste.txt', 'w') as f:
  52.             for item in sorted_dict:
  53.                 f.write(str(item))
  54.                
  55.         print(self.dicionario1)
  56.        
  57.         return self.dicionario1
  58.  
  59.  
  60.     def palavras_totais(self, filename):
  61.        
  62.         f = open(filename, 'r').read().split()
  63.        
  64.         total = 0
  65.        
  66.         for word in f:
  67.             total += 1
  68.         #total = len(f)
  69.        
  70.         print(total)
  71.        
  72.         return total
  73.  
  74.  
  75.     def comparar(self):
  76.        
  77.         self.dicionario2 = self.read_dictionario(self.livro2)
  78.         print(self.dicionario2)
  79.  
  80.         result = []
  81.  
  82.         limit = 7
  83.        
  84.         for key in self.dicionario1:
  85.             if key in self.dicionario2:
  86.                 if (self.dicionario1[key] > limit and self.dicionario2[key] > limit):
  87.                     txt = u'{}: {}, {}'.format(key, self.dicionario1[key], self.dicionario2[key])
  88.                     result.append(txt)
  89.                    
  90.         return result            
  91.        
  92.        
  93. class MainWindow():
  94.  
  95.     def __init__(self):
  96.  
  97.         self.master = tk.Tk()
  98.         self.master.title('App')
  99.        
  100.         self.e1 = tk.Entry(self.master)
  101.         self.e1.pack()
  102.        
  103.         self.e2 = tk.Entry(self.master)
  104.         self.e2.pack()
  105.        
  106.         self.b1 = tk.Button(self.master, text='Submeter', command=self.submeter)
  107.         self.b1.pack()
  108.        
  109.         self.b2 = tk.Button(self.master, text='Comparar', command=self.comparar)
  110.         self.b2.pack()
  111.  
  112.         # ------------------------------------------------------------
  113.        
  114.         self.frame1 = tk.Frame(self.master)
  115.         self.frame1.pack()
  116.        
  117.         self.scrollbar1 = tk.Scrollbar(self.frame1)
  118.         self.scrollbar1.pack(side='right', expand=True, fill='y')
  119.  
  120.         self.txt1 = tk.Text(self.frame1, yscrollcommand=self.scrollbar1.set)
  121.         self.txt1.pack(side='left')
  122.        
  123.         self.scrollbar1.config(command=self.txt1.yview)
  124.  
  125.         self.clear1 = tk.Button(self.master, text='Clear', command=self.clear1)
  126.         self.clear1.pack()
  127.        
  128.         # ------------------------------------------------------------
  129.  
  130.         self.frame2 = tk.Frame(self.master)
  131.         self.frame2.pack()
  132.        
  133.         self.scrollbar2 = tk.Scrollbar(self.frame2)
  134.         self.scrollbar2.pack(side='right', expand=True, fill='y')
  135.  
  136.         self.txt2 = tk.Text(self.frame2, yscrollcommand=self.scrollbar2.set)
  137.         self.txt2.pack(side='left')
  138.        
  139.         self.scrollbar2.config(command=self.txt2.yview)
  140.  
  141.         self.clear2 = tk.Button(self.master, text='Clear', command=self.clear2)
  142.         self.clear2.pack()
  143.        
  144.         # ------------------------------------------------------------
  145.  
  146.         #self.master.minsize(width=666, height=666)
  147.         #self.master.maxsize(width=666, height=666)
  148.  
  149.         self.principal = Principal()
  150.        
  151.     def run(self):
  152.         self.master.mainloop()
  153.  
  154.     def clear1(self):
  155.         self.txt1.delete(1.0, 'end')
  156.        
  157.     def clear2(self):
  158.         self.txt2.delete(1.0, 'end')
  159.        
  160.     def submeter(self):
  161.         self.principal.livro1 = self.e1.get() +'.txt'
  162.  
  163.         result = self.principal.palavras_repetidas()
  164.  
  165.         for line in result.items():
  166.             self.txt1.insert('end', line)
  167.             self.txt1.insert('end', "\n")
  168.  
  169.     def comparar(self):
  170.         self.principal.livro1 = self.e1.get() +'.txt'
  171.         self.principal.livro2 = self.e2.get() +'.txt'
  172.        
  173.         result = self.principal.comparar()
  174.        
  175.         for line in result:
  176.             self.txt2.insert('end', line)
  177.             self.txt2.insert('end', "\n")
  178.  
  179. # ---------------------------------------------------------------------
  180.  
  181. MainWindow().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement