Python calculates circle perimeter, area, sphere volume and draws a circle

Source: Internet
Author: User
Tags gtk
Enter the radius, calculate the circumference, area, and sphere volume of the circle, and draw the circle.
The data for the drag bars, input boxes, and image controls are consistent!

Fedora under test pass

The 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, 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 to 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
Working with views of text input boxes
'''

def __init__ (self, model, rentry, Pentry, Aentry, vEntry):
'''
: Type 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
Handle the view of the drag bar
'''

def __init__ (self, Model, scale):
'''
: Type 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 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", (+), "white")
Draw = Imagedraw.draw (image)
Minor = 150-radius
Major = + 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 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_de Faults (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.