Reputation: 71
I want to have a canvas in a grid BUT I want to separate the input and labels from the canvas, how so? that I can place the canvas without interfering the inputs and label so it will look "cute". Right now if I place the canvas in lets say row 0,3 then there will be a huge space and if there is another widget around it will either leave a big white space or make the widget long. I would like to place the canvas in the right and the other widgets in the left without leaving a big space anywhere
Any help would be appreciated
import tkinter as tk
from tkinter import *
class Gui():
def __init__(self, root):
self.root=root
self.entry = tk.Entry(root)
stvar=tk.StringVar()
stvar.set("one")
self.option=tk.OptionMenu(root, stvar, "one", "two", "three")
self.canvas=tk.Canvas(root, width=300, height=200, background='white')
self.canvas.grid(row=0,column=2)
label1=Label(self.root, text="Figure").grid(row=0,column=0, sticky="nw")
label2=Label(self.root, text="X").grid(row=1,column=0, sticky="w")
label3=Label(self.root, text="Y").grid(row=2,column=0, sticky="w")
self.option.grid(row=0,column=1,sticky="nwe")
entry = Entry(self.root).grid(row = 1,column = 1,sticky = E+ W)
entry1 = Entry(self.root).grid(row = 2,column = 1, sticky = E)
Button1=Button(self.root,text="Draw").grid(row = 3,column = 1, sticky = "we")
figure1=self.canvas.create_rectangle(80, 80, 120, 120, fill="blue")
#Grid.columnconfigure(self.root,1,weight=1, size=200)
if __name__== '__main__':
root=tk.Tk()
gui=Gui(root)
root.mainloop()
Upvotes: 3
Views: 40398
Reputation: 76194
You could create a Frame
and place all of your non-canvas widgets in that. Their placement in the frame's grid will ignore the positioning of widgets outside the frame.
import tkinter as tk
from tkinter import *
class Gui():
def __init__(self, root):
self.root=root
self.entry = tk.Entry(root)
stvar=tk.StringVar()
stvar.set("one")
self.canvas=tk.Canvas(root, width=300, height=200, background='white')
self.canvas.grid(row=0,column=1)
frame = Frame(self.root)
frame.grid(row=0,column=0, sticky="n")
self.option=tk.OptionMenu(frame, stvar, "one", "two", "three")
label1=Label(frame, text="Figure").grid(row=0,column=0, sticky="nw")
label2=Label(frame, text="X").grid(row=1,column=0, sticky="w")
label3=Label(frame, text="Y").grid(row=2,column=0, sticky="w")
self.option.grid(row=0,column=1,sticky="nwe")
entry = Entry(frame).grid(row = 1,column = 1,sticky = E+ W)
entry1 = Entry(frame).grid(row = 2,column = 1, sticky = E)
Button1=Button(frame,text="Draw").grid(row = 3,column = 1, sticky = "we")
figure1=self.canvas.create_rectangle(80, 80, 120, 120, fill="blue")
#Grid.columnconfigure(self.root,1,weight=1, size=200)
if __name__== '__main__':
root=tk.Tk()
gui=Gui(root)
root.mainloop()
Result:
Upvotes: 6