Enter the radius, calculate the circle perimeter, area, and sphere volume, and draw the circle.
The data of the drag bar, input box, and image control is consistent!
Pass the test under Fedora
Copy codeThe Code is as follows:
# Https://github.com/RobberPhex/GTK-Example-CalcArea
From gi. repository import Gtk, Gdk, GdkPixbuf
From PIL import Image, ImageDraw
From io import BytesIO
From math import pi
Class Model:
'''
Model class
Storage radius, calculation perimeter, area, volume
'''
Def _ init _ (self ):
Self. _ radius = 0
Def setRadius (self, radius ):
Self. _ radius = float (radius)
Def getRadius (self ):
Return self. _ radius
Def getPerimeter (self ):
Return pi * self. _ radius * 2
Def getArea (self ):
Return self. _ radius ** 2 * pi
Def getVolume (self ):
Return 4 * pi * self. _ radius ** 3/3
Class Controller:
'''
Controller type
Control views and model updates
'''
Def _ init _ (self, model ):
Self. model = model
Self. _ observers = []
Def addObserver (self, observer ):
Self. _ observers. append (observer)
Def setRadius (self, radius ):
Model. setRadius (radius)
Self. Policy ()
Def Policy (self ):
For observer in self. _ observers:
Observer. update ()
Class TextView:
'''
Text View
Process the view of the text input box
'''
Def _ init _ (self, model, rEntry, pEntry, aEntry, vEntry ):
'''
: Type model Model
'''
Self. model = model
Self. rEntry = rEntry
Self. pEntry = pEntry
Self. aEntry = aEntry
Self. vEntry = vEntry
Def update (self ):
Self. rEntry. set_text ('% 2.2f' % self. model. getRadius ())
Self. pEntry. set_text ('% 2.2f' % self. model. getPerimeter ())
Self. aEntry. set_text ('% 2.2f' % self. model. getArea ())
Self. vEntry. set_text ('% 2.2f' % self. model. getVolume ())
Class ScaleView:
'''
Drag View
View of dragging bars
'''
Def _ init _ (self, model, scale ):
'''
: Type model Model
'''
Self. model = model
Self. scale = scale
Def update (self ):
Self. scale. set_value (self. model. getRadius ())
Class ImageView:
'''
Image view
View of processed images
'''
@ Classmethod
Def imgToPixbuf (cls, img ):
'''
: Type img Image
'''
Buff = BytesIO ()
Img. save (buff, 'ppm ')
Contents = buff. getvalue ()
Buff. close ()
Loader = GdkPixbuf. PixbufLoader. new_with_type ('pnm ')
Loader. write (contents)
Pixbuf = loader. get_pixbuf ()
Loader. close ()
Return pixbuf
@ Classmethod
Def ellipse (cls, radius ):
'''
: Type radius int
'''
Image = Image. new ("RGBA", (300,300), "white ")
Draw = ImageDraw. Draw (image)
Minor = 150-radius
Major = 150 + radius
Draw. ellipse (minor, minor, major, major), outline = 'red ')
Pixbuf = ImageView. imgToPixbuf (image)
Return pixbuf
Def _ init _ (self, model, image ):
Self. model = model
Self. image = image
Def update (self ):
Radius = self. model. getRadius ()
Pixbuf = ImageView. ellipse (radius)
Self. image. set_from_pixbuf (pixbuf)
Class MainWindow (Gtk. Window ):
'''
Main Window Type
Displays the overall interface.
'''
Def textCallback (self, widget, controller ):
'''
Text input callback
'''
Try:
Radius = float (widget. get_text ())
Controller. setRadius (radius)
Failed t ValueError as e:
Pass
Def scaleCallback (self, widget, controller ):
'''
Drag callback
'''
Radius = widget. get_value ()
Controller. setRadius (radius)
Def _ init _ (self ):
Gtk. Window. _ init _ (self, title = "Title ")
Self. set_default_size (600,400)
Self. set_position (Gtk. WindowPosition. CENTER)
Hbox = Gtk. HBox (spacing = 5)
Self. add (hbox)
Vbox = Gtk. VBox (spacing = 5)
Hbox. pack_start (vbox, True, True, 2)
Table = Gtk. Table. new (4, 2, False)
Vbox. pack_start (table, True, True, 2)
Label = Gtk. Label ('radius :')
Table. attach_defaults (label, 0, 1, 0, 1)
Label = Gtk. Label ('Perimeter :')
Table. attach_defaults (label, 0, 1, 1, 2)
Label = Gtk. Label ('area :')
Table. attach_defaults (label, 0, 1, 2, 3)
Label = Gtk. Label ('Volume :')
Table. attach_defaults (label, 0, 1, 3, 4)
Self. radiusEntry = Gtk. Entry. new ()
Self. radiusEntry. connect ('changed ', self. textCallback, controller)
Table. attach_defaults (self. radiusEntry, 1, 2, 0, 1)
Self. perimeterEntry = Gtk. Entry. new ()
Self. perimeterEntry. set_sensitive (False)
Self. perimeterEntry. set_text ('Perimeter ')
Table. attach_defaults (self. perimeterEntry, 1, 2, 1, 2)
Self. areaEntry = Gtk. Entry. new ()
Self. areaEntry. set_sensitive (False)
Self. areaEntry. set_text ('area ')
Table. attach_defaults (self. areaEntry, 1, 2, 2, 3)
Self. volumeEntry = Gtk. Entry. new ()
Self. volumeEntry. set_sensitive (False)
Self. volumeEntry. set_text ('volume ')
Table. attach_defaults (self. volumeEntry, 1, 2, 3, 4)
Self. scale = Gtk. HScale. new_with_range (0,100, 1)
Self. scale. connect ('value-changed ', self. scaleCallback, controller)
Vbox. pack_start (self. scale, False, False, 2)
Pixbuf = GdkPixbuf. Pixbuf. new (GdkPixbuf. Colorspace. RGB, True, 8,300,300)
Pixbuf. fill (0 xaaaaaaaa)
Self. image = Gtk. Image. new_from_pixbuf (pixbuf)
Hbox. pack_start (self. image, True, True, 2)
Self. connect ('delete-event', Gtk. main_quit)
Model = Model ()
Controller = Controller (model)
If _ name _ = '_ main __':
Gdk. threads_init ()
Gdk. threads_enter ()
Win = MainWindow ()
Iv = ImageView (model, win. image)
Controller. addObserver (iv)
TV = TextView (model, win. radiusEntry, win. perimeterEntry, win. areaEntry, win. volumeEntry)
Controller. addObserver (TV)
Sv = ScaleView (model, win. scale)
Controller. addObserver (sv)
Win. show_all ()
Gtk. main ()
Gdk. threads_leave ()