Tkinter, I cannot get my slider to show all buttons - python

Here is a sample of what I am doing. I put 500 buttons on a sliding canvas, and I can only scroll to button 109. What am I doing wrong? I've tried adjusting the width on the widgets, as well as the scrollregion. I'm sure I'm overlooking something simple.
import Tkinter
from Tkinter import *
import os, signal
window1 = Tkinter.Tk()
window1.attributes('-fullscreen', True)
window1.bind("<Escape>", lambda e: e.widget.quit())
window2 = Tkinter.Label(window1)
window2.configure(bg = "blue")
window2.place(x=0, y=0, relwidth=1, relheight=1)
frame=Frame(window2, height=160)
frame.pack(anchor=Tkinter.SW,side=Tkinter.BOTTOM)
canvas=Canvas(frame,bg='black', width=5500, height=160, scrollregion=(0,0,5500,0))
hbar=Scrollbar(frame,orient=HORIZONTAL, bg='black', troughcolor='black')
hbar.pack(side=BOTTOM,fill=X)
hbar.config(command=canvas.xview)
canvas.config(xscrollcommand=hbar.set, xscrollincrement=4.5)
canvas.pack(side=LEFT,expand=True,fill=BOTH)
frm = Frame(canvas)
canvas.create_window(0, 0, window=frm, anchor='nw')
def quit():
window1.quit()
count = 0
while (count < 500):
X = Tkinter.Button(frm, text=str(count), height=6, width=5, padx=0, pady=0, highlightcolor="blue", highlightthickness=3, command=quit)
X.pack(side = "left")
count = count + 1
window1.mainloop()

The problem is that your scrollregion on the Canvas instance isn't wide enough. You can calculate how wide it needs to be based on the requested width of all the buttons, and then set the scrollregion after you've made all the buttons, like this:
import Tkinter
import os, signal
window1 = Tkinter.Tk()
window1.attributes('-fullscreen', True)
window1.bind("<Escape>", lambda e: e.widget.quit())
window2 = Tkinter.Label(window1)
window2.configure(bg = "blue")
window2.place(x=0, y=0, relwidth=1, relheight=1)
frame = Tkinter.Frame(window2, height=160)
frame.pack(anchor=Tkinter.SW,side=Tkinter.BOTTOM)
canvas=Tkinter.Canvas(frame,bg='black', width=5500, height=160)
hbar=Tkinter.Scrollbar(frame,orient=Tkinter.HORIZONTAL, bg='black', troughcolor='black')
hbar.pack(side=Tkinter.BOTTOM,fill=Tkinter.X)
hbar.config(command=canvas.xview)
canvas.config(xscrollcommand=hbar.set, xscrollincrement=4.5)
canvas.pack(side=Tkinter.LEFT, expand=True, fill=Tkinter.BOTH)
frm = Tkinter.Frame(canvas)
canvas.create_window(0, 0, window=frm, anchor='nw')
def quit():
window1.quit()
count = 0
width = 0
while (count < 500):
X = Tkinter.Button(frm, text=str(count), height=6, width=5, padx=0, pady=0, highlightcolor="blue", highlightthickness=3, command=quit)
X.pack(side = "left")
width += X.winfo_reqwidth()
count = count + 1
canvas.config(scrollregion=(0, 0, width, 0))
window1.mainloop()
As a side note, your first two import statements are redundant. In this example, I dropped the second one to avoid polluting the global namespace.

Related

Python tkinker resitze Canvas

