阿平学习Python的第三次笔记吖

Day 3

第四章 对象和图形
1.Python自带的标准gui模块名为Tkinter,tkinter是最易用的gui框架之一,Python是开发真实世界gui的极好语言。图形库(graphics.py)是tkinter的一层包装。
2.第一步:导入graphics模板:

 >>>import graphics
 >>>

如若导入失败,则意味着Python找不到graphics板块,应确保文件放在正确的文件夹中然后重试。
第二步:用graphics模板在屏幕上创建一个图形窗口,即GraphWin,来显示图形。

>>>win =graphics.GraphWin()
>>>

GraphWin是一个对象,我们把它赋给变量win,可通过这个窗口变量来操作窗口对象,例如销毁窗口时,通过发出close命令做到:

>>>win.close()
>>>

3.图形模块中有很多对象,其中最简单的对象是Point(点),Point的用法:

>>>p=Point(20,30)
>>>p.getX()
>>>20
>>>p.getY()
>>>30
>>>win=GraphWin()
>>>p.draw(win)
>>>p2=Point(140,100)
>>>p2.draw(win)

第一行创建了一个位于(20,30)的Point,其坐标可以通过getX和getY来得到。用draw操作将点绘制到窗口中。这个例子创建了两个不同的对象(p和p2),并绘制到GraphWin对象中,其名为win。
除了点外,还有一些命令用于绘制线段、圆、矩形、椭圆、多边形和文本。
线段:

>>>line=Line(Point(20,30),Point(60,90))
>>>line.draw(win)

圆:

>>>center=Point(100,100)
>>>circ=Cicle(center,30)
>>>circ.setFill(‘red‘)
>>>circ.draw(win)

文本:

>>>lable=Text(center,“Red Circle”)
>>>lable.draw(win)

矩形:

>>>rect=Rectangle(Point(20,30),Point(70,70))
>>>race.draw(win)

椭圆:

>>>oval=Oval(Point(20,120),Point(180,199))
>>>oval.draw(win)

4.创建一个类的新实例,我们使用一个新的函数叫做构造函数,一般表达式如下:<class-name>(<param1>,<prama2>....)
这里的class-name是我们要创建的一个新实例的类的名称,例如Circle和Point。括号中的是初始化对象所需要的任何参数,参数的数量和类型取决于该类。如下一个构造函数语句:p=Point(20,30)
对象的方法,我们通过点表示法来调用方法:

<object>.<method-name>(<prama1>,<prama2>...0

如:p.getX()
改变对象状态的方法-move,一般格式:move(dx,dy),让对象在x方向移动x个单位,在y方向上移动个单位。
例:p.move(10,1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用第三方,比如 JSON-lib、Jackson 等来实现 XML 字符串到 JSON 字符串的转换。 具体的做法如下: 1. 先将 XML 字符串转换为 org.w3c.dom.Document 对象。 2. 使用 Jackson 的 XmlMapper 将 Document 对象映射为 JSON 对象。 3. 最后使用 Jackson 的 ObjectMapper 将 JSON 对象转换为 JSON 字符串。 代码示例: ``` import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.xml.sax.SAXException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.fasterxml.jackson.databind.ObjectMapper; public class XmlToJson { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, JsonProcessingException { String xmlString = "<foo>\n" + " <bar>\n" + " <type foobar=\"test\">test type</type>\n" + " <value>test value</value>\n" + " </bar>\n" + " <bar>\n" + " <type>test type2</type>\n" + " <value>test value2</value>\n" + " </bar>\n" + "</foo>"; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(xmlString))); XmlMapper xmlMapper = new XmlMapper(); JsonNode jsonNode = xmlMapper.readTree(document); ObjectMapper objectMapper = new ObjectMapper(); String jsonString = objectMapper.writeValueAsString(jsonNode); System.out.println(jsonString); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值