Python 3.3 Tkinter LabelFrame resizable - python

Is it possible to create a resizable LabelFrame?
Or any way?
And is it possible to use ttk.PanedWindow with LabelFrame for this?
it's my code:
fram1 = ttk.LabelFrame(root, text = "text1", height = 100, width = 200)
fram1.config(relief=FLAT)
fram1.pack(side = "right", fill="both", expand = True)
fram2 = ttk.LabelFrame(root, text = "text2", height = 100, width = 200)
fram2.config(relief=FLAT)
fram2.pack(side = "left", fill="both", expand = True)
and i can't resize these labelframes

The panedwindow can hold any single widget in a pane so a labelframe is no problem and allows you to add further widgets and children of the labelframe. An example:
import sys
from tkinter import *
from tkinter.ttk import *
def main():
app = Tk()
pw = PanedWindow(app, orient='vertical')
paneA = LabelFrame(pw, text="Pane A", height=240, width=320)
paneB = LabelFrame(pw, text="Pane B", height=240, width=320)
pw.add(paneA, weight=50)
pw.add(paneB, weight=50)
pw.pack(fill='both', expand=True)
app.mainloop()
if __name__=='__main__':
sys.exit(main())
The weight allows you to set a proportionate scaling for each pane as you change the size of the container. If both panes have the same weight then they grow by the same amount.

Related

how to create a panedWindow in tkinter with three panedwindows as children and the left and right panedwindows having defined width

Please I am trying to create a UI but I coulld get the main window up and running. I want the left and right paned window to have defined width and the middle should cover remaining width. this is working for the left pane but it is not working for right pane. below is the code and the output. I am just learning tkinter and everything I read from the documentation does not work for me. PLease Is there anyone who could help.
`
import customtkinter
from tkinter import *
import tkinter.messagebox
customtkinter.set_appearance_mode("Dark") # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("dark-blue") # Themes: "blue" (standard), "green", "dark-blue"
def addPage():
pass
def copyText():
pass
class App(customtkinter.CTk):
width = 1440
height = 1024
def __init__(self):
super().__init__()
self.geometry(f"{App.width}x{App.height}")
self.radio_var = tkinter.IntVar(value=0)
###Menus
self.myMenu = Menu(self)
self.config(menu=self.myMenu)
# File Menu
self.fileMenu = Menu(self.myMenu)
self.myMenu.add_cascade(label="File", menu=self.fileMenu)
self.fileMenu.add_command(label="add...", command=addPage)
# Edit Menu
self.editMenu = Menu(self.myMenu)
self.myMenu.add_cascade(label="Edit", menu=self.editMenu)
self.editMenu.add_command(label="Copy", command=copyText)
# define Frames
self.grid_columnconfigure(0, weight=1)
self.grid_rowconfigure(0, weight=1)
self.frameBody = customtkinter.CTkFrame(master=self)
self.frameBody.grid(row=0, column=0, sticky="nswe")
self.frameFooter = customtkinter.CTkFrame(master=self, height=25, fg_color="#a6a6a6", corner_radius=0)
self.frameFooter.grid(row=1, column=0, sticky="nswe")
# main panel body
self.paneBody = PanedWindow(self.frameBody)
self.paneBody.pack(fill="both", expand=True)
#other three panes
self.paneLeftBody = PanedWindow(self.paneBody, bg = 'red', width = 300)
self.paneBody.add(self.paneLeftBody)
self.paneMiddleBody = PanedWindow(self.paneBody,orient = 'vertical')
self.paneBody.add(self.paneMiddleBody)
self.paneRightBody = PanedWindow(self.paneBody, bg = 'black', width = 300)
self.paneBody.add(self.paneRightBody)
self.middleTopPane = customtkinter.CTkFrame(self.paneMiddleBody, fg_color='red')
self.paneMiddleBody.add(self.middleTopPane,sticky="nswe")
self.middleBottomPane = customtkinter.CTkFrame(self.paneMiddleBody, height=300)
self.paneMiddleBody.add(self.middleBottomPane)
if __name__ == "__main__":
app = App()
app.mainloop()
`
Output

Update scrollbar in tkinter

