在Tkinter网格上绘图

时间:2016-07-07 09:42:02

标签: python tkinter grid

我找不到如何使用网格绘制线条的方法。我希望有一条线从北到南分隔左右框架。

    self.left= Frame(self.tk, bg="black")
    self.left.grid(column=0, row = 0, pady=5 ,padx=10, sticky=N)

    self.center = Frame (self.tk ,bg= "black")
    self.center.grid(column=1, row = 0, pady=5,padx=10, sticky=N)

    self.right= Frame(self.tk, bg="black")
    self.right.grid(column=2, row = 0, pady=5,padx=10, sticky=N)

我想要像

这样的东西
self.w.create_rectangle(self.centerwidth/2-2, 0, centerwidth/2+2, self.windowheigh, fill="#00CC00", outline = "#00CC00") 

2 个答案:

答案 0 :(得分:2)

如果要将左框架与右框架分开,可以使用ttk模块中的分隔符(http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-Separator.html

以下是一个例子:

# python3
import tkinter as tk
from tkinter.ttk import Separator, Style

# for python2 : 
# import Tkinter as tk
# from ttk import Separator

fen = tk.Tk()

left = tk.Frame(fen, bg="black",width=100, height=100)
# to prevent the frame from adapting to its content :
left.pack_propagate(False)
tk.Label(left, text="Left frame", fg="white", bg="black", anchor="center", justify="center").pack()
left.grid(column=0, row = 0, pady=5 ,padx=10, sticky="n")
sep = Separator(fen, orient="vertical")
sep.grid(column=1, row=0, sticky="ns")

# edit: To change the color of the separator, you need to use a style
sty = Style(fen)
sty.configure("TSeparator", background="red")

right= tk.Frame(fen, bg="black",width=100, height=100)
right.pack_propagate(False)
tk.Label(right, text="Right frame", fg="white", bg="black").pack()
right.grid(column=2, row = 0, pady=5,padx=10, sticky="n")

fen.mainloop()

example

答案 1 :(得分:1)

我不知道你想要什么,但你可以创建这样的一行。

from Tkinter import *

master = Tk()

w = Canvas(master, width=200, height=100)
w.pack()

w.create_line(100, 0, 100, 100)
#first 2 args are starting point of line and next 2 are ending point of line.

mainloop()

有关添加其他选项的信息,请参阅Sets.cartesianProduct(List<? extends Set<? extends B>>)