Skip to content Skip to sidebar Skip to footer

Drawing On Tkinter Grid

I couldn't find a way how to Draw line using grid. I want to have a line going from North to South seperating left and right frames. self.left= Frame(self.tk, bg='black') s

Solution 1:

If you want to separate the left frame from the right one, you can use a separator from ttk module (http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-Separator.html)

Here is an example:

# 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

Solution 2:

I don't know what you want exactly, but you can create a line like this.

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()

For adding other options, refer to canvas widget of tkinter

Post a Comment for "Drawing On Tkinter Grid"