Drawing on Tkinter grid

2019-07-31 15:58发布

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

I want something like

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

2条回答
Animai°情兽
2楼-- · 2019-07-31 16:15

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

查看更多
ゆ 、 Hurt°
3楼-- · 2019-07-31 16:19

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

查看更多
登录 后发表回答