간단한 페인트 프로그램을 작성했으며 작업에 문제가 있습니다. 그려진 선을 삭제하거나 제거 할 수 있기를 원합니다. ButtonPress-1이 작동 중지되면 ButtonRelease-1이 트리거 될 때까지 (ButtonPress-1이 가동 중임) 라인을 작성합니다. 'Reset'버튼을 누르면 라인이 삭제/제거되기를 원합니다. 나는 현재 그것을 가지고 있기 때문에 'Reset'을 누르면 프로그램이 스스로 재시작한다. 이것은 나쁜 해결책이다.Python tkinter delete event.widget.create_line
from Tkinter import *
import tkMessageBox
import sys
import os
import subprocess
b1 = "up"
xold, yold = None, None
color= "black"
linesize = 2
def main():
global root
root = Tk()
global drawing_area
drawing_area = Canvas(root, width=1050, height=1200, background="white")
drawing_area.pack()
drawing_area.bind("<Motion>", motion)
drawing_area.bind("<ButtonPress-1>", b1down)
drawing_area.bind("<ButtonRelease-1>", b1up)
button1 = Button(root, text = "Reset", command = restart_program, anchor = N)
button1.configure(width = 3, background = "#FFFFFF", relief = FLAT)
button1_window = drawing_area.create_window(640, 0, anchor=N, window=button1)
root.geometry('1050x1200')
root.geometry('+0+720')
root.mainloop()
def restart_program():
python = sys.executable
os.execl(python, python, * sys.argv)
def b1down(event):
global b1
b1 = "down"
def b1up(event):
global b1, xold, yold
b1 = "up"
xold = None
yold = None
def motion(event):
if b1 == "down":
global xold, yold
if xold is not None and yold is not None:
event.widget.create_line(xold,yold,event.x,event.y,smooth=TRUE,fill = color, width=linesize)
xold = event.x
yold = event.y
if __name__ == "__main__":
main()
완벽하게 작동합니다. –
안녕하세요. 이 프로그램에서도 실행 취소 및 다시 실행 기능을 만들려고합니다. 삭제를 위해 여기에서 사용한 것과 같은 방법을 시도했지만 작동하지 않습니다. 그것에 대해 많은 문서를 찾을 수 없습니다. 위와 같은 시도를했지만 canvas.edit_undo ("line") 'function'을 사용해 보았습니다. 당신은 tkinter에 대한 좋은 지식을 가지고있는 것 같습니다. 아마도 당신은 약간의 조언을 얻을 수 있습니다. 또한 답변 할 필요는 없지만 도움이 필요하거나 도움을 줄 수없는 경우 회신을 남겨주세요. –
제 답변에 대한 수정 내용을 참조하십시오. –