python计算圆周长、面积、球体体积并画出圆

yipeiwu_com5年前Python基础

输入半径,计算圆的周长、面积、球体体积,并画出这个圆。
拖动条、输入框和图像控件的数据保持一致!

Fedora下测试通过

复制代码 代码如下:

#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:
    '''
    模型类
    存储半径,计算周长、面积、体积
    '''

    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:
    '''
    控制器类
    控制视图和模型的更新
    '''

    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:
    '''
    文字视图类
    处理文本输入框的视图
    '''

    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:
    '''
    拖动条视图
    处理拖动条的视图
    '''

    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:
    '''
    图像视图
    处理图像的视图
    '''

    @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):
    '''
    主窗口类
    负责整体界面的显示
    '''

    def textCallback(self, widget, controller):
        '''
        文本输入回调
        '''
        try:
            radius = float(widget.get_text())
            controller.setRadius(radius)
        except ValueError as e:
            pass

    def scaleCallback(self, widget, controller):
        '''
        拖动条回调
        '''
        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('半径:')
        table.attach_defaults(label, 0, 1, 0, 1)
        label = Gtk.Label('周长:')
        table.attach_defaults(label, 0, 1, 1, 2)
        label = Gtk.Label('面积:')
        table.attach_defaults(label, 0, 1, 2, 3)
        label = Gtk.Label('体积:')
        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('周长')
        table.attach_defaults(self.perimeterEntry, 1, 2, 1, 2)
        self.areaEntry = Gtk.Entry.new()
        self.areaEntry.set_sensitive(False)
        self.areaEntry.set_text('面积')
        table.attach_defaults(self.areaEntry, 1, 2, 2, 3)
        self.volumeEntry = Gtk.Entry.new()
        self.volumeEntry.set_sensitive(False)
        self.volumeEntry.set_text('体积')
        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()



相关文章

Python读取一个目录下所有目录和文件的方法

本文实例讲述了Python读取一个目录下所有目录和文件的方法。分享给大家供大家参考,具体如下: 这里介绍的是刚学python时的一个读取目录的列子,给大家分享下: #!/usr/bi...

Python中低维数组填充高维数组的实现

Python中低维数组填充高维数组的实现

今天遇到这样一种业务情况: 我的图片的画布是(4,4,3)的三维数组,而得到的图片是(2,2,3)的三维数组,我要把图片放到画布的中间某个位置应该怎么做呢? 大家首先想到是遍历循环,但是...

python分析作业提交情况

python分析作业提交情况

这次做一个比较贴近我实际的东西:python分析作业提交情况。 要求:     将服务器中交作业的学生(根据文件的名字进行提取)和统计成绩的表格中...

netbeans7安装python插件的方法图解

netbeans7安装python插件的方法图解

我们可以手动来添加地址和安装。如图所示: 方法:NetBeans界面,“工具”->“插件”, 点击“设置”->点击“添加”,然后 添加一个更新中心地址 ,名称可以任意,U...

python执行使用shell命令方法分享

1. os.system(shell_command) 直接在终端输出执行结果,返回执行状态0,1 此函数会启动子进程,在子进程中执行command,并返回command命令执行完毕后的...