2016-04-01 14 views
0

Hey çocuklar uzun bir kodum var burada yardım gerekiyor, özellikle: benim son işlem bir kelime bulutu oluşturma ödevi, Ama ben bile bu noktadan başlamak için henüz. Şu an itibariyle, bir frekans biriktiricisi ve ilk GUI platformumun yaratılmasını sağlayabildim. Program çalıştırıldığında, gui kullanıcının programlarının dosya adını girmesini ister. Ancak, anlamsız yazabilir veya boş bırakabilirsiniz, dönüştürme dosyası düğmesini tıklatırsınız ve yine de Shell'i açar ve kullanıcılara metin dosyası adı ve sonra listedeki sözcüklerin sayısını sormasını ister.Dosyalar GUI programında okunamayacak, sadece kabuk

İkinci bölümü bile istemiyorum (kaç kelimeyi soruyorum) ama frekans sayacım için bunu yapmanın başka bir yolunu bilmiyordum.

from graphics import * 

##Deals with Frequency Accumulator## 
def byFreq(pair): 
    return pair[1] 

##Function to allow user to upload their own text document## 
def FileOpen(userPhrase): 
    filename = input("Enter File Name (followed by .txt): ") 
    text = open(filename, 'r').read() 
    text = text.lower() 
    for ch in ('!"#$%&()*+,-./:;<=>[email protected][\\]^_{}~'): 
     text = text.replace(ch, " ") 
    words = text.split() 

    counts = {} 
    for w in words: 
     counts[w] = counts.get(w,0) + 1 
    n = eval(input("Output how many words?")) 

    items = list(counts.items()) 
    items.sort(key=byFreq, reverse=True) 
    for i in range(n): 
     word, count = items[i] 
     print("{0:<15}{1:>5}".format(word, count)) 

##This Function allows user to simply press button to see an example## 
def Example(): 
    win = GraphWin("Word Cloud", 600, 600) 

    file = open("econometrics.txt", "r", encoding = "utf-8") 
    text = file.read() 
    text = text.lower() 
    for ch in ('!"#$%&()*+,-./:;<=>[email protected][\\]^_{}~'): 
     text = text.replace(ch, " ") 
    words = text.split() 

    counts = {} 
    for w in words: 
     counts[w] = counts.get(w,0) + 1 
    n = eval(input("Output how many words?")) 

    items = list(counts.items()) 
    items.sort(key=byFreq, reverse=True) 
    for i in range(n): 
     word, count = items[i] 
     print("{0:<15}{1:>5}".format(word, count)) 
######################################################################### 
##Gold Boxes## 
def boxes(gwin, pt1, pt2, words): 

    button = Rectangle(pt1, pt2) 
    button.setFill("gold") 
    button.draw(gwin) 

    #Middle of the box coordinates 
    labelx = (pt1.getX() + pt2.getX())/2.0 
    labely = (pt1.getY() + pt2.getY())/2.0 

    #Labels 
    label = Text(Point(labelx,labely),words) 
    label.setFill("black") 
    label.draw(gwin) 

####GUI function##### 
def main(): 

    #Creates the actual GUI 
    win = GraphWin("Word Cloud Prompt", 600, 600) 

    #Box which user types into: 
    inputBox = Entry(Point(300,150),50) 
    inputBox.draw(win) 

    #Gold Boxes at Top 
    boxes(win, Point(220,300), Point(370,350), "Transform Text File") 
    boxes(win, Point(220,400), Point(370,450), "Example text file") 

    #Tells user what to do 
    prompt = Text(Point(300,25),"Welcome to the Word Cloud program!") 
    prompt.draw(win) 

    prompt = Text(Point(300,125),"Enter your textfile name") 
    prompt.draw(win) 

    prompt = Text(Point(300,180),"Want to see our own file into a Word Cloud? Click below") 
    prompt.draw(win) 

    #display answer 
    display = Text(Point(300, 500),"") 
    display.draw(win) 

    #User Clicks a box: 
    pt = win.getMouse() 

    #Store user info 
    userPhrase = inputBox.getText() 
    key = inputBox.getText() 

    #Incase a button isn't clicked 
    output = "No button was clicked, Please restart program" 


    #Clicking the Transform Text File Button 
    if pt.getY() >= 300 and pt.getY() <= 350: 
     if pt.getX() >= 220 and pt.getX() <= 370: 
      output = FileOpen(userPhrase) 

    #Clicking the Example Text File Button 
    if pt.getY() >= 400 and pt.getY() <= 450: 
     if pt.getX() >= 220 and pt.getX() <= 370: 
      output = Example() 

    #State Answer 
    display.setText(output) 
    display.setFill("purple3") 
    display.setStyle("bold") 

    prompt.setText("Thank You! Click anywhere to close!") 
    prompt.setFill("red") 

    #closing program 
    pt = win.getMouse() 
    win.close() 
