Reputation: 414
I am trying to do something like figure1 but I don't understand what is wrong. When I run the code figure 2 appears which I want it to look like the first picture. Can someone help me please ? Thanks
import math
import Tkinter
import tkMessageBox
import Canvas
main = Tkinter.Tk()
main.geometry("1500x1000")
main.title("Momentum")
canvas = Tkinter.Canvas(main, width=1000, height =600)
canvas.grid(row=0,column=0)
box = canvas.create_rectangle(150,20,850,400)
ol1= Tkinter.Label(main, text="Object A")
ol2= Tkinter.Label(main,text="Object B")
ml1 = Tkinter.Label(main, text ="MASS")
ml2 = Tkinter.Label(main, text ="MASS")
me1 = Tkinter.Entry(main)
me2 = Tkinter.Entry(main)
ol1.grid(row=550, column=300)
ol2.grid(row=550, column=500)
ml1.grid(row=600, column =300 )
ml2.grid(row =600, column =500 )
me1.grid(row=620, column =300)
me2.grid(row=620, column=500)
main.mainloop()
Upvotes: 0
Views: 1223
Reputation: 385870
It's impossible to give you a perfect answer, because widget layout involves more than just knowing where widgets go. You also need to consider what Tkinter should do with extra space, which you haven't specified.
Part of the problem is that you seem to think grid will allocate space for rows and columns that are empty, and that grid cells are uniform in size. It will not, and they are not.
You can put something at row 1, row 100, row 1000, or row 100000 and it will visually look the same if all of the other rows are empty. Each row and column can be zero pixels tall, 1000 pixels tall, or any other size you want. Tkinter is really good at computing row and column heights and widths, so you don't have to do much calculating for yourself. Empty rows and columns with a positive weight are useful for fillers and spacers.
Overall, it looks like all you need are two columns, with four or five rows. The canvas should span both of the columns, I'm assuming.
Here's something to get you started. You can play with the row and column weights and the padding value to adjust all of the empty space to your liking.
import Tkinter
main = Tkinter.Tk()
main.geometry("1500x1000")
main.title("Momentum")
main.grid_rowconfigure(5, weight=1)
main.grid_columnconfigure(0, weight=1)
main.grid_columnconfigure(1, weight=1)
canvas = Tkinter.Canvas(main, width=1000, height=600)
ol1= Tkinter.Label(main, text="Object A")
ol2= Tkinter.Label(main,text="Object B")
ml1 = Tkinter.Label(main, text ="MASS")
ml2 = Tkinter.Label(main, text ="MASS")
me1 = Tkinter.Entry(main)
me2 = Tkinter.Entry(main)
canvas.grid(row=0,column=0, columnspan=2)
ol1.grid(row=1, column=0, pady=(0,20))
ol2.grid(row=1, column=1, pady=(0,20))
ml1.grid(row=2, column=0)
ml2.grid(row=2, column=1)
me1.grid(row=3, column=0)
me2.grid(row=3, column=1)
box = canvas.create_rectangle(150,20,850,400, outline="blue", fill="white")
main.mainloop()
Upvotes: 1
Reputation:
Using row and columns is pretty straight forward http://effbot.org/tkinterbook/grid.htm You should post what else you have tried and why it is not what you want.
import Tkinter
main = Tkinter.Tk()
main.geometry("1500x1000")
main.title("Momentum")
canvas = Tkinter.Canvas(main, width=1000, height =600)
canvas.grid(row=0,column=1, columnspan=2)
box = canvas.create_rectangle(150,20,850,400)
ol1= Tkinter.Label(main, text="Object A")
ol2= Tkinter.Label(main,text="Object B")
ml1 = Tkinter.Label(main, text ="MASS")
ml2 = Tkinter.Label(main, text ="MASS")
me1 = Tkinter.Entry(main)
me2 = Tkinter.Entry(main)
ol1.grid(row=1, column=1)
ol2.grid(row=1, column=2)
ml1.grid(row=2, column =1 )
ml2.grid(row =2, column = 2)
me1.grid(row=3, column =1)
me2.grid(row=3, column=2)
main.mainloop()
Upvotes: 1