Tkinter scrollbar not appearing - python-3.x

I can't seem to incorporate a scrollbar into my code using .grid().
root = Tk()
root.geometry('%sx%s' % (GetSystemMetrics(0), GetSystemMetrics(1)))
frame_main = Frame(root)
frame_main.grid(sticky='news')
#add widgets
canvas = Canvas(frame_main)
canvas.grid(row=0, column=0, sticky="news")
vsb = Scrollbar(frame_main, orient="vertical", command=canvas.yview)
vsb.grid(row=1, column=12, sticky='nse')
canvas.configure(yscrollcommand=vsb.set)
canvas.config(scrollregion=canvas.bbox("all"))
The whole output is moved toward the bottom-left of the screen, and I cannot see a scrollbar.

Use background in Frame(root, bg='red') and you will see
Canvas is in row 0 but Scrollbar is in row 1

Related

Why my tkinter frame is not on the top of the canvas and canvas can still be scrolled up?

I'm creating a GUI tool and while working on it, I faced an issue that I couldn't figure out why it is happening.
I have a scrollable frame inside a canvas, however that frame is not on the top side of the canvas "even though I want it on the top side" and I noticed that the canvas can still be scrolled up above the frame (frame background is green) which is I consider a wrong behavior.
It doesn't matter how many times, I checked the code and revised/edited it, I still cannot figure it out, so I decided to check here for any hints and thank you in advance.
My code is as follow
import tkinter.ttk as ttk
from tkinter import *
root = Tk()
root.title("Checklist Buddy")
root.config(padx=10, pady=5)
root.geometry("800x500")
top_frame = Frame(root, bg="black")
top_frame.grid(row=0, column=0, sticky="NSEW")
mf = Frame(root, bg="brown")
mf.grid(row=1, column=0, sticky="NEWS")
canvas = Canvas(mf, bg="yellow")
canvas.grid(row=0, column=0, sticky="NEWS")
yscrollbar = ttk.Scrollbar(mf, command=canvas.yview)
yscrollbar.grid(row=0, column=1, sticky="ns")
canvas.bind('<Configure>', lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
canvas.configure(yscrollcommand=yscrollbar.set)
bpo_frame = Frame(canvas, background="green")
win = canvas.create_window((0, 0), window=bpo_frame, height=100)
def _on_mousewheel(event):
canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
def onCanvasConfigure(event):
canvas.itemconfigure(win, width=event.width)
canvas.bind('<Configure>', onCanvasConfigure, add="+")
canvas.bind_all("<MouseWheel>", _on_mousewheel)
root.columnconfigure("all", weight=1)
root.rowconfigure("all", weight=1)
mf.columnconfigure(0, weight=1)
# mf.rowconfigure("all", weight=1)
root.mainloop()
Below is a picture to show what I mean, there shall be no empty yellow space above the green window (no scroll up shall be there as the window shall be on the top side)
The main issue here is the scrollregion of the canvas, which is set to a region with negative y-coordinates. If you want the point (0,0) to be at the top of the canvas, you need to define it. And don't change it once the canvas is reconfigured.
Also, the canvas window needs an anchor in the top left corner. Otherwise, it is centered on (0,0) and reaches half into the negative x and y coordinates.
If you change the lines 15-25 as follows, the behavior is as expected:
(Personally, I would also set the width of the window equal to the width of the canvas before scrolling the first time instead of binding this change to the <Configure>. But you may have a reason for this)
canvas = Canvas(mf, bg="yellow",scrollregion=(0, 0, 1000, 1000))
canvas.grid(row=0, column=0, sticky="NEWS")
yscrollbar = ttk.Scrollbar(mf, command=canvas.yview)
yscrollbar.grid(row=0, column=1, sticky="ns")
#canvas.bind('<Configure>', lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
canvas.configure(yscrollcommand=yscrollbar.set)
bpo_frame = Frame(canvas, background="green")
win = canvas.create_window((0, 0), window=bpo_frame, height=100, anchor=NW)

How to span a Tkinter frame inside of a Tkinter Canvas

I have just started using Tkinter and I have a frame insdie of a canvas placed using the create_window funcion so that I may have a working scrollbar for my program. The issue is the Text widgets that I place in the frame inside of the canvas is not spanning horizontally inside the canvas and I have no idea how to make this happen. I have tried using pack() or grid() and grid_rowconfigure, but if I don't use create_window then the bar on my scroll bar disappears. I can't seem to find any post that addresses this issue, but I may be wrong. Here is the snippet of my code that deals with the GUI of this window:
root = Tk()
# create top part
top=Frame(root)
# create a canvas to scroll
holder = Canvas(top)
# create scroll bar
scroll = Scrollbar(top, orient=VERTICAL, command=holder.yview)
# configure scrollbar for canvas
holder.configure(yscrollcommand=scroll.set)
holder.bind('<Configure>', lambda e: holder.configure(scrollregion=holder.bbox("all")))
# create frame for content inside canvas and add it to the canvas
content = Frame(holder, relief=RAISED)
holder.create_window((0,0), window=content, anchor='nw')
# create bottom part
bottom = Frame(root, relief=SUNKEN)
root.rowconfigure(0, weight=18)
root.rowconfigure(1, weight=1, minsize=50)
root.columnconfigure(0, weight=1)
holder.pack(side=LEFT, fill=BOTH, expand=1)
scroll.pack(side=RIGHT, fill=Y)
top.grid(row=0, column=0, sticky='NSEW')
bottom.grid(row=1, column=0, sticky='NSEW')
num=0
for site in sites:
temp=Text(content, height = 5)
temp.configure(state= DISABLED)
temp.pack(fill=X, side=TOP, padx= 5, pady= 5)
siteBoxes.append(temp)
num += 1
root.mainloop()
First, you should update scrollregion of holder whenever the internal frame content (not the canvas holder) is resized.
Second, you can update the width of the internal frame content whenever the canvas holder is resized.
Below is the updated code:
root = Tk()
# create top part
top=Frame(root)
# create a canvas to scroll
holder = Canvas(top)
# create scroll bar
scroll = Scrollbar(top, orient=VERTICAL, command=holder.yview)
# configure scrollbar for canvas
holder.configure(yscrollcommand=scroll.set)
### --- expand the width of the internal frame to the width of canvas
holder.bind('<Configure>', lambda e: holder.itemconfigure(internal, width=e.width))
# create frame for content inside canvas and add it to the canvas
content = Frame(holder, relief=RAISED)
### --- update scrollregion whenever the internal frame is resized
content.bind('<Configure>', lambda e: holder.configure(scrollregion=holder.bbox("all")))
### --- save the item ID of the internal frame
internal = holder.create_window((0,0), window=content, anchor='nw')
# create bottom part
bottom = Frame(root, relief=SUNKEN)
root.rowconfigure(0, weight=18)
root.rowconfigure(1, weight=1, minsize=50)
root.columnconfigure(0, weight=1)
holder.pack(side=LEFT, fill=BOTH, expand=1)
scroll.pack(side=RIGHT, fill=Y)
top.grid(row=0, column=0, sticky='NSEW')
bottom.grid(row=1, column=0, sticky='NSEW')
num=0
for site in sites:
temp=Text(content, height = 5)
temp.configure(state= DISABLED)
temp.pack(fill=X, side=TOP, padx= 5, pady= 5)
siteBoxes.append(temp)
num += 1
root.mainloop()

How am I suppose to get the width and height of a Frame in Python

In my python code, I am trying to make the width of my button the same as the width of the Frame it is in. The Frame's width changes when the window is resized. I tried Widget['width'] and Widget.winfo_width() but both of them give me errors.
My code:
from tkinter import *
root = Tk()
root.geometry('750x500')
root.minsize(750, 500)
# Frames
Screen = Frame(root, height=500, width=500, bg='pink').grid(row=0, column=0, sticky='nsew', rowspan=4)
root.grid_columnconfigure(0, weight=3)
Buttons = Frame(root, height=500, width=100, bg="blue").grid(row=0, column=1, sticky='nsew', rowspan=4, columnspan=2)
root.grid_columnconfigure(2, weight=1)
root.grid_rowconfigure(3, weight=1)
# Buttons
UpgradeBtn = Button(Buttons, text="Upgrades")
UpgradeBtn.grid(row=0, column=1, columnspan=2)
WallBreakBtn = Button(Buttons, text="Wall Breaking")
WallBreakBtn.grid(row=1, column=1, columnspan=2)
root.mainloop()
Could you help me?
The .grid method returns None so Screen and Buttons in your original code were both None, not widgets. Running .grid in a second statement means Screen or Buttons will have the winfo_XXX methods.
from tkinter import *
root = Tk()
root.geometry('750x500')
root.minsize(750, 500)
# Frames
Screen = Frame(root, height=500, width=500, bg='pink')
Screen.grid(row=0, column=0, sticky='nsew', rowspan=4) # Change
root.grid_columnconfigure(0, weight=3)
Buttons = Frame(root, height=500, width=100, bg="blue")
Buttons.grid(row=0, column=1, sticky='nsew', rowspan=4, columnspan=2) # Change
root.grid_columnconfigure(2, weight=1)
root.grid_rowconfigure(3, weight=1)
# New function
def on_upgrade():
print(Screen.winfo_width(), Screen.winfo_height())
# Buttons
UpgradeBtn = Button(Buttons, text=" w x h ", command = on_upgrade ) # Change
UpgradeBtn.grid(row=0, column=1, columnspan=2)
WallBreakBtn = Button(Buttons, text="Wall Breaking")
WallBreakBtn.grid(row=1, column=1, columnspan=2)
root.mainloop()
This prints the width and height of Screen to the console.

Implement scrollbar in Canvas

So I wanted to implement a scrollbar on the mainframe in Tkinter however that is not possible thus I have to use Canvas. With canvas, I am able to implement a scrollbar and use other widgets accordingly. Everything worked fine until I attempted to add widgets into the Canvas.
Error Faced: Scrollbar looks off and unable to scroll
So what did I do wrong and how can I fix it?
Code (Python 3.8.2)
from tkinter import *
root = Tk()
root.geometry("950x600")
# create canvas
canvas = Canvas(root, width=932, height=600, borderwidth=0, highlightthickness=0, bg="black")
canvas.grid()
# create a scrollbar
vsb = Scrollbar(root, orient="vertical", command=canvas.yview)
vsb.grid(row=0, column=1, sticky='ns')
canvas.configure(yscrollcommand=vsb.set)
# Test the ability to scroll
for x in range(30):
Label(canvas, text="test").grid(row=x)
root.mainloop()
Note: I only want to add other widgets, including scrollbar using only grid manager
Things to add
#1 a frame inside a canvas
#2 binding an event (configure) that changes the canvas view thing with scroll
#3 create a window with the given frame
from tkinter import *
root = Tk()
root.geometry("950x600")
# create canvas
canvas = Canvas(root, width=932, height=600, borderwidth=0, highlightthickness=0, bg="black")
vsb = Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)
# create a scrollbar
f=Frame(canvas)#1
canvas.grid()
canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))#2
canvas.create_window((0,0),anchor='nw',window=f,width=932)#3
vsb.grid(row=0, column=1, sticky='ns')
# Test the ability to scroll
for x in range(300):
Label(f, text="test").grid(row=x)
root.mainloop()

