Tk库的使用(1)

简介: # # To change this template, choose Tools | Templates# and open the template in the editor. # Sample code from Programing Ruby, page 248require 'tk'cl...
#
# To change this template, choose Tools | Templates
# and open the template in the editor.
 
# Sample code from Programing Ruby, page 248
require 'tk'

class Draw
  def do_press(x, y)
    @start_x = x
    @start_y = y
    @current_line = TkcLine.new(@canvas, x, y, x, y)
  end

  def do_motion(x, y)
    if @current_line
      @current_line.coords @start_x, @start_y, x, y
    end
  end

  def do_release(x, y)
    if @current_line
      @current_line.coords @start_x, @start_y, x, y
      @current_line.fill 'black'
      @current_line = nil
    end
  end

  def initialize(parent)
    @canvas = TkCanvas.new(parent)
    @canvas.pack
    @start_x = @start_y = 0
    @canvas.bind("1", lambda {|e| do_press(e.x, e.y)})
    @canvas.bind("B1-Motion",
                 lambda {|x, y| do_motion(x, y)}, "%x %y")
    @canvas.bind("ButtonRelease-1",
                 lambda {|x, y| do_release(x, y)},
                 "%x %y")
  end
end

root = TkRoot.new { title 'Canvas' }
Draw.new(root)
Tk.mainloop
相关文章
|
3月前
|
Python
Python TK实现的取色
Python TK实现的取色
22 0
|
3月前
|
Python
Python TK实现的日历
Python TK实现的日历
22 0
|
4月前
|
Python
tkinter之panedwindow
tkinter之panedwindow
43 0
|
4月前
|
Python
tkinter之简单使用
tkinter之简单使用
22 0
|
3月前
|
人工智能 IDE 开发工具
Python TK输入框Entry设置高度
Python TK输入框Entry设置高度
43 0
|
4月前
|
开发者 Python
【python】tkinter组件,from Tkinter import * 与 import Tkinter 的区别
【python】tkinter组件,from Tkinter import * 与 import Tkinter 的区别
53 0
|
11月前
|
前端开发 Python
Python tkinter库之Canvas 以圆模拟画圆环
Python tkinter库之Canvas 以圆模拟画圆环
154 0
|
前端开发 Python
Python内置包Tkinter的重要控件(下)
本文将接着介绍剩下的五个重要的控件,包括Canvas,Messagebox,Listbox,Checkbutton,Radiobutton。
71 0
Python内置包Tkinter的重要控件(下)
|
前端开发 Python
Python内置包Tkinter的重要控件(上)
学习了这么久的Tkinter,基本上把Tkinter的重要控件都学了一遍,本文主要对其所有重要控件以及重要函数做一个总结,加深对Tkinter的理解与应用。
84 0
Python内置包Tkinter的重要控件(上)
|
存储 人工智能 Java
Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(LabelFrame/Checkbutton/Radiobutton)
Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(LabelFrame/Checkbutton/Radiobutton)
Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(LabelFrame/Checkbutton/Radiobutton)