-1

The following python code displays a bar graph behind two scrolledtext boxes and a number of buttons. I'm really new at doing python and I'd like to know whether it's possible to resize the graph so that:

(1) its left edge is aligned with the right edges of the scrolledtext boxes;

(2) its top is aligned with the top of button 3;

(3) its bottom is aligned with the bottom edge of the lower scrolledtext box; and,

(4) its right edge is aligned with the right edge of the screen.

import tkinter
from tkinter import Button
from tkinter import scrolledtext as tkst
import numpy as np
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure


matplotlib.use('TkAgg')

def dummy():
    return

ww = tkinter.Tk()
ww.geometry('700x400')
ww.state('zoomed')

datalst = [31, 41, 59, 26, 53, 58, 97, 96, 36]
ff = Figure(figsize=(6,6), dpi=100)
xx = ff.add_subplot(111)
ind = np.arange(len(datalst))
rects1 = xx.bar(ind, datalst, 0.8)
canvas = FigureCanvasTkAgg(ff, master=ww)
canvas.draw()
canvas.get_tk_widget().pack()

butt1 = Button(ww,text='Button 1',command=dummy, height=1,width=20,state='normal')
butt1.place(x=12, y=2)
butt2 = Button(ww,text='Button 2',command=dummy, height=1,width=20,state='disabled')
butt2.place(x=162, y=2)
butt3 = Button(ww,text='Button 3',command=dummy, height=1,width=10,state='disabled')
butt3.place(x=320, y=100)
butt4 = Button(ww,text='Button 4',command=dummy, height=1,width=10,state='disabled')
butt4.place(x=320, y=128)
butt5 = Button(ww,text='Button 5',command=dummy, height=1,width=10,state='disabled')
butt5.place(x=320, y=156)
butt6 = Button(ww,text='Button 6',command=dummy, height=1,width=10,state='disabled')
butt6.place(x=320, y=184)
butt7 = Button(ww,text='Button 7',command=dummy, height=1,width=12,state='disabled')
butt7.place(x=420, y=394)
butt8 = Button(ww,text='Button 8',command=dummy, height=1,width=12,state='disabled')
butt8.place(x=600, y=394)

disp_txt1 = tkst.ScrolledText(ww, width=36, height=12, wrap=tkinter.WORD, state='disabled')
disp_txt1.pack(fill=tkinter.BOTH)
disp_txt1.place(x=410, y=100)
disp_txt2 = tkst.ScrolledText(ww, width=36, height=5, wrap=tkinter.WORD, state='disabled')
disp_txt2.pack(fill=tkinter.BOTH)
disp_txt2.place(x=410, y=300)

ww.mainloop()

The orange rectangle below shows where I intend the graph to be:

enter image description here

qzx
  • 177
  • 3
  • 10

1 Answers1

2

To do that import tkinter as tk .Note that is always good to import tkinter this way as it doesn't import namespaces importing tkinter as tk.

The was not the error the you need to specify where you want to position the widget as pack position it at middle of the available space in the window. To do that use canvas.get_tk_widget().pack(side=tk.RIGHT) which means right hand side of the screen. You can check the documentation for the attribute.

FULL CODE

from tkinter import scrolledtext as tkst
import numpy as np
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import tkinter as tk


matplotlib.use('TkAgg')

def dummy():
    return

ww = tk.Tk()
ww.geometry('700x400')
ww.state('zoomed')

datalst = [31, 41, 59, 26, 53, 58, 97, 96, 36]
ff = Figure(figsize=(6,6), dpi=100)
xx = ff.add_subplot(111)
ind = np.arange(len(datalst))
rects1 = xx.bar(ind, datalst, 0.8)
canvas = FigureCanvasTkAgg(ff, master=ww)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.RIGHT)

butt1 = tk.Button(ww,text='Button 1',command=dummy, height=1,width=20,state='normal')
butt1.place(x=12, y=2)
butt2 = tk.Button(ww,text='Button 2',command=dummy, height=1,width=20,state='disabled')
butt2.place(x=162, y=2)
butt3 = tk.Button(ww,text='Button 3',command=dummy, height=1,width=10,state='disabled')
butt3.place(x=320, y=100)
butt4 = tk.Button(ww,text='Button 4',command=dummy, height=1,width=10,state='disabled')
butt4.place(x=320, y=128)
butt5 = tk.Button(ww,text='Button 5',command=dummy, height=1,width=10,state='disabled')
butt5.place(x=320, y=156)
butt6 = tk.Button(ww,text='Button 6',command=dummy, height=1,width=10,state='disabled')
butt6.place(x=320, y=184)
butt7 = tk.Button(ww,text='Button 7',command=dummy, height=1,width=12,state='disabled')
butt7.place(x=420, y=394)
butt8 = tk.Button(ww,text='Button 8',command=dummy, height=1,width=12,state='disabled')
butt8.place(x=600, y=394)

disp_txt1 = tkst.ScrolledText(ww, width=36, height=12, wrap=tk.WORD, state='disabled')
disp_txt1.pack(fill=tk.BOTH)
disp_txt1.place(x=410, y=100)
disp_txt2 = tkst.ScrolledText(ww, width=36, height=5, wrap=tk.WORD, state='disabled')
disp_txt2.pack(fill=tk.BOTH)
disp_txt2.place(x=410, y=300)

ww.mainloop()

enter image description here

AD WAN
  • 1,414
  • 2
  • 15
  • 28