【问题标题】:Python GUI and TKinter GridPython GUI 和 TKinter 网格
【发布时间】:2014-05-11 12:59:50
【问题描述】:

我是 Python 新手,我正在尝试编写一个显示 8x8 网格的程序。垂直线应该是红色的,水平线应该是蓝色的。但我似乎无法弄清楚。我知道它必须在一个循环中,但我什至不确定从哪里开始。请帮忙!

到目前为止,这是我的代码:

从 tkinter 导入 *

class Canvas:
def __init__(self):
    self.window = Tk()
    self.window.title("Grid")

    self.canvas = Canvas(window, width = 200, height = 200,
                         bg = "white")
    self.canvas.pack()

def drawGrid(self):
    self.canvas.create_line()

谢谢!

【问题讨论】:

    标签: python user-interface tkinter


    【解决方案1】:

    查看http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_line-method 了解有关create_line() 方法的详细信息。

    您需要两个参数:线的坐标和填充颜色。坐标是[x0, y0, x1, y1] 的列表,它们对应于以父小部件左上角为原点的像素值,因此要在您的 200x200 Canvas 上绘制一条水平绿线,您可以这样写:

    self.canvas.create_line(0,0,200,200, fill='green')
    

    要创建线条网格,可以使用 for 或 while 循环,它会在每次迭代时修改坐标列表,并在每次循环结束时将其传递给新的 create_line() 函数。

    这应该让你开始。

    【讨论】:

    • 由于某种原因,当我输入 self.canvas.create_line(0,0,200,200, fill = "blue", tags = "line") 它不会在画布上绘制它。我做错了什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多