Getting a pop up canvas to take focus

How do I transfer keyboard control from one canvas to another
self.canvas.bind('<FocusOut>')
self.canvas2.bind('<FocusIn>')
doesn't work, the focus is still on canvas one and the keyboard still acts on canvas one not canvas two.
As well
self.canvas2.focus_set()
still keeps the focus on self.canvas and not on self.canvas2.
focus_set is how you give focus to a widget.
Here's an example that shows two canvases, and sets the focus when you click one of the canvases. When you press the "s" key, a square will be drawn on whichever canvas has the focus.
import tkinter as tk
import random
def draw_square(event):
x0 = random.randint(30, 370)
y0 = random.randint(30, 170)
size = random.randint(10, 30)
event.widget.create_rectangle(x0, y0, x0+size, y0+size, fill="red")
def give_focus(event):
event.widget.focus_set()
event.widget.configure(background="bisque")
def lose_focus(event):
event.widget.configure(background="white")
root = tk.Tk()
label = tk.Label(root, text="Click to focus a canvas, press 's' to draw a square")
canvas1 = tk.Canvas(root, width=400, height=200, background="white",
borderwidth=1, relief="raised")
canvas2 = tk.Canvas(root, width=400, height=200, background="white",
borderwidth=1, relief="raised")
label.pack(side="top", fill="x")
canvas1.pack(fill="both", expand=True)
canvas2.pack(fill="both", expand=True)
for canvas in (canvas1, canvas2):
canvas.bind("<FocusOut>",lose_focus)
canvas.bind("<1>", give_focus)
canvas.bind("<s>", draw_square)
root.mainloop()

Resources