I have an Python3 Tkinter Programm. I have 3 Frames in the Main Window and in one Frame an canvas with scroll Option - now i want resitze the Canvas Area .
Now if i resize it moves the Scroll Bar for Y out the Window and the scrollbar for x works also not as expected (get bigger but slide area don't change)
How i Mange it to resize an Canvas in an grid Layout - The Window must be the same size , the Scrollbas must be updatet and the Canvas Plane must be bigger.
an excerpt from my code:
import tkinter as tk
def menu_build():
caninfo[0] += 10
cangui.configure(width = caninfo[0])
#cangui.configure(scrollregion=cangui.bbox("all"))
def gui():
master = tk.Tk()
master.title( "Easy Switch" )
master.geometry("480x320")
frametop = tk.Frame(master, bg="blue", bd=2)
frametop.grid(column=0,row=0)
frameex = tk.Frame(master, bg="yellow", bd=2)
frameex.grid(column=1,row=1)
framegui = tk.Frame(master, bg="red", bd=2)
framegui.grid(column=0, columnspan=2, row=1)
menu = tk.Menu(master)
master.config(menu=menu)
filemenu = tk.Menu(menu)
menu.add_cascade(label="Config", menu=filemenu)
filemenu.add_command(label="Resize",command=menu_build)
global cangui
cangui = tk.Canvas(framegui, width=385, height=250)
#caninfo = [385,250]
cangui.grid(row=1, column=2)
scroll_x = tk.Scrollbar(framegui, orient="horizontal", command=cangui.xview)
scroll_x.grid(row=2, column=2, sticky="ew")
scroll_y = tk.Scrollbar(framegui, orient="vertical", command=cangui.yview)
scroll_y.grid(row=1, column=3, sticky="ns")
cangui.configure(yscrollcommand=scroll_y.set,xscrollcommand=scroll_x.set)
cangui.configure(scrollregion=cangui.bbox("all"))
cwcb = tk.Checkbutton(framegui, text="ccw").grid(row=2,column=0)
cangui.create_arc(90,90,110,110,style=tk.PIESLICE,width=4,start=0,extent=300)
master.mainloop()
global caninfo
caninfo = [385,250]
if __name__ == "__main__":
gui()
no need to resize the canvas Area
wrote an extra funktion
win = [int(cangui.cget("width")),int(cangui.cget("height"))]
draw_xy = cangui.bbox("all")
swin = (min(0,draw_xy[0]),min(0,draw_xy[1]),max(draw_xy[2],win[0]),max(draw_xy[3],win[1]))
cangui.configure(scrollregion=swin)
reason: canvas.bbox("all") gives only the positon from most upper/left grafic and i want 0/0

Scrollbar issue in tkinter python

I am trying to create a product list using a grid view in Tkinter, but I am totally confused about that why the scrollbar thing is not working, I have checked so many tutorials to fix the scrollbar.
first of all, I thought it was because of pack() or grid(), but after using pack() instead of grid things become worse,
please help me solve this issue
frame_main = Frame(root, bg="gray")
frame_main.pack(fill='both', expand=1)
my_canvas = Canvas(frame_main, bg="red")
my_canvas.pack(fill=Y, expand=1)
vsb = Scrollbar(frame_main, orient="vertical", command=my_canvas.yview)
vsb.pack(side='right', fill=Y)
my_canvas.configure(yscrollcommand=vsb.set)
my_canvas.bind('<Configure>', lambda e: my_canvas.configure(
scrollregion=my_canvas.bbox("all")))
inner_frame = Frame(my_canvas).pack(fill=Y, expand=1)
my_canvas.create_window((0, 0), window=inner_frame, anchor="nw")
img = ImageTk.PhotoImage(Image.open(
'images/apple--600.png').resize((200, 200), Image.ANTIALIAS))
for i in range(4):
for j in range(3):
label = Label(inner_frame, anchor="center", image=img, bg="green")
label.grid(column=j, row=i)
label_txt = Label(inner_frame, anchor="center",
text="item1 \n Price - $20.00", bg="white")
label_txt.grid(column=j, row=i, sticky='WES')
j += 1
i += 2

How to make button using canvas in toplevel() Tkinter

I'm trying to make a button using canvas.create_window in toplevel() in Tkinter. Button is used to go back to main window. First "Start" button is displayed but second "Back" button is not. Code below.
from tkinter import *
win = Tk()
def play_button():
win.withdraw()
top = Toplevel()
top.geometry("300x300")
button_back = Button(top, text="Back", command=back_button)
canvas_two = Canvas(top, width = 300, height = 300)
canvas_two.pack(fill="both", expand=True)
Button_reverse = canvas_two.create_window(0, 0, anchor="nw", window=button_back)
top.resizable(False, False)
def back_button():
win.deiconify()
win.geometry("300x300")
canvas = Canvas(win, width = 300, height = 300)
canvas.pack(fill="both", expand=True)
button_play = Button(win, text="Play", command=play_button)
Play_button = canvas.create_window(0, 0, anchor="nw", window=button_play )
win.mainloop()
The problem is the ordering of creation of canvas_two and button_back. You need to create the Canvas first and then put the Button on top of it as shown below.
def play_button():
win.withdraw()
top = Toplevel()
top.geometry("300x300")
canvas_two = Canvas(top, width=300, height=300)
canvas_two.pack(fill="both", expand=True)
button_back = Button(top, text="Back", command=back_button)
Button_reverse = canvas_two.create_window(0, 0, anchor="nw", window=button_back)
top.resizable(False, False)

tkinter insert widget in a frame

I am trying to make the following layout
tkinter layout
but the ID: label and the entry box are center left , and center right when then they should be next to each other , and they keep getting separated by the grid
I am also trying to use a for loop to make the number pad but im not sure how to make a new variable outside of the loops, and increment by 1 in the loop that creates the button
from tkinter import *
window = Tk()
#BOTTOM FRAME SECTION
bottomframe = Frame(window,bg="cyan", width =900, height = 100)
bottomframe.pack(fill=BOTH,side=BOTTOM)
button = Button(window,text="LOG IN")
button.pack(fill=BOTH,side=BOTTOM)
checkbutton = Checkbutton(window, text="Use pseudonym?")
checkbutton.pack(side=BOTTOM)
topframe = Frame(window,bg="red",width =900, height = 100)
topframe.pack(fill=BOTH,side=TOP)
label1 = Label(window, text="Majestic 12 Identifier")
label1.pack(side=TOP)
label2 = Label(window, text="ID")
label2.pack(side=LEFT)
label3 = Label(window,text="Enter keycode:")
label3.pack(side=TOP)
entry1 = Entry(window)
entry1.pack(side=LEFT)
#GRID SECTION
frame = Frame(window)
frame.pack(fill=BOTH,side=BOTTOM)
n = +1
for i in range(3):
Grid.rowconfigure(frame,i,weight=1)
Grid.columnconfigure(frame,i,weight=1)
for i in range(3):
b = Button(frame, text="%d" % (i+n))
for j in range(3):
b = Button(frame, text="%d" % (j+1))
b.grid(row=i, column=j,ipadx=2,ipady=2,padx=2,pady=2,sticky= W+E+N+S)
window.mainloop()
any help is welcome
Ok, I gave it a try. I played around a bit with the Frame objects. I deleted one, that was not needed. And I introduced topframe2 in order to make it possible for label2 and entry1 to be in the same row.
Watch carefully the parent of the various entries and labels. Not everything should get the window object as direct parent.
I am using expand and fill arguments - here I am basically applying what I just learned at Textbox not expanding with containing frame - TKinter and tkinter gui layout using frames and grid
from tkinter import *
window = Tk()
# BOTTOM FRAME SECTION
topframe = Frame(window, width=900, height=100)
topframe.pack(fill=BOTH, side=TOP)
label1 = Label(topframe, text="Majestic 12 Identifier")
label1.pack(side=TOP, fill=BOTH, expand=1)
topframe2 = Frame(topframe, width=900, height=100)
topframe2.pack(fill=BOTH, side=TOP)
label2 = Label(topframe2, text="ID")
label2.pack(side=LEFT)
entry1 = Entry(topframe2)
entry1.pack(side=LEFT, fill=X, expand=1)
label3 = Label(window, text="Enter keycode:")
label3.pack(side=TOP)
# GRID SECTION
frame = Frame(window)
frame.pack(fill=BOTH, side=TOP, expand=1)
n = +1
for i in range(3):
Grid.rowconfigure(frame, i, weight=1)
Grid.columnconfigure(frame, i, weight=1)
for i in range(3):
b = Button(frame, text="%d" % (i + n))
for j in range(3):
b = Button(frame, text="%d" % (j + 1))
b.grid(row=i, column=j, ipadx=2, ipady=2, padx=2, pady=2, sticky=W + E + N + S)
button = Button(window, text="LOG IN")
button.pack(fill=BOTH, side=BOTTOM)
checkbutton = Checkbutton(window, text="Use pseudonym?")
checkbutton.pack(side=BOTTOM)
if __name__ == '__main__':
window.mainloop()

Tkinter Scrollbar position, yview_moveto() seems not working

Consider the following example:
from tkinter import *
startingWin = Tk()
canvas = Canvas(startingWin, height=600)
canvas.grid(row=0, column=0,sticky="nsew")
canvasFrame = Frame(canvas)
canvas.create_window(0, 0, window=canvasFrame, anchor='nw')
for i in range(70):
element = Button(canvasFrame, text='Button %s ' % i)
element.grid(row=i, column=0)
yscrollbar = Scrollbar(startingWin, orient=VERTICAL)
yscrollbar.config(command=canvas.yview)
canvas.config(yscrollcommand=yscrollbar.set)
yscrollbar.grid(row=0, column=1, sticky="ns")
canvas.yview_moveto(0.5)
canvasFrame.bind("<Configure>", lambda event: canvas.configure(scrollregion=canvas.bbox("all")))
startingWin.mainloop()
Expected output: Scrollbar in the middle.
However, I get the Scrollbar always on the top, regardless of the value I give to yview_moveto() like the following:
How can I fix this?
You shouldn't call canvas.yview_moveto(0.5) until after the scrollregion has been defined.

Categories