이 프로그램은 터미널에 입력한 이름과 함께 인사를 출력합니다.
from gi.repository import Gtk
import sys
class MyWindow(Gtk.ApplicationWindow):
def __init__(self, app):
Gtk.Window.__init__(self, title="What is your name?", application=app)
self.set_default_size(300, 100)
self.set_border_width(10)
# a single line entry
name_box = Gtk.Entry()
# emits a signal when the Enter key is pressed, connected to the
# callback function cb_activate
name_box.connect("activate", self.cb_activate)
# add the Gtk.Entry to the window
self.add(name_box)
# the content of the entry is used to write in the terminal
def cb_activate(self, entry):
# retrieve the content of the widget
name = entry.get_text()
# print it in a nice form in the terminal
print("Hello " + name + "!")
class MyApplication(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def do_activate(self):
win = MyWindow(self)
win.show_all()
def do_startup(self):
Gtk.Application.do_startup(self)
app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)
수
14번째 줄에서 "activate"
시그널은 widget.connect(signal, callback function)
콜백 함수에 연결했습니다. 콜백 함수에 연결했습니다. 더 자세한 설명은 를 참조하십시오. Gtk.Entry 위젯에서 나타날 수 있는 일부 시그널은 "activate"
(사용자가 Entry 키를 눌렀을 때 나타남), "backspace"
(사용자가 Backspace 또는 Shift-Backspace 키를 눌렀을 때 나타남), "copy-clipboard"
(Ctrl-c와 Ctrl-Insert키를 눌렀을 때), "paste-clipboard"
(Ctrl-v와 Shift-Insert 키를 눌렀을 때), "delete-from-cursor"
(문자를 지울 때 Delete 키를 눌렀을 때, 단어를 지울 경우 Ctrl-Delete키를 눌렀을 때) "icon-press"
(사용자가 활성화할 수 있는 아이콘을 눌렀을 때 나타남), "icon-release"
(사용자가 활성화할 수 있는 아이콘을 누른 상황에서 마우스 커서를 그 위에 두고 단추를 뗐을 때 나타남), "insert-at-cursor"
(사용자가 커서 위치에 지정 문자열을 넣기 시작할 때 나타남), "move-cursor"
(사용자가 커서 이동을 시작할 때 나타남), "populate-popup"
(항목의 단축 메뉴를 나타내기 전에 나타남. 단축 메뉴에 항목을 추가할 때 사용할 수 있습니다)이 있습니다.
get_buffer()
와 set_buffer(buffer)
는 항목의 버퍼를 가져오거나 설정할 때 사용할 수 있으며, buffer
자리에는 Gtk.EntryBuffer 객체가 들어갑니다.
get_text()
와 set_text("some text")
는 항목의 텍스트 내용을 가져오고 설정할 때 사용할 수 있습니다.
get_text_length()
메서드는 이름 그대로 동작합니다.
get_text_area()
는 항목 텍스트를 그린 영역(크기)를 가져옵니다.
If we set set_visibility(False)
the characters in the entry are displayed as the invisible char. This is the best available in the current font, but it can be changed with set_invisible_char(ch)
, where ch
is a Unicode character. The latter method is reversed by unset_invisible_char()
.
set_max_length(int)
함수는 항목이 최대 결정 길이 int
를 넘으면 자르며, int
값은 정수형입니다.
기본적으로 Entry 키를 누르면 "activate"
시그널을 내보냅니다. 창의 기본 위젯으로 활성화하려면(창에서 set_default(widget)
메서드 호출), set_activates_default(True)
메서드를 호출하십시오.
항목에 테두리를 설정하려면 set_has_frame(True)
를 실행하십시오.
set_placeholder_text("some text")
메서드는 항목이 비어있으며 포커스를 주지 않았을 경우 나타낼 텍스트를 설정합니다.
set_overwrite_mode(True)
및 set_overwrite_mode(False)
메서드는 이름 그대로 동작합니다.
set_editable(False)
메서드를 호출하면 사용자가 위젯의 텍스트를 편집할 수 없습니다.
completion
이 Gtk.EntryCompletion
인 set_completion(completion)
함수는 completion
값이 None
일 경우 completion 값을 설정하거나 비활성화합니다.
항목 위젯에는 화면에 나타난 텍스트 이면의 진행 상황 또는 활동 정보를 나타낼 수 있습니다. 진행 표시줄 막대에 비율을 주어 채워넣을 목적으로 set_progress_fraction(fraction)
함수를 사용하며, 여기서 fraction
은 0.0
과 1.0
사이의 float
값입니다. progress_pulse()
를 호출할 때마다 진행 표시줄 바운싱 블록을 이동할 전체 항목 너비 비율을 설정할 때 set_progress_pulse_step()
함수를 사용합니다. 후자 함수는 일부 진행 과정을 끝냈고 블록이 앞뒤로 튀는 항목의 진행 상황 표시자가 "활동 모드"로 들어감을 나타냅니다. 각각의 함수는 progress_pulse()
를 호출하여블록을 약간씩 움직이게 합니다(앞에서 말한 바와 같이 set_progress_pulse_step()
함수로 주기당 움직이는 정도를 결정합니다).
항목 위젯은 아이콘을 나타낼 수도 있습니다. 이 아이콘은 눌렀을 떄 활성화할 수 있는데, 끌어다 놓을 수 있게 할 수 있으며, 풍선 도움말을 설정할 수도 있습니다. 아이콘을 추가하려면 set_icon_from_stock(icon_position, stock_id)
또는 set_icon_from_pixbuf(icon_position, pixbuf)
, set_icon_from_icon_name(icon_position, icon_name)
둘 중 하나를 사용하시면 되며 icon_position
에는 Gtk.EntryIconPosition.PRIMARY
(항목 시작 부분에 아이콘 위치) Gtk.EntryIconPosition.SECONDARY
(항목 뒷 부분에 아이콘 위치) 값 중 하나가 들어갑니다 아이콘에 풍선 도움말을 설정하려면, set_icon_tooltip_text("풍선 도움말 문장")
또는 set_icon_tooltip_markup("팡고 마크업 언어를 사용한 풍선 도움말 문장")
함수를 사용하십시오.
이 예제는 다음 참고자료가 필요합니다:
GtkEntry