zoukankan      html  css  js  c++  java
  • Python课程笔记(六)

    今天上课补上了上次未学完比较重点的鼠标和键盘事件,同时开始学习运用turtle进行绘图。

    本次课程的代码: https://gitee.com/wang_ming_er/python_course_learning/tree/master/7SeventhClass

    1、鼠标和键盘事件

    鼠标事件:指哪里显示位置

    from tkinter import *
    
    
    def callback(event):
        print( "clicked at:", event.x, event.y)
        s = (event.x, event.y)
        txt.set(s)
    
    win = Tk()
    win.geometry('200x120')
    win.title('鼠标事件')
    
    frame = Frame(win, width=200, height=100, bg = 'cyan')
    frame.bind("<Button-1>", callback)
    frame.pack()
    
    txt =  StringVar()
    L = Label(win, width=20, textvariable = txt)
    L.pack() 
    win.mainloop() 
    

    键盘事件:输入什么显示什么

    from tkinter import *  
      
    win = Tk()  
    win.title('键盘事件') 
    
    def key_action(event):  
        print( "pressed", repr(event.char))
        s = event.char
        txt.set(s)
      
    def callback(event):  
        L.focus_set()  
    
    txt =  StringVar()
    L = Label(win, width=20, textvariable = txt, font = 'song -36 bold',bg = 'cyan')
    L.bind("<KeyPress>", key_action)
    L.bind("<Button-1>", callback)
    L.pack()   
    
    win.mainloop()  
    

    2、Tkinter绘图

    Python Tkinter 画布(Canvas)组件和 html5 中的画布一样,都是用来绘图的。我们可以将图形,文本,小部件或框架放置在画布上。

    语法格式:

    w = Canvas ( master, option=value, ... )
    
    • master: 按钮的父容器。
    • options: 可选项,即该按钮的可设置的属性。这些选项可以用键 = 值的形式设置,并以逗号分隔。
    可选项 &描述
    bd 边框宽度,单位像素,默认为 2 像素。
    bg 背景色
    confine 如果为 true (默认), 画布不能滚动到可滑动的区域外。
    cursor 光标的形状设定,如arrow, circle, cross, plus 等
    height 高度
    highlightcolor 要高亮的颜色
    relief 边框样式,可选值为 FLAT、SUNKEN、RAISED、GROOVE、RIDGE。 默认为 FLAT。
    scrollregion 一个元组 tuple (w, n, e, s) ,定义了画布可滚动的最大区域,w 为左边,n 为头部,e 为右边,s 为底部。
    width 画布在 X 坐标轴上的大小。
    xscrollincrement 用于滚动请求水平滚动的数量值。
    xscrollcommand 水平滚动条,如果画布是可滚动的,则该属性是水平滚动条的 .set()方法。
    yscrollincrement 类似 xscrollincrement, 但是垂直方向。
    yscrollcommand 垂直滚动条,如果画布是可滚动的,则该属性是垂直滚动条的 .set()方法。
    • line − 创建线条
    line = canvas.create_line(x0, y0, x1, y1, ..., xn, yn, options)
    
    • polygon − 创建一个至少有三个顶点的多边形
    polygon = canvas.create_polygon(x0, y0, x1, y1,...xn, yn, options)
    
    • oval − 创建一个圆 (很特别,画一个内切圆
    oval = canvas.create_oval(x0, y0, x1, y1, options)
    

    画个笑脸:

    '''
       窗体中的画布示例:
        绘制笑脸
    '''
    import tkinter
    import tkinter.messagebox
    win = tkinter.Tk()
    win.title('小丑smile')
    win.geometry('250x250')  
    
    can = tkinter.Canvas(win, height=250, width=250) #定义画布
    io1 = can.create_oval(35,30,210,210, fill='yellow') #画一蓝色圆
    io2 = can.create_oval(70,70,180,180, fill='green')
    io3 = can.create_oval(65,70,185,170, outline='yellow', fill='yellow') 
    io4 = can.create_oval(80,100,110,130, fill='black') 
    io5 = can.create_oval(150,100,180,130, fill='purple')
    
    can.pack()
    win.mainloop()
    

    3、Turtle绘图

    turtle名为海龟,其爬的路径就是画的图形。

    绘图窗体:

    空间坐标:

    例如:

    还是通过实例学习,自己在网上找了一个画西瓜的程序学习学习。

    import turtle   #导入海龟模块库
    
    #初始化画笔,设置画笔属性
    t = turtle.Pen() #初始化画笔
    t.pensize(10)   #设置画笔的宽度
    
    #瓜皮-绿色
    t.begin_fill()       #准备开始填充图形
    t.fillcolor("green") #设置填充颜色为绿色
    t.circle(400, extent = 30) #画一个半径400,角度为30的弧
    t.goto(0, 400)  #设置坐标(0, 400)
    t.penup()      #抬笔
    t.home()       #将位置和方向恢复到初始状态,位置初始坐标为(0,0)
    t.pendown()    #落笔
    t.circle(400, extent = -30)#画一个半径400,反方向角度为30的弧
    t.goto(0, 400)  #设置坐标(0, 400)
    t.end_fill()   #填充完成
    
    #果肉-红色
    t.penup()#抬笔
    t.goto(0, 50)#设置坐标(0, 50)
    t.setheading(0) #把方向调为default(设置当前朝向为angle角度)
    t.pendown()#落笔
    
    t.be加粗样式gin_fill()#准备开始填充图形
    t.fillcolor("red")#设置填充颜色为红色
    t.circle(350, extent = 30)#画一个反方向半径350,角度为30的弧
    t.goto(0, 400)#设置坐标(0, 400)
    t.penup()#抬笔
    t.goto(0, 50)#设置坐标(0, 50)
    t.setheading(0) #把方向调为default(设置当前朝向为angle角度)
    t.pendown()#落笔
    t.circle(350, extent = -30)#画一个反方向半径350,角度为30的弧
    t.goto(0, 400)#设置坐标(0, 400)
    t.end_fill()#填充完成
    
    #画西瓜子函数
    def _dot(x, y, size):  #一个设置西瓜子的函数
        t.penup() #抬笔
        t.goto(x, y)# 设置坐标(x, y)
        t.pendown()# 落笔
        t.dot(size)#  画一个size大小圆点
    
    #瓜子
    _dot(0, 300, 30) #调用_dot函数画西瓜子
    _dot(50, 200, 30)
    _dot(-50, 200, 30)
    _dot(0, 100, 30)
    _dot(100, 120, 30)
    _dot(-100, 120, 30)
    

  • 相关阅读:
    asp.net后台导出excel的方法:使用response导出excel
    asp.net后台导出excel的方法一:使用response导出excel
    infragistics--web网站升级注意点
    infragistics--网站部署时webtab的tab前出现textbox
    Jewels and Stones
    To Lower Case
    Unique Email Addresses
    unique-morse-code-words
    很久没来博客园了。。。。
    Centos7 基础知识---------root文件夹下没有.ssh文件
  • 原文地址:https://www.cnblogs.com/wangzheming35/p/12613442.html
Copyright © 2011-2022 走看看