버튼으로 Tkinter Text 위젯의 텍스트를 설정하는 방법

Jinku Hu 2020년6월25일 Tkinter Tkinter Text
버튼으로 Tkinter Text 위젯의 텍스트를 설정하는 방법

Tkinter Text 위젯에는 Text 의 내용을 설정하기위한 전용 set 메소드가 없습니다. 컨텐츠를 완전히 변경해야하는 경우 먼저 기존 컨텐츠를 삭제 한 다음 새 컨텐츠를 삽입해야합니다.

deleteinsert 메소드를 사용하여 Text 에 텍스트를 설정하는 완전한 작업 코드

import tkinter as tk

root = tk.Tk()
root.geometry("400x240")


def setTextInput(text):
    textExample.delete(1.0, "end")
    textExample.insert(1.0, text)


textExample = tk.Text(root, height=10)
textExample.pack()

btnSet = tk.Button(
    root, height=1, width=10, text="Set", command=lambda: setTextInput("new content")
)
btnSet.pack()

root.mainloop()

Tkinter Tkinter Text_delete 의 내용 설정 및 삽입 방법

textExample.delete(1.0, "end")

Textdelete 메소드는 Tkinter Text 상자를 지우는 방법 기사에 소개 된대로 Text 상자에서 지정된 문자 범위를 삭제합니다.

1.0 은 첫 번째 문자이고'end'Text 위젯에있는 컨텐츠의 마지막 문자입니다. 따라서 ‘텍스트’상자 안의 모든 내용을 삭제합니다.

textExample.insert(1.0, text)

insert 메소드는 텍스트를 지정된 위치에 삽입합니다. 위의 코드에서 시작 부분에 ‘텍스트’를 삽입합니다.

튜토리얼이 마음에 드시나요? DelftStack을 구독하세요 YouTube에서 저희가 더 많은 고품질 비디오 가이드를 제작할 수 있도록 지원해주세요. 구독하다
작가: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

관련 문장 - Tkinter Text