Blame platform-demos/ko/radiobutton.py.page

Packit 1470ea
Packit 1470ea
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="radiobutton.py" xml:lang="ko">
Packit 1470ea
  <info>
Packit 1470ea
    <title type="text">RadioButton (Python)</title>
Packit 1470ea
    <link type="guide" xref="beginner.py#buttons"/>
Packit 1470ea
    <link type="seealso" xref="grid.py"/>
Packit 1470ea
    <link type="next" xref="buttonbox.py"/>
Packit 1470ea
    <revision version="0.1" date="2012-05-09" status="draft"/>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright">
Packit 1470ea
      <name>Marta Maria Casetti</name>
Packit 1470ea
      <email its:translate="no">mmcasetti@gmail.com</email>
Packit 1470ea
      <years>2012</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <desc>상호 배타적 관계의 단추입니다.</desc>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>조성호</mal:name>
Packit 1470ea
      <mal:email>shcho@gnome.org</mal:email>
Packit 1470ea
      <mal:years>2017</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>RadioButton</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/radiobutton.png"/>
Packit 1470ea
  

RadioButton이 세개가 있습니다. 터미널에서 켜고 끈 단추를 볼 수 있습니다.

Packit 1470ea
Packit 1470ea
  <links type="section"/>
Packit 1470ea
Packit 1470ea
  <section id="code">
Packit 1470ea
    <title>예제 결과를 만드는 코드</title>
Packit 1470ea
    from gi.repository import Gtk
Packit 1470ea
import sys
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyWindow(Gtk.ApplicationWindow):
Packit 1470ea
Packit 1470ea
    def __init__(self, app):
Packit 1470ea
        Gtk.Window.__init__(self, title="RadioButton Example", application=app)
Packit 1470ea
        self.set_default_size(250, 100)
Packit 1470ea
        self.set_border_width(20)
Packit 1470ea
Packit 1470ea
        # a new radiobutton with a label
Packit 1470ea
        button1 = Gtk.RadioButton(label="Button 1")
Packit 1470ea
        # connect the signal "toggled" emitted by the radiobutton
Packit 1470ea
        # with the callback function toggled_cb
Packit 1470ea
        button1.connect("toggled", self.toggled_cb)
Packit 1470ea
Packit 1470ea
        # another radiobutton, in the same group as button1
Packit 1470ea
        button2 = Gtk.RadioButton.new_from_widget(button1)
Packit 1470ea
        # with label "Button 2"
Packit 1470ea
        button2.set_label("Button 2")
Packit 1470ea
        # connect the signal "toggled" emitted by the radiobutton
Packit 1470ea
        # with the callback function toggled_cb
Packit 1470ea
        button2.connect("toggled", self.toggled_cb)
Packit 1470ea
        # set button2 not active by default
Packit 1470ea
        button2.set_active(False)
Packit 1470ea
Packit 1470ea
        # another radiobutton, in the same group as button1,
Packit 1470ea
        # with label "Button 3"
Packit 1470ea
        button3 = Gtk.RadioButton.new_with_label_from_widget(
Packit 1470ea
            button1, "Button 3")
Packit 1470ea
        # connect the signal "toggled" emitted by the radiobutton
Packit 1470ea
        # with the callback function toggled_cb
Packit 1470ea
        button3.connect("toggled", self.toggled_cb)
Packit 1470ea
        # set button3 not active by default
Packit 1470ea
        button3.set_active(False)
Packit 1470ea
Packit 1470ea
        # a grid to place the buttons
Packit 1470ea
        grid = Gtk.Grid.new()
Packit 1470ea
        grid.attach(button1, 0, 0, 1, 1)
Packit 1470ea
        grid.attach(button2, 0, 1, 1, 1)
Packit 1470ea
        grid.attach(button3, 0, 2, 1, 1)
Packit 1470ea
        # add the grid to the window
Packit 1470ea
        self.add(grid)
Packit 1470ea
Packit 1470ea
    # callback function
Packit 1470ea
    def toggled_cb(self, button):
Packit 1470ea
        # a string to describe the state of the button
Packit 1470ea
        state = "unknown"
Packit 1470ea
        # whenever the button is turned on, state is on
Packit 1470ea
        if button.get_active():
Packit 1470ea
            state = "on"
Packit 1470ea
        # else state is off
Packit 1470ea
        else:
Packit 1470ea
            state = "off"
Packit 1470ea
        # whenever the function is called (a button is turned on or off)
Packit 1470ea
        # print on the terminal which button was turned on/off
Packit 1470ea
        print(button.get_label() + " was turned " + state)
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyApplication(Gtk.Application):
Packit 1470ea
Packit 1470ea
    def __init__(self):
Packit 1470ea
        Gtk.Application.__init__(self)
Packit 1470ea
Packit 1470ea
    def do_activate(self):
Packit 1470ea
        win = MyWindow(self)
Packit 1470ea
        win.show_all()
Packit 1470ea
Packit 1470ea
    def do_startup(self):
Packit 1470ea
        Gtk.Application.do_startup(self)
Packit 1470ea
Packit 1470ea
app = MyApplication()
Packit 1470ea
exit_status = app.run(sys.argv)
Packit 1470ea
sys.exit(exit_status)
Packit 1470ea
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="methods">
Packit 1470ea
    <title>RadioButton 위젯에 쓸만한 메서드</title>
Packit 1470ea
    

16번째 줄에서 "toggled" 시그널은 widget.connect(signal, callback function) 함수로 toggled_cb() 콜백 함수에 연결했습니다. 더 자세한 설명은 <link xref="signals-callbacks.py"/>를 참조하십시오.

Packit 1470ea
Packit 1470ea
    

<link xref="properties.py"/>에서 보신 바와 같이 , button1 = Gtk.RadioButton(label="Button 1") 대신 다음 코드로 단추와 레이블을 만들 수 있습니다

Packit 1470ea
    
Packit 1470ea
button1 = Gtk.RadioButton()
Packit 1470ea
button1.set_label("Button 1").
Packit 1470ea
    

레이블을 붙여서 새 RadioButton을 만드는 또 다른 방법은 button1 = Gtk.RadioButton.new_with_label(None, "Button 1")(처음 인자는 get_group()으로 가져올 수 있는 라디오 단추의 그룹, 두번째 인자는 레이블입니다) 코드를 활용하는 방법입니다.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="references">
Packit 1470ea
    <title>API 참고서</title>
Packit 1470ea
    

이 예제는 다음 참고자료가 필요합니다:

Packit 1470ea
    <list>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkWindow.html">GtkWindow</link>

</item>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkGrid.html">GtkGrid</link>

</item>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkRadioButton.html">GtkRadioButton</link>

</item>
Packit 1470ea
    </list>
Packit 1470ea
  </section>
Packit 1470ea
</page>