python tkinter pack 同一行_用python tkinter中的一行连接2个复选按钮
我有一個程序在畫布中放置復選按鈕,當選項(另一個復選按鈕)被選中時。我有另一個選項(另一個復選按鈕)來畫線。為了畫線,首先我應該選擇checkbutton“draw a line”,然后單擊放置在畫布中的任何checkbutton,另一個單擊畫布上的任何地方。這個工作與我放置的第一個復選按鈕,但如果我放置了幾個復選按鈕,它只從畫布中的最后一個復選按鈕的地方畫線,而不從我選擇的檢查點畫線。我相信我應該創建一個字典來記錄我放置的復選按鈕,這樣我就可以回調它們了,但是我不知道如何實現它,有什么想法嗎?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('', 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('', 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('', place_checkbutton_in_canvas)
root.mainloop()
總結
以上是生活随笔為你收集整理的python tkinter pack 同一行_用python tkinter中的一行连接2个复选按钮的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 吉利发射9颗卫星上天!CEO:我们野心没
- 下一篇: 重启后第二批!60款国产网络游戏获批:腾