python提示没有canvas这个模块

Python014

python提示没有canvas这个模块,第1张

python提示没有canvas这个模块。如下为绘图不成功的代码

from tkinter import *

import math

WIDTH, HEIGHT = 510, 210

ORIGIN_X, ORIGIN_Y = 2, HEIGHT/2 #原点

SCALE_X, SCALE_Y = 40, 100 #x轴、y轴缩放倍数

ox, oy = 0, 0

x, y = 0, 0

arc = 0 #弧度

END_ARC = 360 * 2 #函数图形画两个周期

root = Tk()

c = Canvas(root, bg = 'white', width = WIDTH, height = HEIGHT)

c.pack()

c.create_text(200, 20, text = 'y = cos(x)')

c.create_line(0, ORIGIN_Y, WIDTH, ORIGIN_Y)

c.create_line(ORIGIN_X, 0, ORIGIN_X, HEIGHT)

for i in range(0, END_ARC+1, 10):

arc = math.pi * i / 180

x = ORIGIN_X + arc * SCALE_X

y = ORIGIN_Y - math.cos(arc) * SCALE_Y

c.create_line(ox, oy, x, y)

ox, oy = x, y

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

直接执行返回 0 然后没有显示图片

在这里插入图片描述

2、解决办法

在最下面加一行代码:

root.mainloop()

1

完整代码如下:

from tkinter import *

import math

WIDTH, HEIGHT = 510, 210

ORIGIN_X, ORIGIN_Y = 2, HEIGHT/2 #原点

SCALE_X, SCALE_Y = 40, 100 #x轴、y轴缩放倍数

ox, oy = 0, 0

x, y = 0, 0

arc = 0 #弧度

END_ARC = 360 * 2 #函数图形画两个周期

root = Tk()

c = Canvas(root, bg = 'white', width = WIDTH, height = HEIGHT)

c.pack()

c.create_text(200, 20, text = 'y = cos(x)')

c.create_line(0, ORIGIN_Y, WIDTH, ORIGIN_Y)

c.create_line(ORIGIN_X, 0, ORIGIN_X, HEIGHT)

for i in range(0, END_ARC+1, 10):

arc = math.pi * i / 180

x = ORIGIN_X + arc * SCALE_X

y = ORIGIN_Y - math.cos(arc) * SCALE_Y

c.create_line(ox, oy, x, y)

ox, oy = x, y

root.mainloop()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

然后即可绘制出图片

在这里插入图片描述

3、原因

root到root.pack()之间,是设计的部件的类型,尺寸,样式,位置,然后绑定一个事件。mainloop就进入到事件(消息)循环。一旦检测到事件,就刷新组件。那是网上的说法,我的理解是如果没有mainloop,它也会绘图,但是绘图完成后就关闭了程序,所以就好像没有显示一样,加上mainloop后,它会监听你的行为,有关闭它才会结束程序

year_input=int(input("请输入年份:"))

if year_input >= 1000:

....SymbolicAnimals = year_input%12

....if SymbolicAnimals == 0:

........print("{}年是:申(猴)".format(year_input))

....elif SymbolicAnimals == 1:

........print("{}年是:酉(鸡)".format(year_input))

....elif SymbolicAnimals == 2:

........print("{}年是:戌(狗)".format(year_input))

....elif SymbolicAnimals == 3:

........print("{}年是:亥(猪)".format(year_input))

....elif SymbolicAnimals == 4:

........print("{}年是:子(鼠)".format(year_input))

....elif SymbolicAnimals == 5:

........print("{}年是:丑(牛)".format(year_input))

....elif SymbolicAnimals == 6:

........print("{}年是:寅(虎)".format(year_input))

....elif SymbolicAnimals == 7:

........print("{}年是:卯(兔)".format(year_input))

....elif SymbolicAnimals == 8:

........print("{}年是:辰(龙)".format(year_input))

....elif SymbolicAnimals == 9:

........print("{}年是:巳(蛇)".format(year_input))

....elif SymbolicAnimals == 10:

........print("{}年是:午(马)".format(year_input))

....elif SymbolicAnimals == 11:

........print("{}年是:未(羊)".format(year_input))

else:

....print("请输入大于4位数的年份")

《Python灰帽子黑客与逆向工程师的Python编程之道》百度网盘pdf最新全集下载:

链接:https://pan.baidu.com/s/1QqjrTzpKBsgFisae1oXZ9w

?pwd=x4sd 提取码:x4sd

简介:本书的绝大部分篇幅着眼于黑客技术领域中的两大经久不衰的话题:逆向工程与漏洞挖掘,并向读者呈现了几乎每个逆向工程师或安全研究人员在日常工作中所面临的各种场景,其中包括:如何设计?构建自己的调试工具,如何自动化实现烦琐的逆向分析任务,如何设计与构建自己的fuzzing工具,如何利用fuzzing测试来找出存在于软件产品中的安全漏洞,一些小技巧诸如钩子与注入技术的应用,以及对一些主流Python安全工具如PyDbg、Immunity\nDebugger、Sulley、IDAPython、PyEmu等的深入介绍。作者借助于如今黑客社区中备受青睐的编程语言Python读者构建出精悍的脚本程序来——应对上述这些问题。出现在本书中的相当一部分Python代码实例借鉴或直接来源于一些的开源安全项目,诸如Pedram\nAmini的Paimei,由此读者可以领略到安全研究者?是如何将黑客艺术与工程技术优雅融合来解决那些棘手问题的。

本书适合热衷于黑客技术,特别是与逆向工程与漏洞挖掘领域相关的读者,以及所有对Python编程感兴趣的读者阅读与参考。