I'm programming something in python and I need to create a scrollbar that updates when new widgets are added to the window. I don't know how to do this and I haven't found the answer in any place. Is there a command for this? This is my code right now.
import tkinter as tk
root = tk.Tk()
root.geometry("600x400")
my_canvas = tk.Canvas(root)
my_canvas.pack(side = "left", fill = "both", expand = 1)
my_scrollbar = tk.Scrollbar(root, orient = "vertical", command = my_canvas.yview)
my_scrollbar.pack(side = "right", fill = "y")
my_canvas.configure(yscrollcommand = my_scrollbar.set)
my_canvas.bind("<Configure>", lambda e: my_canvas.configure(scrollregion = my_canvas.bbox("all")))
my_frame = tk.LabelFrame(my_canvas, width = my_canvas.winfo_width())
my_frame.grid_propagate(0)
my_frame.pack()
for i in range(10):
my_label = tk.Label(my_frame, text = "Label")
my_label.pack()
my_canvas.create_window((0, 0), width = 600, anchor = "nw", window = my_frame)
def create_label(scroll, canv, fram):
my_label = tk.Label(fram, text="Label")
my_label.pack()
my_button = tk.Button(my_frame, text = "Button", command = lambda: create_label(my_scrollbar, my_canvas, my_frame))
my_button.pack()
root.mainloop()
Bind to the <Configure> event of the inner frame. That event fires whenever the frame changes size.
def adjust_scrollregion(event):
my_canvas.configure(scrollregion=my_canvas.bbox("all"))
my_frame.bind("<Configure>", adjust_scrollregion)
Unrelated to the question that was asked... calling my_frame.grid_propagate(0) is pointless since you use pack inside of my_frame.

update label variable in python tkinter

from tkinter import *
import tkinter as tk
import random
f = 0
def test_print():
f = random.randint(0,118)
master.update_idletasks()
# creating Tk window
master = Tk()
master.minsize(600,400)
var = IntVar()
var.set(f)
# cretaing a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)
# button widgets which can also expand and fill
# in the parent widget entirely
# Button 1
b1 = Button(pane, text = "Click me !",
background = "red", fg = "white", command = test_print)
b1.pack(side = TOP, expand = True, fill = BOTH)
label = tk.Label(pane, textvariable = var)
label.pack(side = BOTTOM, expand = False)
# Execute Tkinter
master.mainloop()
This code runs fine but it doesn't give me a random number when i press the button. I need it to give me a random output so i can get a random element from a list. Does anybody have the answer to this?
Here it is. Now you can see that the label changes:
from tkinter import *
import tkinter as tk
import random
def test_print():
f = random.randint(0, 118)
label.configure(text=str(f))
master.update_idletasks()
# creating Tk window
master = Tk()
master.minsize(600, 400)
# cretaing a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill=BOTH, expand=True)
# button widgets which can also expand and fill
# in the parent widget entirely
# Button 1
b1 = Button(pane, text="Click me !",
background="red", fg="white", command=test_print)
b1.pack(side=TOP, expand=True, fill=BOTH)
label = tk.Label(pane)
label.pack(side=BOTTOM, expand=False)
# Execute Tkinter
master.mainloop()
here is the answer
from tkinter import *
import tkinter as tk
import random
maxnumber = 118
f = random.randint(0,maxnumber)
def test_print():
master.update_idletasks()
label.configure(textvariable = var)
# creating Tk window
master = Tk()
master.minsize(600,400)
var = IntVar()
var.set(f)
# cretaing a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)
# button widgets which can also expand and fill
# in the parent widget entirely
# Button 1
b1 = Button(pane, text = "Click me !",
background = "red", fg = "white", command = test_print)
b1.pack(side = TOP, expand = True, fill = BOTH)
label = tk.Label(pane)
label.pack(side = BOTTOM, expand = False)
# Execute Tkinter
master.mainloop()

Python tkinter how to zoom in widgets

My code:
import tkinter as tk
root = tk.Tk()
for i in range(50):
for j in range(50):
tk.Button(height=1, width=2, bg='Blue').grid(row=j, column=i)
root.mainloop()
I can not see all of the buttons in the screen even when I maxmize the window. so I want to add an option to zoom out (all of the widgets will be smaller) so I can see all of them. How do I do that?
Example code:
import tkinter as tk
root = tk.Tk()
root.state('zoomed')
widgets_to_zoom_list = []
DEFAULT_SIZE = 50
def zoom(widget):
for every_widget in widgets_to_zoom_list:
every_widget.config(width=widget.get(), height=widget.get())
def main():
canvas = tk.Canvas(root)
frame = tk.Frame(canvas)
zoom_scale = tk.Scale(root, orient='vertical', from_=1, to=100)
zoom_scale.config(command=lambda args: zoom(zoom_scale))
zoom_scale.set(DEFAULT_SIZE)
pixel = tk.PhotoImage(width=1, height=1)
for i in range(50):
btn = tk.Button(frame, text=str(i + 1), bg='Blue', image=pixel, width=DEFAULT_SIZE, height=DEFAULT_SIZE, compound="c")
btn.grid(row=0, column=i)
widgets_to_zoom_list.append(btn)
canvas.create_window(0, 0, anchor='nw', window=frame)
# make sure everything is displayed before configuring the scroll region
canvas.update_idletasks()
canvas.configure(scrollregion=canvas.bbox('all'))
canvas.pack(fill='both', side='left', expand=True)
zoom_scale.pack(fill='y', side='right')
root.mainloop()
if __name__ == '__main__':
main()
From what i have been able to understand from your question, i think you want the window to be resizable in tkinter.
To allow a window to be resized by the user, we use the resizable function -:
root.resizable(height = True, width = True)
In this case the two args are height and width which help you customize if you only want the widget to be vertically resizable or only want it to be horizontally resizable.
Your code with this function added should look like this -:
import tkinter as tk
root = tk.Tk()
root.resizable(True, True) #Assuming you want both vertically and horizontally resizable window.
for i in range(50):
for j in range(50):
tk.Button(height=1, width=2, bg='Blue').grid(row=j, column=i)
root.mainloop()
I hope this will solve your problem.
And I also hope you are safe in this time of an ongoing pandemic.

