I have this code here. Like you see if the text in one of the button is long, it will not all apper. Is there a solution to resize the width of the canvas which is now defined equal to 300 so that it will show all the text in in the buttons. I don't want to do it manually because in the futur the text will be get from an external file and the data can change.
Any ideas
from tkinter import *
from tkinter.ttk import *
from tkinter import messagebox
from time import strftime
import csv
import xmlrpc.client as xmlrpclib
root = Tk()
frame = Frame(root)
container = Frame(root)
canvas = Canvas(container, borderwidth = 0, highlightthickness = 0, width=300, height=500, bg = "white")
#scrollbar = Scrollbar(container, orient="vertical", command=canvas.yview)
vertibar=Scrollbar(
container,
orient=VERTICAL
)
vertibar.pack(side=RIGHT,fill=Y)
vertibar.config(command=canvas.yview)
horibar=Scrollbar(
container,
orient=HORIZONTAL
)
horibar.pack(side=BOTTOM,fill=X)
horibar.config(command=canvas.xview)
scrollable_frame = Frame(canvas)
# setting the minimum size of the root window
root.geometry("300x650")
# Adding widgets to the root window
Label(root, text='my app',
font=('Verdana', 15)).pack(pady=10)
# tell the canvas how large the frame will be, so that it knows how much it can scroll:
scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
scrollregion=canvas.bbox("all")
)
)
frame_id =canvas.create_window((0, 0), window=scrollable_frame, anchor='nw')
canvas.bind("<Configure>",canvas.itemconfig(frame_id, width=canvas.winfo_reqwidth()))
#canvas.configure(yscrollcommand=scrollbar.set)
canvas.config(
xscrollcommand=horibar.set,
yscrollcommand=vertibar.set
)
# rechner_full = str(rechner[row])
# print(rechner_full)
Button(scrollable_frame, text="text1").pack(fill="both", side="top")
Button(scrollable_frame, text="text2").pack(fill="both", side="top")
Button(scrollable_frame, text="text3").pack(fill="both", side="top")
Button(scrollable_frame, text="longtexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxt").pack(fill="both", side="top")
# print(button.cget('text'))
container.pack()
canvas.pack(side="left", fill="both")
#scrollbar.pack(side="right", fill="both")
# Button for closing
exit_button = Button(root, text="Beenden", command=root.destroy)
exit_button.pack(pady=20)
root.mainloop()
Since scrollable_frame will be resized to show the longest button, so you can expand canvas inside the callback bound to <Configure> event on scrollable_frame:
...
scrollable_frame = Frame(canvas)
frame_id =canvas.create_window((0, 0), window=scrollable_frame, anchor='nw')
def on_frame_resized(event):
canvas_width = canvas.winfo_reqwidth()
if event.width > canvas_width:
# expand canvas to the width of scrollable_frame
canvas.configure(width=event.width)
else:
# expand scrollable_frame to the width of canvas
canvas.itemconfigure(frame_id, width=canvas_width)
canvas.configure(scrollregion=canvas.bbox("all"))
# tell the canvas how large the frame will be, so that it knows how much it can scroll:
scrollable_frame.bind("<Configure>", on_frame_resized)
...
However you need to remove root.geometry(...) because it will block the main window from expanding due to resize of canvas. Also you need to remove canvas.bind("<Configure>", ...) because it will mess up the work done by on_frame_resized().
...
#root.geometry("300x650")
...
#canvas.bind("<Configure>",canvas.itemconfig(frame_id, width=canvas.winfo_reqwidth()))
...
Related
I have a Main TabControl (Cronaca). Inside I have a second TabControl (Incident).
I would like to insert a third new TabControl called "Example" into Incident. The new Tabcontrol "Example" will have to contain elements such as those comboboxes (those that were previously contained in Incident, then remove them from Incident). How can I do?
Can you show me this simple code? I know, I have already plugged a Tabcontrol into another Tabcontrol, but I am currently confused and in the early stages of Pyhon. Help will be useful to me
P.S: OPTIONAL: My code is probably written in an unordered way. If it is possible, can some good person also make order in the staves? (the staves that come before and the staves that come after). Thank you
from tkinter import *
from tkinter import ttk
import tkinter as tk
window=Tk()
window.attributes('-zoomed', True)
window.configure(bg='#f3f2f2')
style = ttk.Style(window)
style.theme_use('clam')
#######################
tabControl = ttk.Notebook(window, style='Custom.TNotebook', width=700, height=320)
cronaca = ttk.Notebook(tabControl)
politica = ttk.Notebook(tabControl)
gossip = ttk.Notebook(tabControl)
tabControl.add(cronaca, text ='Cronaca')
tabControl.add(politica, text ='Politica')
tabControl.add(gossip, text ='Gossip')
tabControl.place(x=1, y=1)
#CRONACA
a = ttk.Frame(cronaca)
canvas = tk.Canvas(a)
scrollbar = ttk.Scrollbar(a, orient="vertical", command=canvas.yview)
scrollable_frame = ttk.Frame(canvas, width = 500, height = 500)
scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
scrollregion=canvas.bbox("all")
)
)
canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)
#Incidente
a.pack()
cronaca.add(a, text="Incidente")
combo1=ttk.Combobox(scrollable_frame, width = 18)
combo1.place(x=20, y=20)
combo1['value'] = ["text1", "text2"]
combo2=ttk.Combobox(scrollable_frame, width = 18)
combo2.place(x=20, y=80)
combo2['value'] = ["text1", "text2"]
combo3=ttk.Combobox(scrollable_frame, width = 18)
combo3.place(x=20, y=140)
combo3['value'] = ["text1", "text2"]
combo4=ttk.Combobox(scrollable_frame, width = 18)
combo4.place(x=20, y=200)
combo4['value'] = ["text1", "text2"]
canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
b = ttk.Frame(cronaca)
cronaca.add(b, text="Microcriminalità")
c = ttk.Frame(cronaca)
cronaca.add(c, text="Criminalità")
d = ttk.Frame(cronaca)
cronaca.add(d, text="Disagi")
#tab 2
c = ttk.Frame(politica)
d = ttk.Frame(politica)
window.mainloop()
You can create another Notebook inside "Incidente" tab and put the scrollable frame inside a "Example" tab inside the new Notebook.
...
#CRONACA
#-- create a Notebook widget inside "cronaca"
incident = ttk.Notebook(cronaca)
#-- add the notebook into the "Incidente" tab
cronaca.add(incident, text="Incidente")
#Incidente
#-- create the scrollable frame inside the "Incidente" tab instead
a = ttk.Frame(incident)
#-- add the scrollable frame into a tab named "Example" inside "Incident" notebook
incident.add(a, text="Example")
...
Below is the updated code:
# avoid using wildcard import
#from tkinter import *
from tkinter import ttk
import tkinter as tk
window = tk.Tk() # changed from Tk() to tk.Tk()
window.attributes('-zoomed', True)
window.configure(bg='#f3f2f2')
style = ttk.Style(window)
style.theme_use('clam')
#######################
tabControl = ttk.Notebook(window, style='Custom.TNotebook', width=700, height=320)
cronaca = ttk.Notebook(tabControl)
politica = ttk.Notebook(tabControl)
gossip = ttk.Notebook(tabControl)
tabControl.add(cronaca, text ='Cronaca')
tabControl.add(politica, text ='Politica')
tabControl.add(gossip, text ='Gossip')
tabControl.place(x=1, y=1) # suggest to use pack() instead of place()
#CRONACA
#-- create a Notebook widget inside "cronaca"
incident = ttk.Notebook(cronaca)
#-- add the notebook into the "Incidente" tab
cronaca.add(incident, text="Incidente")
#Incidente
#-- create the scrollable frame inside the "Incident" notebook instead
a = ttk.Frame(incident)
#-- add the scrollable frame into a tab named "Example" inside "Incident" notebook
incident.add(a, text="Example")
canvas = tk.Canvas(a)
scrollbar = ttk.Scrollbar(a, orient="vertical", command=canvas.yview)
scrollable_frame = ttk.Frame(canvas, width = 500, height = 500)
scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
scrollregion=canvas.bbox("all")
)
)
canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)
combo1=ttk.Combobox(scrollable_frame, width = 18)
combo1.place(x=20, y=20)
combo1['value'] = ["text1", "text2"]
combo2=ttk.Combobox(scrollable_frame, width = 18)
combo2.place(x=20, y=80)
combo2['value'] = ["text1", "text2"]
combo3=ttk.Combobox(scrollable_frame, width = 18)
combo3.place(x=20, y=140)
combo3['value'] = ["text1", "text2"]
combo4=ttk.Combobox(scrollable_frame, width = 18)
combo4.place(x=20, y=200)
combo4['value'] = ["text1", "text2"]
canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
b = ttk.Frame(cronaca)
cronaca.add(b, text="Microcriminalità")
c = ttk.Frame(cronaca)
cronaca.add(c, text="Criminalità")
d = ttk.Frame(cronaca)
cronaca.add(d, text="Disagi")
#tab 2
c = ttk.Frame(politica)
d = ttk.Frame(politica)
window.mainloop()
I want this entry bar and other contents I'll add to the frame later to be centred correctly, I received this code that supposedly should work but it isn't.
import tkinter as tk
import math
import time
root = tk.Tk()
root.geometry()
root.attributes("-fullscreen", True)
exit_button = tk.Button(root, text = "Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
frame = tk.Frame(root)
main_entry = tk.Entry(root, width = 100, fg = "black")
main_entry.place(x=50, y=50)
frame.place(relx=.5,rely=.5, anchor='center')
root.mainloop()
As you can see the frame isn't centred so how can I fix this?
In order to achieve widget centering on a fullscreen I've had to use grid manager.
The code below works but the exact positioning requires some fiddling with frame padding.
frame padx = w/2-300 and pady = h/2-45 are arbitrary values found using a bit of trial and error.
import tkinter as tk
root = tk.Tk()
root.attributes( '-fullscreen', True )
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
frame = tk.Frame( root )
main_entry = tk.Entry( frame, width = 100 )
main_entry.grid( row = 0, column = 0, sticky = tk.NSEW )
frame.grid( row = 0, column = 0, padx = w/2-300, pady = h/2-45, sticky = tk.NSEW )
exit_button = tk.Button( frame, text = 'Exit', command = root.destroy )
exit_button.grid( row = 1, column = 0, sticky = tk.NSEW )
tk.mainloop()
Frame automatically changes size to size of objects inside Frame (when you use pack()) but you have nothing inside Frame. You put all widgets directly in root - so Frame has no size (width zero, height zero) and it is not visible.
When I use tk.Frame(root, bg='red', width=100, height=100) then I see small red frame in the center.
You have two problems:
(1) you put Entry in wrong parent - it has to be frame instead of root,
(2) you use place() which doesn't resize Frame to its children and it has size zero - so you don't see it. You would have to set size of Frame manully (ie. tk.Frame(..., width=100, height=100)) or you could use pack() and it will resize it automatically.
I add colors for backgrounds to see widgets. blue for window and red for frame.
import tkinter as tk
root = tk.Tk()
root['bg'] = 'blue'
root.attributes("-fullscreen", True)
exit_button = tk.Button(root, text="Exit", command=root.destroy)
exit_button.place(x=1506, y=0)
frame = tk.Frame(root, bg='red')
frame.place(relx=.5, rely=.5, anchor='center')
main_entry = tk.Entry(frame, width=100, fg="black")
main_entry.pack(padx=50, pady=50) # with external margins 50
root.mainloop()
There is only one frame in my GUI, and it resizes itself to the size of the window. The frame has a child label, and I want the label to always be 1/3 the height of the frame and 1/1.5 the width of the frame. The code below tries to do that but the label always resizes itself to the size of the frame.
import tkinter
tk = tkinter.Tk()
tk.geometry("400x400")
f = tkinter.Frame(tk, bd=5, bg="white")
f.pack(padx=10, pady=10)
def callback(event):
f.config(height=tk.winfo_height(), width=tk.winfo_width())
l.config(width=int(f.winfo_width()/1.5), height=int(f.winfo_height()/3))
l = tkinter.Label(f, text="lead me lord", bg="yellow", relief=tkinter.RAISED, bd=5)
l.pack(side="bottom")
tk.bind("<Configure>", callback)
tk.mainloop()
The width and height of the label are in characters. In order to use pixels, you need to add an empty image to the label:
img = tkinter.PhotoImage() # an image of size 0
l = tkinter.Label(f, text="lead me lord", bg="yellow", relief=tkinter.RAISED, bd=5,
image=img, compound='center')
Actually you don't need to resize the frame in the callback if you add fill="both", expand=1 into f.pack(...):
import tkinter
tk = tkinter.Tk()
tk.geometry("400x400")
f = tkinter.Frame(tk, bd=5, bg="white")
f.pack(padx=10, pady=10, fill="both", expand=1)
def callback(event):
l.config(width=int(f.winfo_width()/1.5), height=int(f.winfo_height()/3))
#l.config(width=event.width*2//3, height=event.height//3) # same as above line if bind on frame
img = tkinter.PhotoImage()
l = tkinter.Label(f, text="lead me lord", bg="yellow", relief=tkinter.RAISED, bd=5,
image=img, compound='center')
l.pack(side="bottom")
f.bind("<Configure>", callback) # bind on frame instead of root window
tk.mainloop()
Given your precise specifications, the best solution is to use place since it lets you use relative widths and heights. However, if you plan to have other widgets in the window, place is rarely the right choice.
This example will do exactly what you asked: place the label at the bottom with 1/3 the height and 1/1.5 the width. There is no need to have a callback for when the window changes size.
Note: I had to change the call to pack for the frame. The text of your question said it would expand to fill the window but the code you had wasn't doing that. I added the fill and expand options.
import tkinter
tk = tkinter.Tk()
tk.geometry("400x400")
f = tkinter.Frame(tk, bd=5, bg="white")
f.pack(padx=10, pady=10, fill="both", expand=True)
l = tkinter.Label(f, text="lead me lord", bg="yellow", relief=tkinter.RAISED, bd=5)
l.place(relx=.5, rely=1.0, anchor="s", relheight=1/3., relwidth=1/1.5)
tkinter.mainloop()
When the window is expanded or maximized, the frame is just constant. The size is constant. I want the over all frame to move as I expand or maximize the window. How can this be done?
from tkinter import *
def data():
for i in range(1000):
if (i % 2) == 0:
l4 = Label(frame, text="Size of rectangle:")
l4.grid(row=i, column=0)
en = Entry(frame)
en.grid(row=i, column=1)
b3 = Button(frame, text="Save")
b3.grid(row=1001, column=0)
b4 = Button(frame, text="Back")
b4.grid(row=1001, column=1)
def myfunction(event):
canvas.configure(scrollregion=canvas.bbox("all"),width=250,height=700)
def _on_mousewheel(event):
canvas.yview_scroll(-1*(event.delta/120), "units")
root=Tk()
sizex = 272
sizey = 707
posx = 100
posy = 100
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
myframe=Frame(root)
myframe.place(x=0,y=0)
canvas=Canvas(myframe)
frame=Frame(canvas)
myscrollbar=Scrollbar(myframe,orient="vertical",command=canvas.yview)
canvas.configure(yscrollcommand=myscrollbar.set)
canvas.bind_all('<MouseWheel>', lambda event: canvas.yview_scroll(int(-1*(event.delta/120)), "units"))
myscrollbar.pack(side="right",fill="y")
canvas.pack(side="left")
canvas.create_window((0,0),window=frame,anchor='nw')
frame.bind("<Configure>",myfunction)
data()
root.bind("<MouseWheel>", myfunction)
root.mainloop()
The canvas will generate a <Configure> event when it is resized. You can bind to this event and reset the size of the inner frame to match the width of the canvas when this happens.
However, in your specific case you've got a series of stacked widgets, none of which will grow when you resize the window. Because of that, even when you resize the window, the canvas doesn't grow. Because the canvas doesn't grow, the event won'g fire.
You'll need to use the appropriate options to the geometry managers to make sure the entire hierarchy of windows grows and shrinks appropriately, and then add the binding to set the size of the inner window.
Here's a very basic example. It starts by creating a canvas and packing it so that it fills the root window. It then adds a frame inside the canvas, and arranges for the width of the frame to change whenever the canvas is resized.
import tkinter as tk
def handle_canvas_resize(event):
canvas.itemconfigure(window_id, width=event.width)
def handle_frame_resize(event):
canvas.configure(scrollregion=canvas.bbox("all"))
root = tk.Tk()
canvas = tk.Canvas(root, width=200, height=200)
scrollbar = tk.Scrollbar(root, command=canvas.yview)
canvas.configure(yscrollcommand=scrollbar.set)
scrollbar.pack(side="right", fill="y")
canvas.pack(fill="both", expand=True)
inner_frame = tk.Frame(canvas, background="bisque")
window_id = canvas.create_window(2,2, window=inner_frame)
for i in range(1, 101):
label = tk.Label(inner_frame, text="label #{}".format(i))
label.pack(side="top", fill="x", padx=1, pady=1)
inner_frame.bind("<Configure>", handle_frame_resize)
canvas.bind("<Configure>", handle_canvas_resize)
root.after_idle(canvas.yview_moveto, 0)
root.mainloop()
My code has a Frame, a Canvas inside the Frame, and an inner Frame inside the Canvas. I want to put Entry boxes inside the inner Frame and fit them to the inner Frame via pack_propagate(0) to avoid pixel/font width conversions with the Entry widget's width option. However, this breaks the inner Frame's scroll functionality. I want to add Entry widgets dynamically above and below the first and last Entry widgets in the inner Frame, which I am currently doing using pack(before=). So I would like to stick with the packer if possible.
How can I get the scrollbar working again? The following minimum working example has frame.pack_propagate(0) commented out, so the Entry widgets are not sized to the column correctly:
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
myframe = ttk.Frame(root)
myframe.pack()
sb = ttk.Scrollbar(myframe)
sb.pack(side=tk.RIGHT, fill=tk.Y, expand=1)
canvas = tk.Canvas(myframe, width=200, height=300,
scrollregion=(0, 0, 200, 300), yscrollcommand=sb.set)
canvas.pack()
frame = ttk.Frame(canvas, width=200, height=300) # inner Frame
canvas.create_window((0, 0), window=frame, anchor='nw')
sb.config(command=canvas.yview)
frame.bind("<Configure>", lambda event: canvas.configure(
scrollregion=canvas.bbox(tk.ALL)))
# frame.pack_propagate(0) # How to enable this and ensure scrollbar works?
s = tk.StringVar()
s.set("I'm a box")
for _ in range(100):
eb = ttk.Entry(frame, textvariable=s)
eb.pack()
root.mainloop()
Edit1: Added StringVar() and changed eb.grid() to eb.pack()
See example which resize inner Frame to Canvas - it uses
self._canvas.bind('<Configure>', self.inner_resize)
and inside method inner_resize()
self._canvas.itemconfig(self._window, width=event.width)
Full example
import tkinter as tk
import tkinter.ttk as ttk
class ScrolledFrame(tk.Frame):
def __init__(self, parent, vertical=True, horizontal=False):
super().__init__(parent)
# canvas for inner frame
self._canvas = tk.Canvas(self, bg='red')
self._canvas.grid(row=0, column=0, sticky='news') # changed
# create right scrollbar and connect to canvas Y
self._vertical_bar = tk.Scrollbar(self, orient='vertical', command=self._canvas.yview)
if vertical:
self._vertical_bar.grid(row=0, column=1, sticky='ns')
self._canvas.configure(yscrollcommand=self._vertical_bar.set)
# create bottom scrollbar and connect to canvas X
self._horizontal_bar = tk.Scrollbar(self, orient='horizontal', command=self._canvas.xview)
if horizontal:
self._horizontal_bar.grid(row=1, column=0, sticky='we')
self._canvas.configure(xscrollcommand=self._horizontal_bar.set)
# inner frame for widgets
self.inner = tk.Frame(self._canvas)
self._window = self._canvas.create_window((0, 0), window=self.inner, anchor='nw')
# autoresize inner frame
self.columnconfigure(0, weight=1) # changed
self.rowconfigure(0, weight=1) # changed
# resize when configure changed
self.inner.bind('<Configure>', self.resize)
# resize inner frame to canvas size
self.resize_width = False
self.resize_height = False
self._canvas.bind('<Configure>', self.inner_resize)
def resize(self, event=None):
self._canvas.configure(scrollregion=self._canvas.bbox('all'))
def inner_resize(self, event):
# resize inner frame to canvas size
if self.resize_width:
self._canvas.itemconfig(self._window, width=event.width)
if self.resize_height:
self._canvas.itemconfig(self._window, height=event.height)
# --- main ----
root = tk.Tk()
sf = ScrolledFrame(root)
sf.resize_width = True # it will resize frame to canvas
sf.pack(fill='both', expand=True)
s = tk.StringVar()
s.set("I'm a box")
for _ in range(100):
eb = ttk.Entry(sf.inner, textvariable=s)
eb.pack(fill='x', expand=True)
root.mainloop()