学习笔记(59):Python实战编程-Graphics

立即学习:https://edu.csdn.net/course/play/19711/343123?utm_source=blogtoedu

1.graphics:图形界面组件的绘制,利用的是坐标的定位来对各个组件进行相对地位置布局

 

2.graphics与thinkter的区别

1)窗口的创建上:

    win = graphics.GraphWin("四则运算",#定标题义窗口的
                            700,230#定义窗体的大小尺寸
                            )

2)组件的显示上,前者用draw后者用pack或者其他的布局函数

    graphics.Text(graphics.Point(80,50),#文本框开始绘制的坐标,一般是文本框的左上角点为开始绘制点
                  "计算数字一:"#文本框标题
                  ).draw(win)#用于表示在窗口win中显示,用draw进行显示

3)设置背景色文本信息上

    entry1.setFill("white")#给输入框设置底色
    entry1.setText("0.0")#给输入框设置初始值
    entry1.draw(win)#输入框的显示

 

3.完整代码

import graphics

def main():

    #-------------------创建窗体---------------------

    win = graphics.GraphWin("四则运算",#定标题义窗口的
                            700,230#定义窗体的大小尺寸
                            )
    #-------------------创建文本框1----------------

    graphics.Text(graphics.Point(80,50),#文本框开始绘制的坐标,一般是文本框的左上角点为开始绘制点
                  "计算数字一:"#文本框标题
                  ).draw(win)#用于表示在窗口win中显示,用draw进行显示

    # -------------------创建第一个输入框----------------
    entry1 = graphics.Entry(graphics.Point(180,50),#设置起始绘制坐标点
                            8)#设置输入框的宽度

    entry1.setFill("white")#给输入框设置底色
    entry1.setText("0.0")#给输入框设置初始值
    entry1.draw(win)#输入框的显示

    #-------------------创建文本框2----------------

    graphics.Text(graphics.Point(300,50),#文本框开始绘制的坐标,一般是文本框的左上角点为开始绘制点
                  "计算数字二:"#文本框标题
                  ).draw(win)#用于表示在窗口win中显示,用draw进行显示

    # -------------------创建第二个输入框----------------
    entry2 = graphics.Entry(graphics.Point(400,50),#设置起始绘制坐标点
                            8)#设置输入框的宽度

    entry2.setFill("white")#给输入框设置底色
    entry2.setText("0.0")#给输入框设置初始值
    entry2.draw(win)#输入框的显示

    #---------------------创建显示四则计算的文本框
    graphics.Text(graphics.Point(80,100),"加法运算:").draw(win)
    add_result_entry = graphics.Entry(graphics.Point(180,100),8)
    add_result_entry.setFill("white")
    add_result_entry.setText("0.0")
    add_result_entry.draw(win)

    graphics.Text(graphics.Point(300,100),"减法运算:").draw(win)
    sub_result_entry = graphics.Entry(graphics.Point(400,100),8)
    sub_result_entry.setFill("white")
    sub_result_entry.setText("0.0")
    sub_result_entry.draw(win)

    graphics.Text(graphics.Point(80,150),"乘法运算:").draw(win)
    mul_result_entry = graphics.Entry(graphics.Point(180,150),8)
    mul_result_entry.setFill("white")
    mul_result_entry.setText("0.0")
    mul_result_entry.draw(win)

    graphics.Text(graphics.Point(300,150),"除法运算:").draw(win)
    div_result_entry = graphics.Entry(graphics.Point(400,150),8)
    div_result_entry.setFill("white")
    div_result_entry.setText("0.0")
    div_result_entry.draw(win)

    graphics.Text(graphics.Point(180,200),"注:第二个数不可以是0,因为除数不可以为0").draw(win)

    #--------------等待事件执行--------------------
    win.getMouse()#当输入完成后单击鼠标就会自动进行计算

    #-----------------------进行四则计算并且获得显示结果-------------------
    add_result = eval(entry1.getText()) + eval(entry2.getText())
    sub_result = eval(entry1.getText()) - eval(entry2.getText())
    mul_result = eval(entry1.getText()) * eval(entry2.getText())
    div_result = eval(entry1.getText()) / eval(entry2.getText())
    add_result_entry.setText(add_result)
    sub_result_entry.setText(sub_result)
    mul_result_entry.setText(mul_result)
    div_result_entry.setText(div_result)


    win.mainloop()#窗体的显示

if __name__ == '__main__':
    main()

4.运行结果

1)运行前

2)运行后

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Use Python’s built-in features to create innovative graphics for data visualization and technical illustrations. This book goes beyond simple commands and libraries to explain how to not only display but also rotate, shade, and edit graphics for any purpose. Starting with the essential Python functions to set up a plotting space and produce 2 and 3-dimensional objects, you’ll learn how to construct more complex objects, translate and rotate them, remove hidden lines, introduce shading to add realism, and project images to visualize any dataset. The final chapter includes several worked applications in science and engineering including planetary models, which you can adapt for your own use. Written for developers who want to harness Python’s capabilities to fine-tune their images, Python Graphics covers the different commands for plotting dots, lines, arrows, or arcs, creating custom plotting grids, correcting distortions, adding text and labels to illustrations, manipulating arcs and circles, specify and use colors, and more. Armed with these techniques and core math skills, you’ll be ready to create and customize detailed technical illustrations or data visualizations. What You’ll Learn Use Python′s built-in commands for plotting, removing distortions, and rotating objects Create and edit 2D and 3D objects Develop illustrations for scientific and engineering applications Who This Book Is For Python developers looking for tips on how to create illustrations and visualizations, as well as scientists, engineers, or students using Python. It assumes familiarity with vectors, matrices, geometry and trigonometry.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有情怀的机械男

你的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值