【问题标题】:connect 2 checkbuttons with a line in python tkinter在python tkinter中用一条线连接2个checkbuttons
【发布时间】:2020-08-24 14:56:24
【问题描述】:

我有一个程序在选择选项(另一个支票禁止)时,可以将CheckButtons放置在画布中。我有另一个选项(另一个复选按钮)来画一条线。为了画线,首先我应该选择复选按钮“画一条线”,然后单击放置在画布中的任何复选按钮,然后单击画布上的任何位置。这适用于我放置的第一个检查按钮,但如果我放置多个检查按钮,它只会从画布中的最后一个检查按钮位置绘制线,而不是从我选择的检查点绘制线。 我相信我应该创建一个字典来跟踪我放置的复选按钮,以便我可以回调它们,但我不知道如何实现它,知道该怎么做吗?

    from tkinter import *

root = Tk()
top_canvas = Canvas(root,width=1376,height=768, bg='light blue')
top_canvas.pack()

buttons = []

class CMD:  #Auxilliary function for callbacks using parameters. Syntax: CMD(function, argument1, argument2, ...)
    def __init__(s1, func, *args):
        s1.func = func
        s1.args = args
    def __call__(s1, *args):
        args = s1.args+args
        s1.func(*args)

def color_checkbutton(pos):  # define the colors of the checkbutton
    checkbutton_available()
    if buttons[pos][0].get() == 1:
        buttons[pos][2].configure(bg='red')
    else:
        buttons[pos][2].configure(bg='green')

def place_checkbutton_in_canvas(e):  # order to insert the checkbutton
    if len(str(e.widget))<12: ## Don't place a new one if a checkbox was clicked
        b = IntVar()
        pos = len(buttons)
        global xx, yy
        xx = e.x
        yy = e.y
        buttons.append([b,pos, Checkbutton(top_canvas, variable=b, textvariable=b, command=CMD(color_checkbutton, pos))])
        buttons[-1][2].place(x=xx, y=yy)
        color_checkbutton(pos)

def place_checkbutton():  #to run when checkbutton is selected. Now the checkbutton will be placed where mouse clicked if choose_line is selected
    top_canvas.bind('<Button-1>', place_checkbutton_in_canvas)

def checkbutton_available():
    def drawline(ev):
        flx = ev.x
        fly = ev.y
        def auxiliary():
           lineor = top_canvas.create_line(xx, yy, flx, fly, width =3, fill = 'red')
        auxiliary()
    if chosen_option.get() == 2:
            top_canvas.bind('<Button-1>', drawline)

chosen_option = IntVar()
choose_checkbutton = Radiobutton(top_canvas, text = "place checkbutton", variable=chosen_option, value = 1, command = place_checkbutton)
choose_checkbutton.place(x=10, y=10)
choose_line = Radiobutton(top_canvas, text = "draw line", variable=chosen_option, value = 2)
choose_line.place(x=10, y=100)

top_canvas.bind('<Button-1>', place_checkbutton_in_canvas)

root.mainloop()

【问题讨论】:

    标签: python python-3.x dictionary tkinter canvas


    【解决方案1】:

    在您的代码中,您从 (xx, yy) 到 (flx, fly) 画了一条线,但您的坐标 (xx, yy) 似乎是仅在 place_checkbutton_in_canvas(e) 函数中定义的全局变量。

    所以 (xx, yy) 包含您放置的最后一个复选框的坐标。只有当您放置一个新的检查按钮时,您的行的起点才会重新定义。

    要更正此问题,您应该在选中复选框时分配 (xx, yy) 全局变量。

    另外,如果你想从多个起点画线,只有两个 int 变量(xx 和 yy)是不够的。您需要有两个整数数组(或一对数组)来存储所有当前检查值的位置。

    如果您不想同时绘制多条线,也许您应该使用单选按钮而不是复选框。

    【讨论】:

    • 我想从多个起点画线,我知道我需要使用字典或数组,但我不知道如何实现它。每次我放置一个复选按钮时,一个新条目应该进入字典。我没有看到的是如何使用变量 pos 的值创建一个新键(我用来给检查按钮一个数字)
    • 我不太确定你为什么要字典。我的第一个方法是创建一个数组,每次添加一个复选框时都将其坐标放入其中。你最终会得到类似 [(x1, y1), (x2, y2), (x3, y3), ...] 的东西,如果以后你想删除它们,因为数组中的位置可能会变得更难可能会改变。
    • 一个更高级的解决方案,不会存储您的复选框坐标并根据复选框状态添加/删除它们,而是直接存储指向所有复选框对象的指针。事实上,由于您的复选框对象已经自己存储了自己的位置,因此您可以轻松获取该信息。此外,您可以同时验证您的复选框是否被勾选。
    猜你喜欢
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 2020-09-12
    相关资源
    最近更新 更多