猿问

我需要设法在触摸屏上获取用户签名

我正在制作一个 POS 系统,我想包括一个购买后签名的选项。我已经尝试过使用画布的 Tkinter 方法,但是速度很慢,而且非常四四方方,有什么建议吗?


这是我现在正在使用的代码:


from tkinter import *


canvas_width = 500

canvas_height = 150


def paint( event ):

   python_green = "#476042"

   x1, y1 = ( event.x - 1 ), ( event.y - 1 )

   x2, y2 = ( event.x + 1 ), ( event.y + 1 )

   w.create_oval( x1, y1, x2, y2, fill = python_green )


master = Tk()

master.title( "Painting using Ovals" )

w = Canvas(master, 

           width=canvas_width, 

           height=canvas_height)

w.pack(expand = YES, fill = BOTH)

w.bind( "<B1-Motion>", paint )


message = Label( master, text = "Press and Drag the mouse to draw" )

message.pack( side = BOTTOM )


mainloop()

顺便说一句,这个代码不是我的,我是从这个网站上得到的


犯罪嫌疑人X
浏览 180回答 2
2回答

MMTTMM

这是一个简单的 tkinter 绘图应用程序。from tkinter import *b1 = "up"xold, yold = None, Nonedisplay_width = '500'display_height = '500'canvas_width = '500'canvas_height = '500'def main():&nbsp; &nbsp; root = Tk()&nbsp; &nbsp; root.geometry((display_width+"x"+display_height))&nbsp; &nbsp; drawing_area = Canvas(root,width=canvas_width,height=canvas_height,bg="white")&nbsp; &nbsp; drawing_area.bind("<Motion>", motion)&nbsp; &nbsp; drawing_area.bind("<ButtonPress-1>", b1down)&nbsp; &nbsp; drawing_area.bind("<ButtonRelease-1>", b1up)&nbsp; &nbsp; drawing_area.pack(side=RIGHT)&nbsp; &nbsp; root.mainloop()def b1down(event):&nbsp; &nbsp; global b1&nbsp; &nbsp; x1, y1 = ( event.x - 4 ), ( event.y - 4 )&nbsp; &nbsp; x2, y2 = ( event.x + 4 ), ( event.y + 4 )&nbsp; &nbsp; event.widget.create_oval( x1, y1, x2, y2, fill = "black" )&nbsp; &nbsp; b1 = "down"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# you only want to draw when the button is down&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # because "Motion" events happen -all the time-def b1up(event):&nbsp; &nbsp; global b1, xold, yold&nbsp; &nbsp; b1 = "up"&nbsp; &nbsp; xold = None&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# reset the line when you let go of the button&nbsp; &nbsp; yold = Nonedef motion(event):&nbsp; &nbsp; if b1 == "down":&nbsp; &nbsp; &nbsp; &nbsp; global xold, yold&nbsp; &nbsp; &nbsp; &nbsp; x1, y1 = ( event.x - 4 ), ( event.y - 4 )&nbsp; &nbsp; &nbsp; &nbsp; x2, y2 = ( event.x + 4 ), ( event.y + 4 )&nbsp; &nbsp; &nbsp; &nbsp; event.widget.create_oval( x1, y1, x2, y2, fill = "black" )&nbsp; &nbsp; &nbsp; &nbsp; if xold is not None and yold is not None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; python_green = "#476042"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x1, y1 = ( event.x - 4 ), ( event.y - 4 )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x2, y2 = ( event.x + 4 ), ( event.y + 4 )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.widget.create_oval( x1, y1, x2, y2, fill = "black" )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.widget.create_line(xold,yold,event.x,event.y,smooth=TRUE,width=9)&nbsp; &nbsp; # here's where you draw it. smooth. neat.&nbsp; &nbsp; &nbsp; &nbsp; xold = event.x&nbsp; &nbsp; &nbsp; &nbsp; yold = event.yif __name__ == "__main__":&nbsp; &nbsp; main()

互换的青春

一个干净的例子:import tkinter as tkclass Signature(tk.Canvas):&nbsp; &nbsp; def __init__(self, *args, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; self.thickness = kwargs.pop('thickness', 4)&nbsp; &nbsp; &nbsp; &nbsp; tk.Canvas.__init__(self, *args, **kwargs)&nbsp; &nbsp; &nbsp; &nbsp; self._xold = None&nbsp; &nbsp; &nbsp; &nbsp; self._yold = None&nbsp; &nbsp; &nbsp; &nbsp; self.bind('<B1-Motion>', self._on_motion)&nbsp; &nbsp; def _on_motion(self, event):&nbsp; &nbsp; &nbsp; &nbsp; x1, y1 = ( event.x - self.thickness ), ( event.y - self.thickness )&nbsp; &nbsp; &nbsp; &nbsp; x2, y2 = ( event.x + self.thickness ), ( event.y + self.thickness )&nbsp; &nbsp; &nbsp; &nbsp; event.widget.create_oval( x1, y1, x2, y2, fill='black' )&nbsp; &nbsp; &nbsp; &nbsp; if self._xold is not None and self._yold is not None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.create_oval( x1, y1, x2, y2, fill='black' )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.create_line(self._xold,self._yold,event.x,event.y,smooth=True,width=self.thickness*2+1)&nbsp; &nbsp; # here's where you draw it. smooth. neat.&nbsp; &nbsp; &nbsp; &nbsp; self._xold = event.x&nbsp; &nbsp; &nbsp; &nbsp; self._yold = event.yif __name__ == '__main__':&nbsp; &nbsp; canvas_width = '500'&nbsp; &nbsp; canvas_height = '500'&nbsp; &nbsp; root = tk.Tk()&nbsp; &nbsp; sig = Signature(root, width=canvas_width,height=canvas_height,bg='white', thickness=1)&nbsp; &nbsp; sig.pack()&nbsp; &nbsp; root.mainloop()请注意,这会在事件的位置绘制一个椭圆形,并绘制一条线将其与最后一个事件连接起来,这有助于平滑线。
随时随地看视频慕课网APP

相关分类

Python
我要回答