Python Tkinter使用Canvas类创建不同类型的线

在Tkinter, Canvas.create_line()方法用于在任何画布中创建线条。这些行只能在画布上看到, 因此首先需要创建一个Canvas对象, 然后将其打包到主窗口中。
语法如下:

Canvas.create_line(x1, y1, x2, y2, ...., options = ...)

注意:创建一条线至少需要4个点, 但是你也可以添加多个点来创建不同的图形。
【Python Tkinter使用Canvas类创建不同类型的线】类参数–
使用的数据成员:主画布用于给定类的成员函数:create()方法使用的小部件:Canvas Tkinter使用的方法:canvas.create_line()pack()title()geometry()
以下是Python代码–
# Imports each and every method and class # of module tkinter and tkinter.ttk from tkinter import * from tkinter.ttk import * class GFG: def __init__( self , master = None ): self .master = master# Calls create method of class GFG self .create()def create( self ):# This creates a object of class canvas self .canvas = Canvas( self .master)# This creates a line of length 200 (straight horizontal line) self .canvas.create_line( 15 , 25 , 200 , 25 )# This creates a lines of 300 (straight vertical dashed line) self .canvas.create_line( 300 , 35 , 300 , 200 , dash = ( 5 , 2 ))# This creates a triangle (triangle can be created by other methods also) self .canvas.create_line( 55 , 85 , 155 , 85 , 105 , 180 , 55 , 85 )# This pack the canvas to the main window and make it expandable self .canvas.pack(fill = BOTH, expand = True )if __name__ = = "__main__" :# object of class Tk, resposible for creating # a tkinter toplevel window master = Tk() geeks = GFG(master)# This sets the title to Lines master.title( "Lines" )# This sets the geometry and position of window # on the screen master.geometry( "400x250 + 300 + 300" )# Infnite loop breaks only by interrupt master.mainloop()

输出如下:
Python Tkinter使用Canvas类创建不同类型的线

文章图片
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

    推荐阅读