|
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="buttonbox.py" xml:lang="ko">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">ButtonBox (Python)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.py#layout"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="button.py"/>
|
|
Packit |
1470ea |
<link type="next" xref="statusbar.py"/>
|
|
Packit |
1470ea |
<revision version="0.2" date="2012-08-01" status="stub"/>
|
|
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>ButtonBox</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/buttonbox_calculator.png"/>
|
|
Packit |
1470ea |
계산기 - 단추는 수평 ButtonBox에 둘러싸여있습니다.
|
|
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="Calculator", application=app)
|
|
Packit |
1470ea |
self.set_default_size(350, 200)
|
|
Packit |
1470ea |
self.set_border_width(10)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# an entry
|
|
Packit |
1470ea |
self.entry = Gtk.Entry()
|
|
Packit |
1470ea |
# with an initial text
|
|
Packit |
1470ea |
self.entry.set_text('0')
|
|
Packit |
1470ea |
# text aligned on the right
|
|
Packit |
1470ea |
self.entry.set_alignment(1)
|
|
Packit |
1470ea |
# the text in the entry cannot be modified writing in it
|
|
Packit |
1470ea |
self.entry.set_can_focus(False)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a grid
|
|
Packit |
1470ea |
grid = Gtk.Grid()
|
|
Packit |
1470ea |
grid.set_row_spacing(5)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# to attach the entry
|
|
Packit |
1470ea |
grid.attach(self.entry, 0, 0, 1, 1)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# the labels for the buttons
|
|
Packit |
1470ea |
buttons = [7, 8, 9, '/',
|
|
Packit |
1470ea |
4, 5, 6, '*',
|
|
Packit |
1470ea |
1, 2, 3, '-',
|
|
Packit |
1470ea |
'C', 0, '=', '+']
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# each row is a ButtonBox, attached to the grid
|
|
Packit |
1470ea |
for i in range(4):
|
|
Packit |
1470ea |
hbox = Gtk.ButtonBox.new(Gtk.Orientation.HORIZONTAL)
|
|
Packit |
1470ea |
hbox.set_spacing(5)
|
|
Packit |
1470ea |
grid.attach(hbox, 0, i + 1, 1, 1)
|
|
Packit |
1470ea |
# each ButtonBox has 4 buttons, connected to the callback function
|
|
Packit |
1470ea |
for j in range(4):
|
|
Packit |
1470ea |
button = Gtk.Button(label=buttons[i * 4 + j])
|
|
Packit |
1470ea |
button.set_can_focus(False)
|
|
Packit |
1470ea |
button.connect("clicked", self.button_clicked)
|
|
Packit |
1470ea |
hbox.add(button)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# some variables for the calculations
|
|
Packit |
1470ea |
self.first_number = 0
|
|
Packit |
1470ea |
self.second_number = 0
|
|
Packit |
1470ea |
self.counter = 0
|
|
Packit |
1470ea |
self.operation = ""
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# add the grid to the window
|
|
Packit |
1470ea |
self.add(grid)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function for all the buttons
|
|
Packit |
1470ea |
def button_clicked(self, button):
|
|
Packit |
1470ea |
# for the operations
|
|
Packit |
1470ea |
if button.get_label() == '+':
|
|
Packit |
1470ea |
self.counter += 1
|
|
Packit |
1470ea |
if self.counter > 1:
|
|
Packit |
1470ea |
self.do_operation()
|
|
Packit |
1470ea |
self.entry.set_text('0')
|
|
Packit |
1470ea |
self.operation = "plus"
|
|
Packit |
1470ea |
elif button.get_label() == '-':
|
|
Packit |
1470ea |
self.counter += 1
|
|
Packit |
1470ea |
if self.counter > 1:
|
|
Packit |
1470ea |
self.do_operation()
|
|
Packit |
1470ea |
self.entry.set_text('0')
|
|
Packit |
1470ea |
self.operation = "minus"
|
|
Packit |
1470ea |
elif button.get_label() == '*':
|
|
Packit |
1470ea |
self.counter += 1
|
|
Packit |
1470ea |
if self.counter > 1:
|
|
Packit |
1470ea |
self.do_operation()
|
|
Packit |
1470ea |
self.entry.set_text('0')
|
|
Packit |
1470ea |
self.operation = "multiplication"
|
|
Packit |
1470ea |
elif button.get_label() == '/':
|
|
Packit |
1470ea |
self.counter += 1
|
|
Packit |
1470ea |
if self.counter > 1:
|
|
Packit |
1470ea |
self.do_operation()
|
|
Packit |
1470ea |
self.entry.set_text('0')
|
|
Packit |
1470ea |
self.operation = "division"
|
|
Packit |
1470ea |
# for =
|
|
Packit |
1470ea |
elif button.get_label() == '=':
|
|
Packit |
1470ea |
self.do_operation()
|
|
Packit |
1470ea |
self.entry.set_text(str(self.first_number))
|
|
Packit |
1470ea |
self.counter = 1
|
|
Packit |
1470ea |
# for Cancel
|
|
Packit |
1470ea |
elif button.get_label() == 'C':
|
|
Packit |
1470ea |
self.first_number = 0
|
|
Packit |
1470ea |
self.second_number = 0
|
|
Packit |
1470ea |
self.counter = 0
|
|
Packit |
1470ea |
self.entry.set_text('')
|
|
Packit |
1470ea |
self.operation = ""
|
|
Packit |
1470ea |
# for a digit button
|
|
Packit |
1470ea |
else:
|
|
Packit |
1470ea |
new_digit = int(button.get_label())
|
|
Packit |
1470ea |
if self.entry.get_text() == 'error':
|
|
Packit |
1470ea |
number = 0
|
|
Packit |
1470ea |
else:
|
|
Packit |
1470ea |
number = int(self.entry.get_text())
|
|
Packit |
1470ea |
number = number * 10 + new_digit
|
|
Packit |
1470ea |
if self.counter == 0:
|
|
Packit |
1470ea |
self.first_number = number
|
|
Packit |
1470ea |
else:
|
|
Packit |
1470ea |
self.second_number = number
|
|
Packit |
1470ea |
self.entry.set_text(str(number))
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_operation(self):
|
|
Packit |
1470ea |
if self.operation == "plus":
|
|
Packit |
1470ea |
self.first_number += self.second_number
|
|
Packit |
1470ea |
elif self.operation == "minus":
|
|
Packit |
1470ea |
self.first_number -= self.second_number
|
|
Packit |
1470ea |
elif self.operation == "multiplication":
|
|
Packit |
1470ea |
self.first_number *= self.second_number
|
|
Packit |
1470ea |
elif self.operation == "division":
|
|
Packit |
1470ea |
try:
|
|
Packit |
1470ea |
self.first_number /= self.second_number
|
|
Packit |
1470ea |
except ZeroDivisionError:
|
|
Packit |
1470ea |
self.first_number = 0
|
|
Packit |
1470ea |
self.second_number = 0
|
|
Packit |
1470ea |
self.counter = 0
|
|
Packit |
1470ea |
self.entry.set_text('error')
|
|
Packit |
1470ea |
self.operation = ""
|
|
Packit |
1470ea |
return
|
|
Packit |
1470ea |
else:
|
|
Packit |
1470ea |
self.first_number = 0
|
|
Packit |
1470ea |
self.second_number = 0
|
|
Packit |
1470ea |
self.counter = 0
|
|
Packit |
1470ea |
self.entry.set_text('error')
|
|
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>ButtonBox 위젯에 쓸만한 메서드</title>
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item>ButtonBox 배치는 set_layout(layout) 함수로 설정하며, layout 에 들어갈 값은 Gtk.ButtonBoxStyle.SPREAD (단추가 상자 크기만큼 늘어남), Gtk.ButtonBoxStyle.EDGE (단추가 상자 가장자리에 붙음), Gtk.ButtonBoxStyle.START (단추가 상자 시작 부분에 모임), Gtk.ButtonBoxStyle.END (단추가 상자 끝부분에 모임), Gtk.ButtonBoxStyle.CENTER (단추가 상자 한가운데로 모임)가 될 수 있습니다. </item>
|
|
Packit |
1470ea |
<item>set_child_secondary(button, is_secondary) 함수로 button 이 하위 두번째 모임으로 나타날지 여부를 설정합니다. 2차 하위요소는 대화상자의 도움말 단추가 될 수 있습니다. 이 모임은 모양새가 START , SPREAD , EDGE 중 하나고, 그 이전 하위 요소의 모양새가 END 일 때, 다른 하위 요소의 전에 나타납니다. 모양새가 START , END 중 하나면, 주 하위 요소의 단추 상자 다른 끝부분에 두번째 하위 요소를 정렬합니다. 다른 모양새로 설정하면, 주 하위 요소 바로 다음에 나타냅니다. </item>
|
|
Packit |
1470ea |
<item>set_child_non_homogeneous(button, is_non_homogeneous) 함수로 하위 요소에 대한 동일 크기 조절 방식 비적용 여부를 설정합니다. 기본 값은 false 입니다. </item>
|
|
Packit |
1470ea |
<item>set_spacing(spacing) 함수는 상자안의 단추 사이 간격을 픽셀 단위로 설정합니다. </item>
|
|
Packit |
1470ea |
</list>
|
|
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/stable/GtkButtonBox.html">GtkButtonBox</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/stable/GtkBox.html">GtkBox</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/stable/GtkButton.html">GtkButton</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/stable/GtkEntry.html">GtkEntry</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/stable/GtkGrid.html">GtkGrid</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|