用鼠标点击在tkinter中的单选按钮之间绘制线条

时间:2015-04-15 20:31:29

标签: tkinter tkinter-canvas

编程新手和tkinter新手......我希望有两列单选按钮;如果用户单击列0中的button1然后单击column2中的button3,则在单选按钮之间绘制一条线。我看到绘制线条的唯一方法是在画布上。从一侧到另一侧获得线路的最佳方法是什么?我是否在画布上制作虚拟按钮,或者单选按钮是否可以放在画布上,选择/取消选择鼠标单击是否可以控制该行的开始/结束?

from tkinter import *


root = Tk()
root.geometry("800x600")
var1 = IntVar()
var2 = IntVar()
start = None

def selLight():
   selection = "You have selected light " + str(var1.get())
   labelLight.config(text = selection)

def selScale():
   selection = "You have selected scale " + str(var2.get())
   labelScale.config(text = selection)

def onclick_handler(event):
    global start
    start = (event.x, event.y)

def onrelease_handler(event):
    global start
    if start is not None:
        x = start[0]
        y = start[1]
        event.widget.create_line(x, y, event.x, event.y)
        start = None

canvas = Canvas(root, height = 550, width = 240, bg = 'red')
canvas.grid(row = 0, column = 1, rowspan = 3)
canvas.bind("<Button-1>", onclick_handler)
canvas.bind("<ButtonRelease-1>", onrelease_handler)


lightBtn1 = Radiobutton(root, text = "Light 1", variable=var1, value = 1, command=selLight)
lightBtn1.grid(row = 0, column = 0)

lightBtn2 = Radiobutton(root, text = "Light 2", variable=var1, value = 2, command=selLight)
lightBtn2.grid(row = 1, column = 0)

lightBtn3 = Radiobutton(root, text = "Light 3", variable=var1, value = 3, command=selLight)
lightBtn3.grid(row = 2, column = 0)

scaleBtn1 = Radiobutton(root, text = "Scale 1",variable=var2, value = 1, command = selScale)
scaleBtn1.grid(row=0, column=2)

scaleBtn2 = Radiobutton(root, text = "Scale 2",variable=var2, value = 2, command = selScale)
scaleBtn2.grid(row=1, column=2)

scaleBtn3 = Radiobutton(root, text = "Scale 3", variable = var2, value = 3, command = selScale)
scaleBtn3.grid(row=2, column=2)

labelLight = Label(root)
labelLight.grid(row = 6, column = 1)

labelScale = Label(root)
labelScale.grid(row=7, column = 1)

root.mainloop()

0 个答案:

没有答案