Frame not expanding

Maybe this is just how Tkinter works but Im unsure. Currently I have the Main window with three frames laid out next to each other. Frame ContainerFrame is a master, then characterFrame and planetFrame are placed inside the ContainerFrame. The issue is or what I would like to happen is that the frames would fill up a set area of the window regardless of whether or not their is data/ widgets in them.
Here is what I envision it to look like https://imgur.com/OjdKFh4
from tkinter import *
from tkinter import ttk
MainWindow = Tk()
def mainWindow():
MainWindow.option_add('*tearOff', False)
MainWindow.title("Interface")
MainWindow.geometry('800x600')
menubar = Menu(MainWindow)
MainWindow.config(menu = menubar)
File = Menu(menubar)
About = Menu(menubar)
menubar.add_cascade(menu = File, label = "File")
menubar.add_cascade(menu = About, label = "About")
def frameContainer():
containerFrame = Frame(MainWindow)
containerFrame.pack(anchor = "nw", side = "left", fill = "both", expand = False)
scroller = Scrollbar(orient = "vertical")
characterFrame = Frame(containerFrame, borderwidth="2", relief="sunken")
characterFrame.pack(anchor = "nw", side = "left", expand = True)
planetFrame = Frame(containerFrame ,borderwidth="2", relief="sunken")
planetFrame.pack(anchor = "nw", side = "right", expand = True)
scroller = Scrollbar(orient = "vertical")
scroller.pack(anchor = "e", side = "right", fill = "y", expand = False)
characterLable = Button(characterFrame, text ="Characters")
characterLable.pack()
Label(characterFrame, text ="Test1").pack()
Label(characterFrame, text ="Test2").pack()
Label(planetFrame, text ="Test1").pack()
Label(planetFrame, text ="Test2").pack()
mainWindow()
frameContainer()
MainWindow.mainloop()
Normally you would use a frame to organize a widget with a scrollbar, but a frame is not scrollable. If you want to scroll an area containing other widgets the usual thing to do is to use a canvas.
Study this guide: Tkinter Scrollbar Patterns
Pack can be difficult to use and the only way I have found to overcome this is to keep trying. It's usually easier to see what you are doing if you let the different frames have different bg colors. Also I've taken the liberty to change some of your variable names as they do not give a hint as to what they are or are too similar to other names, eg. mainWindow and MainWindow.
I have added some padding to some widgets to make it look better.
from tkinter import *
root = Tk()
def create_main_window():
root.option_add('*tearOff', False)
root.title("Interface")
root.geometry('400x300+800+50')
menubar = Menu(root)
root.config(menu = menubar)
File = Menu(menubar)
About = Menu(menubar)
menubar.add_cascade(menu = File, label = "File")
menubar.add_cascade(menu = About, label = "About")
def create_container_frame():
container = Frame(root, bg='tan')
container.pack(fill="both", expand=True)
scroller = Scrollbar(container, orient="vertical")
scroller.pack(side="right", fill="y")
characterFrame = Frame(container, bd=2, relief="sunken", bg='thistle')
characterFrame.pack(side="left", fill='y', padx=(10,0), pady=10)
character_button = Button(characterFrame, text ="Characters")
character_button.pack(padx=10, pady=(10,0))
Label(characterFrame, text ="Test1").pack()
Label(characterFrame, text ="Test2").pack()
planetFrame = Frame(container ,bd=2, relief="sunken", bg='khaki')
planetFrame.pack(side="left", fill='both', expand=True, padx=10, pady=10)
Label(planetFrame, text="Test1").pack(pady=(10,0))
Label(planetFrame, text="Test2").pack()
create_main_window()
create_container_frame()
root.mainloop()
Is this the layout you are aiming for?

Categories