|
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="colorbutton.py" xml:lang="el">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">ColorButton (Python)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.py#color-selectors"/>
|
|
Packit |
1470ea |
<link type="next" xref="fontchooserwidget.py"/>
|
|
Packit |
1470ea |
<revision version="0.1" date="2012-06-03" 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>Ελληνική μεταφραστική ομάδα GNOME</mal:name>
|
|
Packit |
1470ea |
<mal:email>team@gnome.gr</mal:email>
|
|
Packit |
1470ea |
<mal:years>2012-2015</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
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>dmtrs32@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2012, 2013</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
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>marablack3@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2014</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
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>tomtryf@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2014, 2015</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>Κουμπί χρώματος (ColorButton)</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/colorbutton.png"/>
|
|
Packit |
1470ea |
Αυτό το ColorButton ξεκινά τον διάλογο επιλογής χρωμάτων και τυπώνει στον τερματικό τις τιμές RGB του επιλεγμένου χρώματος.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<links type="sections"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="code">
|
|
Packit |
1470ea |
<title>Ο χρησιμοποιούμενος κώδικας για παραγωγή αυτού παραδείγματος</title>
|
|
Packit |
1470ea |
from gi.repository import Gtk
|
|
Packit |
1470ea |
from gi.repository import Gdk
|
|
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="ColorButton", application=app)
|
|
Packit |
1470ea |
self.set_default_size(150, 50)
|
|
Packit |
1470ea |
self.set_border_width(10)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a colorbutton (which opens a dialogue window in
|
|
Packit |
1470ea |
# which we choose a color)
|
|
Packit |
1470ea |
self.button = Gtk.ColorButton()
|
|
Packit |
1470ea |
# with a default color (blue, in this instance)
|
|
Packit |
1470ea |
color = Gdk.RGBA()
|
|
Packit |
1470ea |
color.red = 0.0
|
|
Packit |
1470ea |
color.green = 0.0
|
|
Packit |
1470ea |
color.blue = 1.0
|
|
Packit |
1470ea |
color.alpha = 0.5
|
|
Packit |
1470ea |
self.button.set_rgba(color)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# choosing a color in the dialogue window emits a signal
|
|
Packit |
1470ea |
self.button.connect("color-set", self.on_color_chosen)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a label
|
|
Packit |
1470ea |
label = Gtk.Label()
|
|
Packit |
1470ea |
label.set_text("Click to choose a color")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a grid to attach button and label
|
|
Packit |
1470ea |
grid = Gtk.Grid()
|
|
Packit |
1470ea |
grid.attach(self.button, 0, 0, 2, 1)
|
|
Packit |
1470ea |
grid.attach(label, 0, 1, 2, 1)
|
|
Packit |
1470ea |
self.add(grid)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# if a new color is chosen, we print it as rgb(r,g,b) in the terminal
|
|
Packit |
1470ea |
def on_color_chosen(self, user_data):
|
|
Packit |
1470ea |
print("You chose the color: " + self.button.get_rgba().to_string())
|
|
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>Χρήσιμες μέθοδοι για ένα γραφικό στοιχείο ColorButton</title>
|
|
Packit |
1470ea |
set_color(color) , όπου το color ορίζεται όπως στο παράδειγμα, ορίζει το χρώμα του ColorButton, που από προεπιλογή είναι μαύρο. get_color() επιστρέφει το χρώμα.
|
|
Packit |
1470ea |
Στη γραμμή 23 το σήμα "color-set" συνδέεται με τη συνάρτηση επανάκλησης on_color_chosen() χρησιμοποιώντας widget.connect(signal, callback function) . Δείτε <link xref="signals-callbacks.py"/> για μια πιο λεπτομερή εξήγηση.
|
|
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/GtkColorButton.html">GtkColorButton</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkColorChooser.html">GtkColorChooser</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gdk3/stable/gdk3-RGBA-Colors.html">RGBA Colors</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|