2015-01-15 3 views
12

우분투의 TKinter에 파이썬 프로그램을 작성하여 Text 위젯에있는 특정 폴더의 파일 이름을 으로 가져오고 인쇄합니다. Text위젯에있는 이전의 파일 이름에 파일 이름을 추가하는 것입니다.하지만 먼저 파일 이름을 지우고 새 파일 이름 목록을 추가하고 싶습니다. 그러나 Text 위젯의 이전 목록 인 개의 파일 이름을 지우려고 고심하고 있습니다.Tkinter Text 위젯의 내용을 지우거나 지우는 방법은 무엇입니까?

누군가 Text 위젯을 삭제하는 방법을 설명해 주실 수 있습니까?

Screenshoot 및 코딩은 아래주고있다 :

screenshot showing text widget with contents

import os 
from Tkinter import * 

def viewFile(): 
    path = os.path.expanduser("~/python") 
    for f in os.listdir(path): 
     tex.insert(END, f + "\n") 

if __name__ == '__main__': 
    root = Tk() 

    step= root.attributes('-fullscreen', True) 
    step = LabelFrame(root, text="FILE MANAGER", font="Arial 20 bold italic") 
    step.grid(row=0, columnspan=7, sticky='W', padx=100, pady=5, ipadx=130, ipady=25) 

    Button(step, text="File View", font="Arial 8 bold italic", activebackground= 
      "turquoise", width=30, height=5, command=viewFile).grid(row=1, column=2) 
    Button(step, text="Quit", font="Arial 8 bold italic", activebackground= 
      "turquoise", width=20, height=5, command=root.quit).grid(row=1, column=5) 

    tex = Text(master=root) 
    scr=Scrollbar(root, orient=VERTICAL, command=tex.yview) 
    scr.grid(row=2, column=2, rowspan=15, columnspan=1, sticky=NS) 
    tex.grid(row=2, column=1, sticky=W) 
    tex.config(yscrollcommand=scr.set, font=('Arial', 8, 'bold', 'italic')) 

    root.mainloop() 
+1

텍스트 위젯에 대한 문서를 읽으셨습니까? 이 기능은 명확하게 문서화되어 있습니다. 당신이 고군분투하고 있다고 말하면서, 당신이 시도한 것을 보여줄 수 있습니까? –

+1

아마 http://effbot.org/tkinterbook/entry.htm#Tkinter.Entry.delete-method –

+0

내 요구 결과를 얻으려면 여기에 하나의 성명을 쓰십시오. – Fahadkalis

답변

33

난 그냥 '1.0'를 추가하여 내 옆에 확인하고 그것은 당신이 시도 할 수

tex.delete('1.0', END) 

작업 시작

+0

고마워, 그건 일단 내가 tex를해야한다는 것을 깨달았다.내가 그것을 삭제하기 전에 config (state = NORMAL) . – Seth

9

tkinterbook에 따르면 cl 귀 텍스트 요소는 다음과 같아야합니다.

text.delete(1.0,END) 

이것은 나를 위해 일했습니다. source

그것은 다음과 같이 수행된다 엔트리 요소가 제거 된 다르다 :

entry.delete (0, END) 0 대신 여기서 1.0

+0

Tkinter 인덱스가''1.0 ''과 같은 문자열로 지정되어야하므로 (실제로는 그렇지만)''1.0 ''이 작동한다는 것이 이상하다는 것을 알게됩니다. (또한 작동하며 다른 위치의 대부분에서 수행됩니다. 같은 문서). – martineau

0
from Tkinter import * 

app = Tk() 

# Text Widget + Font Size 
txt = Text(app, font=('Verdana',8)) 
txt.pack() 

# Delete Button 
btn = Button(app, text='Delete', command=lambda: txt.delete(1.0,END)) 
btn.pack() 

app.mainloop() 

로를 #note를 txt.delete(1.0,END)의 예 말한 바와 같이.

lambda을 사용하면 실제 기능을 정의하지 않고도 내용을 삭제할 수 있습니다.

도움이 되길 바랍니다

관련 문제