Reputation: 707
I'm trying to make a window in tkinter that will show the results of my calculations. It should have a graph and some small tables. I can't arrange them the way I need them. Below is the code for examples. In this case, I show only one graph and one table.
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import tkinter as tk
from tkinter import ttk
import numpy as np
import pandas as pd
df1 = pd.DataFrame(np.random.randint(0,30,size=(15, 3)), columns=list('ABC'))
df1['index'] = df1.index
l1 = [1,2,3,4,5]
l2 = [6,7,8,9,10]
l3 = [100,200,300,400,500]
root = tk.Tk()
lf = ttk.Labelframe(root, text='Results')
lf.grid(row=0, column=0, sticky='nwes', padx=3, pady=3)
fig = Figure(figsize=(10,5), dpi=70)
ax = fig.add_subplot(111)
df1.plot(x='index', y=['A', 'B', 'C'], ax=ax)
canvas = FigureCanvasTkAgg(fig, master=lf)
canvas.show()
canvas.get_tk_widget().grid(row=0, column=0)
label = ttk.Label(root, text= 'Res_values')
label.config(font=("Courier", 20))
label.place(x = 810, y = 70)
li1 = l1
li2 = l2
li3 = l3
list1 = tk.Listbox(root)
list2 = tk.Listbox(root)
list3 = tk.Listbox(root)
for item in li1:
list1.insert(0,item)
for item in li2:
list2.insert(0,item)
for item in li3:
list3.insert(0,item)
list1.grid(row=0, column=1)
list2.grid(row=0, column=2)
list3.grid(row=0, column=3)
root.mainloop()
The result of the code execution looks like this:
I would like the table to be placed higher and closer to the graph and the columns become smaller.
Like that
Is it possible to do this?
UPD I changed the parts of the code where it is indicated where to place the table
label = ttk.Label(root, text= 'Res_values')
label.config(font=("Courier", 20))
label.grid(row=0, column = 1, columnspan=3, sticky='N')
And
list1.grid(row=1, column=1, sticky='E'+'W')
list2.grid(row=1, column=2, sticky='E'+'W')
list3.grid(row=1, column=3, sticky='E'+'W')
The result looks like this:
Could anyone please explain what I'm doing wrong?
Upvotes: 0
Views: 1248
Reputation: 1857
You should only use one geometry manager, i.e. just grid
, not place
as well as grid
. This will also ensure, when changing the layout of boxes such as what you want, nothing will overlap.
To 'raise' your list-boxes to the top, you can add sticky=N
as an argument when you call grid
on them. This would look like list1.grid(row=0, column=1, sticky=tk.N)
.
To keep your "Res_values" text above the list-boxes, you can add it as the first row (and change the list-boxes to the second row), and make its column-span argument 3, so it centers in the middle (You may have to add sticky=tk.E+tk.W
here).
Upvotes: 1