Python calculates circle circumference, area, sphere volume and draws round _python

Source: Internet
Author: User
Tags gtk

Enter the radius, calculate the circumference, area, sphere volume of the circle, and draw the circle.
Drag bars, input boxes, and image controls to keep the data consistent!

Fedora under test through

Copy Code code 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, calculate 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 class
Controlling updates for views and models
'''

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.notify ()

def notify (self):
For observer in Self._observers:
Observer.update ()


Class TextView:
'''
Text View class
View of working with text input boxes
'''

    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 Bar View
Handles the view of the drag bar
'''

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
Working with views of an image
'''

@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", (+), "white")
        draw = Imagedraw.draw (image)
         minor = 150-radius
        major = + radius
  & nbsp;     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 class
Responsible for the overall interface display
'''

    def textcallback (self, Widget, controller):
        ""
        Text Input callback
        '
        Try:
             radius = float (Widget.get_text ())
            Controller.setradius (RADIUS)
        except ValueError as e:
             Pass

def scalecallback (self, Widget, controller):
'''
Drag Bar 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 (0XAAAAAAAA)
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 ()



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.