PySimpleGUI 图形元素
图形元素类似于Canvas,但非常强大。你可以定义你自己的坐标系,用你自己的单位工作,然后在一个用像素定义的区域内显示它们。
你应该向Graph对象提供以下值−
- 画布的大小,以像素为单位
-
你的坐标系的左下(x,y)坐标
-
你的坐标系的右上方(x,y)坐标
图形被创建,并通过调用以下与Tkinter画布相似的方法获得图形ID-
draw_arc(self, top_left, bottom_right, extent, start_angle, style=None, arc_color='black', line_width=1, fill_color=None)
draw_circle(self, center_location, radius, fill_color=None, line_color='black', line_width=1)
draw_image(self, filename=None, data=None, location=(None, None))
draw_line(self, point_from, point_to, color='black', width=1)
draw_lines(self, points, color='black', width=1)
draw_oval(self, top_left, bottom_right, fill_color=None, line_color=None, line_width=1)
draw_point(self, point, size=2, color='black')
draw_polygon(self, points, fill_color=None, line_color=None, line_width=None)
draw_rectangle(self, top_left, bottom_right, fill_color=None, line_color=None, line_width=None)
draw_text(self, text, location, color='black', font=None, angle=0, text_location='center')
除了上述绘制方法,Graph类还定义了 move_figure() 方法,通过该方法,由其ID识别的图像被移动到新的位置,给出相对于其先前坐标的新坐标。
如果你把drag_submits属性设置为True,就可以捕获图形区域内的鼠标事件。当你在图形区域的任何地方点击时,产生的事件是:Graph_key + ‘+UP’。
在下面的例子中,我们在图形元素的中心画了一个小圆。在图形对象的下面,有一些按钮用于在左、右、上、下方向移动圆。当点击时, mov_figure() 方法被调用。
运行上述程序。使用按钮来移动圆圈。
