I know you can align a button to the left with pack(side="left"), and you can also do right top and bottom, but how would i align stuff on a new line just below a set of buttons above it?
I tried using grid but no matter how much i googled it I couldn't find out how to fix it.
What you'd want to do is create different frames, each with their own positioning logic. Take a look at this:
import tkinter
root = tkinter.Tk()
frame = tkinter.Frame(root)
frame.pack()
bottomframe = tkinter.Frame(root)
bottomframe.pack(side=tkinter.BOTTOM)
tkinter.Button(frame, text="left").pack(side=tkinter.LEFT)
tkinter.Button(frame, text="middle").pack(side=tkinter.LEFT)
tkinter.Button(frame, text="right").pack(side=tkinter.LEFT)
# what you want
tkinter.Button(bottomframe, text="top").pack(side=tkinter.TOP)
tkinter.Button(bottomframe, text="under").pack(side=tkinter.TOP)
root.mainloop()
Related
I am trying to create a scrollable canvas in tkinter, filled with a grid of smaller square canvases. I have no problem setting this up, but the result is very slow when scrolling. Any advice on how to speed things up? I have considered, but have yet to try to implement:
Using pages of many widgets rather than one scrollable canvas. This would solve the problem, but I'd like to use a scrolling solution if possible.
When scrolling, have the canvas "snap" to the nearest row of squares. The scrolling does not need to be continuous, e.g. we don't need to see half a square at a time.
Change the canvas to only contain the squares that are currently being viewed. All windows that aren't being viewed can be destroyed and created again later. I am unclear if this approach will even help, but a similar approach worked well for a large image on a different project I've worked on.
It could be possible that this lag is just a hardware issue on my end. Unfortunately, I'm not sure how to test this.
Here is the reprex, any help is appreciated! Thank you.
import tkinter as tk
import numpy as np
root = tk.Tk()
outside = tk.Frame(root, bg='green')
outside.pack(expand=True, fill='both')
def get_box(): # box is 16x16 pixels
colors = ['red', 'blue', 'green', 'yellow']
color = np.random.choice(colors, 1)[0]
ret = tk.Canvas(canvas, bg=color, width=16, height=16, highlightthickness=0)
return ret
canvas = tk.Canvas(outside, width=16*30, bg='gray20', highlightthickness=0). # 30 columns
canvas.pack(fill='y', expand=True, side='left')
scroll_y = tk.Scrollbar(outside, orient="vertical", command=canvas.yview) # Create scrollbar
canvas.configure(scrollregion=canvas.bbox('all'), yscrollcommand=scroll_y.set)
scroll_y.pack(fill='y', side='right', expand=True)
for i in range(600): # create many boxes. ideally, ~4000 once things are running smoothly
box = get_box()
canvas.create_window(16 * (i % 30), 16 * (i //30), anchor='nw', window=box)
canvas.update_idletasks()
canvas.configure(scrollregion=canvas.bbox('all'), yscrollcommand=scroll_y.set)
root.mainloop()
I have a tkinter window with several images that use grid. I tried to grid a scrollbar to the right side of the window but don't know what options to use because "side = right", "fill = Y" cannot be used inside grid. Any suggestions?
Grid is row / column oriented. So each widget needs its row and column in the geometry manager set. Here is a demo that answers the question posed.
# create a widget and scrollbar
import tkinter as tk
root = tk.Tk()
text = tk.Text(root)
vs = tk.Scrollbar(root, command=text.yview)
text.configure(yscrollcommand=vs.set)
# set the row, column and expansion for each widget
text.grid(row=0, column=0, sticky='news')
vs.grid(row=0, column=1, sticky='news')
# now make the grid manager expand to fill the available space in root.
# making 0, 0 expand to use all the spare space and 0,1 be fixed
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.mainloop()
If you are managing a set of images on label widgets then put them in a frame to separate out the geometry management of those. You might want to consider if you should
put them into a canvas instead which has quite good scrolling support.
from tkinter import *
# ==================================================Settings=======================================================
root = Tk()
root.title("Video Youtube Downloader") # set up the title and size.
root.configure(background='black') # set up background.
root.minsize(800, 500)
root.maxsize(800, 500)
# ==================================================Frames=======================================================
top = Frame(root, width=800, height=50, bg='yellow').pack(side=TOP)
bottom = Frame(root, width=800, height=50, bg='red').pack(side=BOTTOM)
left = Frame(root, width=550, height=450, bg='black').pack(side=LEFT)
right = Frame(root, width=250, height=450, bg='blue').pack(side=RIGHT)
# ==================================================Buttons=======================================================
btn_clear_url = Button(right, text="Clear Url", font=('arial', 10, 'bold')).grid(row=1, columns=1)
I am trying to add buttons to the right Frame, but for some reason when I run the program the IDE shows that it is running but there is
no window. When I delete .grid(row=1, columns=1) the window appears.
How can I fix this bug, and add btn_clear_url button to the right Frame?
First of all, you need to invoke Tk's mainloop at the end of your code (you can see here why).
Another problem is chaining method calls like that. You're actually assigning return value of the last call to the variable, which is None in case of grid() and pack() - therefore, all your variables end up having None as the value. You need to separate widget instantiating call and grid or pack call and put each on its own line.
Other than that, you're setting both minsize and maxsize to the very same size - if you're really just trying to make your window not resizable, set the size with:
root.geometry('800x500') # for instance
...and after that configure resizable attribute:
root.resizable(width=False, height=False)
Also, I suggest you get rid of from tkinter import *, since you don't know what names that imports. It can replace names you imported earlier, and it makes it very difficult to tell where names in your program are supposed to come from. Use import tkinter as tk instead.
When you place widget in a tk window you cannot use grid and pack at the same time in the same window, so you use should use pack () instead of grid()
The problem starts with this line of code:
right = Frame(root, width=250, height=450, bg='blue').pack(side=RIGHT)
With that, right is set to None because .pack(side=RIGHT) returns None.
Next, you do this:
btn_clear_url = Button(right, ...)).grid(row=1, columns=1)
Because right is None, it's the same as Button(root, ...). Since you are already using pack for a widget in root, you can't also use grid.
The solution -- and best practice -- is to separate widget creation from widget layout:
top = Frame(root, width=800, height=50, bg='yellow')
bottom = Frame(root, width=800, height=50, bg='red')
left = Frame(root, width=550, height=450, bg='black')
right = Frame(root, width=250, height=450, bg='blue')
top.pack(side=TOP)
bottom.pack(side=BOTTOM)
left.pack(side=LEFT)
right.pack(side=RIGHT)
With the above, right will be properly set to the frame instance, and adding a widget to right and using grid will no longer fail.
I'm currently trying to implement a scrollbar on a canvas, since I learned that I can't do it instantly on a frame. I can make it appear, but I cannot actually make it work. I'm still a beginner when it comes to python and tkinter and the previous posts in this matter haven't helped me that much. Here's my code(I'm open to advice on anything else I've done that's considered bad practice as well):
from tkinter import *
class myApp():
def __init__(self,root):
myApp.f2=Frame(root)
myApp.f2.pack()
myApp.canv=Canvas(self.f2)
myApp.canv.pack()
myApp.f1=Frame(self.canv)
myApp.f1.pack(side=LEFT, fill=BOTH, expand=TRUE)
myApp.scroll=Scrollbar(self.f1,orient=VERTICAL,
command=myApp.canv.yview)
myApp.scroll.grid(row=0,column=6)
myApp.canv.config(yscrollcommand=myApp.scroll.set)
I have to use grid for the rest of the widgets, that I haven't included here.
i don't really understand how the bind works, but here's the code i use for a scrollbar in a Toplevel, it's not from me but i don't remember where i found it (i think it's on stackoverflow, you should search more, i'm sure you will find something). it should work but you can scroll the bar only if you hover it with the mouse though
Toplevel = tk.Toplevel(self)
#create canvas to make a scrollbar
canvas = tk.Canvas(Toplevel, borderwidth=0)
#create frame which will contains your widgets
frame = tk.Frame(canvas)
#create and pack your vsb to the Toplevel and link it to the canvas yview
vsb = tk.Scrollbar(Toplevel, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)
vsb.pack(side="right", fill="y")
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((5,5), window=frame, anchor="nw")
#i don't understand this line
frame.bind("<Configure>", lambda event, canvas=canvas: canvas.configure(scrollregion=canvas.bbox("all")))
#add your widgets to the frame
...
PS : Don't use from tkinter import * you can(and will) have name collisions, use import tkinter or import tkinter as tk
Edit : this question is my source.
I am new to tkinter and python3. I have worked on creating a scrollbar for a frame that is a child of a canvas which is also a child of Toplevel(). The scrollbar buttons function well but the bar/box itself stretches from top to bottom and cannot move. Furthermore, using the scroll buttons, the user can scroll way beyond the content (where there nothing to view).
Here is the code.
#! /usr/bin/env python3
from tkinter import *
from filegroups import typeGroups
app = Tk()
types_window = Toplevel(app)
types_window.wm_title('Types')
yscrollbar = Scrollbar(types_window, orient=VERTICAL)
yscrollbar.grid(row=0, column=1, sticky=N+S)
canvas = Canvas(types_window,
width = 300,
height = 500,
yscrollcommand=yscrollbar.set)
canvas.grid(row=0,column=0)
canvas.config(scrollregion=canvas.bbox("all"))
yscrollbar.config(command=canvas.yview)
frame = Frame(canvas)
canvas.create_window(0,0,anchor=NW,window=frame)
for key in sorted(typeGroups.keys()):
options_frame = LabelFrame(frame, text=key)
options_frame.grid(padx=5, pady=10)
for item in typeGroups[key]:
item_button = Checkbutton(options_frame,
text=item)
item_button.grid()
app.mainloop()
You need to update the canvas scrollregion after filling the frame with labels by adding these two lines just before app.mainloop():
canvas.update_idletasks()
canvas.config(scrollregion=canvas.bbox("all"))
The call to update_idletasks is needed to ensure that the canvas content is updated before we ask for the bounding box.