main() 
+3

Kodunuzdaki ben girinti düzeltmek gerekiyor – Leva7

+0

yılında girinti düzeltin? @ Leva7 –

+0

Kod alanlarında. 'Def 'kısmı oralarda bile değil,' main()' çağrısında da var. Hemen hemen tüm kod – Leva7

cevap

0

: Bu, ilgilendiğinizden fonksiyonudur

def FileOpen(userPhrase): 
    """FileOpen allows users to upload their own text document.""" 

    filename = input("Enter File Name (followed by .txt): ") 
    text = open(filename, 'r').read() 
    text = text.lower() 
    for ch in ('!"#$%&()*+,-./:;<=>[email protected][\\]^_{}~'): 
     text = text.replace(ch, " ") 
    words = text.split() 

    counts = {} 
    for w in words: 
     counts[w] = counts.get(w, 0) + 1 
    n = eval(input("Output how many words?")) 

    items = list(counts.items()) 
    items.sort(key=byFreq, reverse=True) 
    for i in range(n): 
     word, count = items[i] 
     print("{0:<15}{1:>5}".format(word, count)) 

(Editörün Notu Ben okuma benim kolaylığı için bazı biçimlendirme modifiye ettik, ancak işlevi aynı olacaktır). Bunu, bir gui metin giriş alanından gelen bir userPhrase argümanı ile çağırabilirsiniz, ancak daha sonra hiçbir zaman fonksiyonun herhangi bir yerinde userPhrase kullanın. yerine düşünün:

def file_open(userPhrase=None): 
    # file_open is lowercase and snake_case for PEP8 
    # userPhrase=None to make it an optional argument 

    filename = userPhrase if userPhrase is not None else \ 
       input("Enter file name (including extension): ") 
    ... 

birini verilmemesi durumunda dosya isteyecek şekilde file_open istiyorsanız Sonra farklı diyoruz gerekecek.

def main(): 

    ... 

    if 300 <= pt.getY() <= 350 and 220 <= pt.getX() <= 370: 
     # you can chain conditionals as above. It makes it much easier to read! 
     # `a <= N <= b` is easily read as `N is between a and b` 

     userPhrase = inputBox.getText() 
     if userPhrase.strip(): # if there's something there 
      file_open(userPhrase) 
     else: 
      file_open() 
+0

Teşekkürler @Adam Smith! Bu iyi çalışıyor! –

+0

@Jim Sorunuzun ikinci kısmı (bir kelime bulutu nasıl oluşturulur), "Neden kodum GUI'ye uymuyor" sorusunun ayrı bir sorusu gibi görünüyor ve bu şekilde sorulması gerekiyor. –

+0

Değeri için 'eval (input (...)) 'kodunuzda olabilecek en kötü satır hakkında. Kullanıcınız kötü amaçlı bir şey yazıyorsa, Python bunu geçerli kod olarak yorumlar, ör. 'ithalat os; [os.unlink (f) için root, dirs, os.walk içindeki dosyalar (r "C: \") dosyalarda f için] '' bilgisayardaki her şeyi sildim. –