I am trying to keep a horizontal scrollbar to a canvas with an image to view the image which I want to stretch horizontally. When I grid the scrollbar, it displays the content in a different way. Here is the Tkinter code,
import os
import tkinter as tk
import sys
from ttkwidgets import CheckboxTreeview
from PIL import ImageTk, Image
import PIL as pl
basePath = r'C:/'
class App(object):
def __init__(self, master, path):
self.nodes = dict()
frame = tk.Frame(master)
self.tree = CheckboxTreeview(frame ,height=28)
self.tree.column("#0", width=300)
self.tree.heading('#0', text='Select files', anchor='n')
ysb = tk.Scrollbar(frame, orient='vertical', command=self.tree.yview)
xsb = tk.Scrollbar(frame, orient='horizontal', command=self.tree.xview)
self.tree.configure(yscroll=ysb.set, xscroll=xsb.set)
self.tree.grid()
ysb.grid(row=0, column=1, sticky='ns')
xsb.grid(row=1, column=0, sticky='ew')
frame.grid(row=0,column=0,sticky='w')
frame2.grid(row=0,column=2,sticky='n')
canvas = tk.Canvas(master,bg="lightblue",height=300, width=680)
canvas.grid(row=0, column=2, sticky='s')
self.img1 = Image.open(("logos/pic.jfif"))
self.img1 = self.img1.resize((1000,100))
self.img1 = ImageTk.PhotoImage(self.img1)
canvas.create_image(101, 101,anchor='nw',image=self.img1)
canvasHorizontalScroll = tk.Scrollbar(canvas,orient = "horizontal",command = canvas.xview)
#canvasHorizontalScroll.grid(row=1, column=0, sticky='ew') #this is where the scrollbar is being placed in the canvas
abspath = os.path.abspath(path)
self.insert_node('', abspath, abspath)
self.tree.bind('<<TreeviewOpen>>', self.open_node)
def insert_node(self, parent, text, abspath):
node = self.tree.insert(parent, 'end', text=text, open=False)
if os.path.isdir(abspath):
self.nodes[node] = abspath
self.tree.insert(node, 'end')
def open_node(self, event):
node = self.tree.focus()
abspath = self.nodes.pop(node, None)
if abspath:
self.tree.delete(self.tree.get_children(node))
for p in os.listdir(abspath):
self.insert_node(node, p, os.path.join(abspath, p))
if __name__ == '__main__':
root = tk.Tk()
root.geometry("1000x600")
global frame2
frame2 = tk.Frame(root,bg="grey60",height=300,width=680)
label2 = tk.Label(root,text='frame2',bg="grey60").place(x=340, y=10)
app = App(root, path=basePath)
root.mainloop()
And the output is something like this,
But when I uncomment the line to place the scrollbar with grid and then view the result, It is something different like this,
It pretty much hides the canvas and keeps a small scrollbar at the bottom. I could keep the horizontal scrollbar for the treeview frame but I couldn't do the same for this canvas. I just want a horizontal scrollbar to scroll and view the image in the canvas till the image ends