2016-08-28 2 views
-1

그래서 "tkinter"라는 단어를 입력하면 문자 빈도가 나옵니다. 그러나 "t k i n t e r"을 입력하면 완전히 다른 데이터를 얻을 수 있습니다. 입력에서 공백을 제거하려면 어떻게해야합니까? key, value/float(len(user_input))*100, '%'을 Tkinter 레이블에 추가하려면 어떻게합니까?tkinter 레이블에 정보 추가 및 공백 제거

공백을 대체하는 방법과 도움이되는 주석 원의 제안을 통해 데이터를 해당 키와 함께 표시하는 레이블에 성공적으로 추가 할 수 있습니다. 그러나 단추를 클릭 할 때마다 이전 레이블을 제거하는 방법에 대한 또 다른 질문이 있습니까?

코드 :

from Tkinter import * 
from collections import Counter 
import string 


def let_freq(): 
    user_input = (e.get().lower()).translate(None, string.whitespace) 
    res = "" 
    value_alphabet = Counter(user_input) 
    v = StringVar() 

    label = Label(root, text="Result:", textvariable=v).grid(ipadx=5,ipady=5) 

    for key, value in value_alphabet.items(): 
     print key, value/float(len(user_input))*100, '%' 
     res += '\n'+key+'-'+`value/float(len(user_input))*100`+'%' 

    v.set(res) 

root = Tk() 
e = Entry(root); assert isinstance(e, object); e.grid(ipadx=5, ipady=5) 
button = Button(root, command=let_freq, width=16).grid(ipadx=5, ipady=5) 
root.mainloop() 
+0

것은 당신이 파이썬 문서, 웹 사이트, 또는 이러한 근본적인 질문에 대한 답변을 인터넷 검색을 시도했다 Tkinter를 라벨에 결과를 추가하는 데 도움이 수 있습니까? –

+0

예, 시도한 사항이 없으면 질문을 게시하지 않았습니까? – komoka

+0

@komoka "완전히 다른 데이터를 줄 것"으로 충분하지 않습니다. 정확히 무엇을 줄 것입니까? –

답변

0

사용 .replace(" ","") 공백을 제거합니다. 코드 아래

from Tkinter import * 
from collections import Counter 


def let_freq(): 
    user_input = e.get().lower() 
    user_input_Nowhitespaces = user_input.replace(" ", "") 
    value_alphabet = Counter(user_input_Nowhitespaces) 
    v = StringVar() 
    label = Label(root,text="Result:",textvariable=v).grid(ipadx=5, ipady=5) 
    res="" 

    for key, value in value_alphabet.items(): 
     print key, value/float(len(user_input_Nowhitespaces))*100, '%' 
     res+='\n'+key+','+`value/float(len(user_input_Nowhitespaces))*100`+'%' 

    v.set(res) 

root = Tk() 
e = Entry(root); assert isinstance(e, object); e.grid(ipadx=5, ipady=5) 
button = Button(root, command=let_freq, width=16).grid(ipadx=5, ipady=5) 

root.mainloop() 
+0

사용자가 둘 이상의 단어로 구성된 문장을 입력하면 어떻게 될까요? –

+0

이 경우 입력 문자열을 단어로 토큰 화해야합니다. str = "Hello world"와 같이 str.strip ("")'은 "hello", "world"가됩니다. 그러나 입력이 "Hello world"이면 "hello", "w", "o", "r", "l", "d"가됩니다. – vinod

+0

아니요, str = "Hello world"str. strip ("")은 "hello", "world"가되지 않습니다. –