tkinter elements not resizing with window, using pack - python

I have the current code below for some basic parameter entry into an AI assignment. It is just there to st the starting parameters and display the outpit of the different algorithms implemented, however the box that contains the output will not resize? I think I am doing something wrong with maybe the parent-child structure but I can't figure out what.
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
self.mainframe= tk.Frame(master=self, width=768, height=576)
self.mainframe.pack(fill=tk.BOTH, expand=1)
self.xsizelabel = tk.Label(self.mainframe, text="Size (X)")
self.xsizelabel.pack(side="top")
self.xsize = tk.Entry(self.mainframe)
self.xsize.insert(0, 2)
self.xsize.pack(side="top")
self.ysizelabel = tk.Label(self.mainframe, text="Size (Y)")
self.ysizelabel.pack(side="top")
self.ysize = tk.Entry(self.mainframe)
self.ysize.insert(0, 1)
self.ysize.pack(side="top")
self.xstartlabel = tk.Label(self.mainframe, text="Starting Position (X)")
self.xstartlabel.pack(side="top")
self.xStart = tk.Entry(self.mainframe)
self.xStart.insert(0, 0)
self.xStart.pack(side="top")
self.ystartlabel = tk.Label(self.mainframe, text="Starting Position (Y)")
self.ystartlabel.pack(side="top")
self.yStart = tk.Entry(self.mainframe)
self.yStart.insert(0, 0)
self.yStart.pack(side="top")
self.outputstartlabel = tk.Label(self.mainframe, text="Output")
self.outputstartlabel.pack(side="top")
self.separator = tk.Frame(master=self.mainframe, width=768, height=576, bd=1)
self.separator.pack(fill=tk.BOTH, padx=5, pady=5)
self.output = tk.Scrollbar(self.separator)
self.output.pack(side=tk.RIGHT, fill=tk.Y)
self.listbox = tk.Listbox(self.separator, yscrollcommand=self.output.set)
self.listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
self.run_button = tk.Button(self.mainframe)
self.run_button["text"] = "Run with these settings"
self.run_button["command"] = self.runAlgorithm
self.run_button.pack(side="top")
self.quit = tk.Button(self.mainframe, text="QUIT", fg="red",
command=self.master.destroy)
self.quit.pack(side="bottom")
but the resulting window looks like this:
default
expanded
nothing expands when I expand the window, dispite setting the autofill and expand options. what am I doing wrong?

I can't run your program because you didn't present the whole thing. I see that you have set the fill and expand options on self.mainframe, but you didn't set those options in the constructor. Therefore the base window, which contains self.mainframe, will not expand to fill its available space. You need to make all the parent windows expandable, because when you drag the edges of the main window you are acting on the top level frame.

Related

tkinter.Toplevel size automatically fit its widgets

I have made my custom infobox class, InfoBox, that I am using in my application. The tk.messagebox.showinfo did not suit my needs to the poor shape. But InfoBox does not adjust its size to fit the widgets I place inside. How can I make it as small as possible without cutting the widgets?
The class receives a string, msg, and a PhotoImage object, image, which are placed in the InfoBox. I added a screenshot of one such InfoBox.
class InfoBox(tk.Toplevel):
def __init__(self, parent, msg, image):
tk.Toplevel.__init__(self, parent)
self.parent = parent
self.msg = msg
self.image = image
self.title = "Gassy"
self.font = font.Font(family="Optima", size=20)
frame_left = tk.Frame(self)
frame_right = tk.Frame(self)
frame_left.grid(row=0, column=0, sticky=tk.NSEW)
frame_right.grid(row=0, column=1, sticky=tk.NSEW)
tk.Label(frame_left, image=self.image).grid(row=0, column=0, sticky=tk.N)
textbox = tk.Text(frame_right, font=self.font)
textbox.grid(row=0, column=0)
textbox.insert(tk.END, self.msg)
textbox.config(state=tk.DISABLED)
tk.Button(frame_left, text="Den er grei!", font=self.font, command=self.destroy).grid(row=1, column=0)
As #kevin mentioned, it works as intended, the textwidget is mostly empty and occupies a large blank area, this is what makes you think that the geometry manager is not shrinking the window to the widgets.
this:
(I removed the images and fonts that were not provided, and unnecessary)
import tkinter as tk
class InfoBox(tk.Toplevel):
def __init__(self, parent, msg):
tk.Toplevel.__init__(self, parent)
self.parent = parent
self.msg = msg
self.title = "Gassy"
frame_left = tk.Frame(self)
frame_right = tk.Frame(self)
frame_left.grid(row=0, column=0, sticky=tk.NSEW)
frame_right.grid(row=0, column=1, sticky=tk.NSEW)
# textbox = tk.Text(frame_right)
# textbox.grid(row=0, column=0)
# textbox.insert(tk.END, self.msg)
# textbox.config(state=tk.DISABLED)
tk.Button(frame_left, text="Den er grei!", command=self.destroy).grid(row=1, column=0)
root = tk.Tk()
info = InfoBox(root, '123 ' * 1000)
root.mainloop()
produces that:
whereas that:
import tkinter as tk
class InfoBox(tk.Toplevel):
def __init__(self, parent, msg):
tk.Toplevel.__init__(self, parent)
self.parent = parent
self.msg = msg
self.title = "Gassy"
frame_left = tk.Frame(self)
frame_right = tk.Frame(self)
frame_left.grid(row=0, column=0, sticky=tk.NSEW)
frame_right.grid(row=0, column=1, sticky=tk.NSEW)
textbox = tk.Text(frame_right)
textbox.grid(row=0, column=0)
textbox.insert(tk.END, self.msg)
textbox.config(state=tk.DISABLED)
tk.Button(frame_left, text="Den er grei!", command=self.destroy).grid(row=1, column=0)
root = tk.Tk()
info = InfoBox(root, '123 ' * 1000)
root.mainloop()
produces this:
Clearly, the Toplevel subclass adjusts its size to the widgets it contains
The test widget is displayed at a certain size, regardless of its content. The Toplevel resizes around the widgets, NOT around whatever is inserted in the text widget; like with a text processor rudimentary window, the text processor does not shrink or expand as text is typed or edited. The same applies here.
The keyword args width and height allow to configure the size (as a number of characters, or lines) of a text widget

Pararell instance of tkinter application window python

I want to create some simple tkinter python app (like StickyNotes on Windows), i have create the class mainApplication and i do not know how to by just simply triggering the button create another instance of this class which will be displayed pararell to other window (or even multiple windows). I know how to assigned function to pushButton, and other simple stuff but the problem is with this pararell window displaying. Thanks in advance for help.
class mainApplication(Frame):
_ids = count(0)
def __init__(self, parent):
""" """
self.id = next(self._ids)
Frame.__init__(self, parent)
self.parent = parent
self.parent.minsize(width=200,height=100)
self.parent.geometry(('%dx%d+%d+%d' % (200, 100, 1700, 0+self.id*100)))
self.initUI()
def initUI(self):
""" """
self.parent.title("a2l")
self.pack(fill=BOTH, expand=True)
style = Style()
style.configure("TFrame", background="#333")
frame1 = Frame(self, style="TFrame")
frame1.pack(fill=X)
self.lbl0 = Label(frame1, text="api", width=7, background="#333", foreground = "red")
self.lbl0.pack(side=TOP, padx=5, pady=5)
self.closeButton = Button(self, text="new", command = self.createNewInstance)
self.closeButton.pack(side=RIGHT, padx=5, pady=5)
#=======================================================================
# self.generateButton = Button(self, text="GENERATE", command = self.)
# self.generateButton.pack(side=RIGHT, padx=5, pady=5)
#=======================================================================
def createNewInstance(self):
y = mainApplication()
return y
if __name__ == "__main__":
root = Tk()
x = mainApplication(root).pack(side="top", expand=False)
Tk().mainloop()
You shouldn't create more than one Tk() window in one application with tkinter. Instead tkinter provides a widget called Toplevel which can be useful for this kind of thing.
It creates another window which can exist along side the Tk() window and alongside other Toplevel widgets.
You could use this to create a series of persistent windows with whatever text or widgets you wanted on them at any kind of trigger including a Button.
See my example below for a demonstration:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.top = [] #list to contain the Toplevel widgets
self.entry = Entry(self.root)
self.button = Button(self.root, text="Create window", command=self.command)
self.entry.pack()
self.button.pack()
def command(self): #called when button is pressed
self.top.append(Toplevel(self.root)) #adds new Toplevel to the list
Label(self.top[len(self.top)-1], text=self.entry.get()).pack() #Adds label equal to the entry widget to the new toplevel
root = Tk()
App(root)
root.mainloop()

how do i put dynamically added entry fields in a set frame or canvas that does not resize

i have dynamically addable and delete able entry fields that i want to set inside a frame or canvas inside of a main frame but when i try the frame dissappears or dynamically grows with the entry fields. i want the canvas to use the scrollbar if entry fields exceed the window size.
from tkinter import *
import tkinter as tk
class Demo2:
def __init__(self, master):
global rows
self.master = master
self.frame = tk.Frame(self.master)
master.title("test")
self.frame.pack()
addboxButton = Button(self.frame, text='<Add Time Input>', fg="Red", command=self.addBox)
addboxButton.pack()
this is where my buttons are added and deleted.
def addBox(self):
def delete():
delboxButton.grid_remove()
ent1.delete(0,END)
ent2.delete(0,END)
ent1.grid_remove()
ent2.grid_remove()
root = self.frame
frame=Frame(root,width=900,height=900)
frame.pack()
canvas=Canvas(frame,bg='#FFFFFF',width=700,height=300,scrollregion=(0,0,700,300))
vbar=Scrollbar(frame,orient=VERTICAL)
vbar.pack(side=RIGHT,fill=Y)
vbar.config(command=canvas.yview)
canvas.config(width=700,height=300)
canvas.config(yscrollcommand=vbar.set)
canvas.pack(side=LEFT,expand=TRUE,fill=BOTH)
I am trying to figure out now how to make the first set of entry start out on the screen when its opened. and bind the add call to an action.
i = 0
ent1 = Entry(canvas)
ent1.grid(row=i, column=0,sticky="nsew")
i += 1
i = 0
ent2 = Entry(canvas)
ent2.grid(row=i, column=1,sticky="nsew")
i += 1
delboxButton = Button(canvas, text='delete', fg="Red", command=delete)
delboxButton.grid(row=0 ,column=2)
root = tk.Tk()
root.title("test Complete")
root.geometry("500x500")
app = Demo2(root)
root.mainloop()
The normal way this is tackled is to create a single frame and add it to the canvas with the canvas create_window method. Then, you can put whatever you want in the frame using pack, place or grid.
For a description of the technique see Adding a scrollbar to a group of widgets in Tkinter
Here's an example illustrating how the technique works for widgets created by a button. I didn't include the delete functionality or the ability for everything to resize properly to keep the example short, but you seem to have a pretty good idea of how to make the delete function work and I don't know exactly what sort of resize behavior you want.
import tkinter as tk
class Demo2:
def __init__(self, master):
self.master = master
self.entries = []
self.canvas = tk.Canvas(master, width=400, height=200)
self.vsb = tk.Scrollbar(master, orient="vertical", command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.vsb.set)
self.add_button = tk.Button(master, text="Add", command=self.add)
self.container = tk.Frame()
self.canvas.create_window(0, 0, anchor="nw", window=self.container)
self.add_button.pack(side="top")
self.vsb.pack(side="right", fill="y")
self.canvas.pack(side="left", fill="both", expand=True)
# start with 3 entry widgets
self.add()
self.add()
self.add()
def add(self):
entry = tk.Entry(self.container)
entry.pack(side="top", fill="x")
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
self.entries.append(entry)
root = tk.Tk()
demo = Demo2(root)
root.mainloop()

Using scrollbars with canvas in tkinter

I'm trying to put together a window that displays a bunch of labels generated from a dict. I'm having trouble getting the scrollbars to work properly. They won't stick to the sides of the frame when I resize the window, and I can't get the canvas to respond to the scroll command. I need the window to support a large number of labels.
from Tkinter import *
from math import floor
bits = {}
#the dict is then built
class Bitbox(Canvas):
def __init__(self, parent, bitdict, *args, **kwargs):
Canvas.__init__(self, parent, background="black")
self.bitdict = bitdict
self.parent = parent
self.lbllist = []
n=0
for i in bitdict.keys():
label = Label(self, text=i, bg='black', fg='green')
n += 1
label.grid(row = ((n-1)%30), column=int(floor((n-1)/30)))
self.lbllist.append(label)
def main():
root = Tk()
frame = Frame(root)
frame.grid(sticky=N+S+E+W)
bts = Bitbox(frame, bits)
bts.grid(row=0, column=0)
vbar = Scrollbar(frame, orient=VERTICAL)
vbar.grid(row=0, column=1, sticky=N+S)
vbar.config(command=bts.yview)
hbar = Scrollbar(frame, orient=HORIZONTAL)
hbar.grid(row=1, column=0, columnspan=2, sticky=W+E)
bts.config(xscrollcommand=hbar.set)
hbar.config(command=bts.xview)
bts.config(yscrollcommand=vbar.set)
bts.config(scrollregion=(0,0,500,1000))
root.mainloop()
Clearly I'm new at all this. It's entirely possible I have a fundamental misunderstanding of how these widgets interact. Any help is much appreciated.
to get the scrollbar to react to the mouse bind the mouse to the scrollbar like this:
def on_mousewheel(event):
bts.yview_scroll(-1*(event.delta/120), "units")
def main():
global bts
#your code...
root.bind_all("<MouseWheel>",on_mousewheel)

Adding python functionality to Tkinter front end

This is the front end I developed for my application using Tkinter:
from Tkinter import *
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Simple")
self.pack(fill=BOTH, expand=1)
frame = Frame(self, relief="flat", borderwidth=1)
label=Label(frame,text="Scope:")
label.pack(side="left", fill=None, expand=False)
var = StringVar()
var.set("today")
list = OptionMenu(frame, var, "today","yesterday","this week","last week","this month","last month")
list.pack(side="left", fill=None, expand=False)
fetchButton = Button(frame, text="Fetch",command=self.handle(var))
fetchButton.pack(side="left", fill=None, expand=False)
frame.grid(row=1,column=1,pady=4,padx=5,sticky=W)
area = Text(self,height=15,width=60)
area.grid(row=2,column=1,rowspan=1,pady=4,padx=5)
scroll = Scrollbar(self)
scroll.pack(side=RIGHT, fill=Y)
area.config(yscrollcommand=scroll.set)
scroll.config(command=area.yview)
scroll.grid(row=2, column=2, sticky='nsew')
quitButton = Button(self, text="Cancel",command=self.quit)
quitButton.grid(pady=4,padx=5,sticky=W,row=3, column=1)
root = Tk()
app = Example(root)
root.mainloop()
Where exactly do I have to put the handle() method so it can write repeatedly to the text widget? When I put handle() within the Example class and use self.area.insert(), it shows an error saying
Example instance has no attribute 'area'
Please help out.
You need to pass the function object to the Button instance, not a function call. i.e.
fetchButton = Button(frame, text="Fetch",command=self.handle)
To make the handle work in the context of the rest of the code:
from Tkinter import *
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.parent.title("Simple")
self.pack(fill=BOTH, expand=1)
self.init_ui()
def init_ui(self):
self.frame = Frame(self, relief="flat", borderwidth=1)
self.frame.grid(row=1,column=1,pady=4,padx=5,sticky=W)
self.label=Label(self.frame,text="Scope:")
self.label.pack(side="left", fill=None, expand=False)
self.var = StringVar()
self.var.set("today")
self.list = OptionMenu(self.frame, self.var, "today","yesterday",
"this week","last week","this month",
"last month")
self.list.pack(side="left", fill=None, expand=False)
self.fetchButton = Button(self.frame, text="Fetch",command=self.handle)
self.fetchButton.pack(side="left", fill=None, expand=False)
self.area = Text(self,height=15,width=60)
self.area.grid(row=2,column=1,rowspan=1,pady=4,padx=5)
self.scroll = Scrollbar(self)
self.scroll.pack(side=RIGHT, fill=Y)
self.area.config(yscrollcommand=self.scroll.set)
self.scroll.config(command=self.area.yview)
self.scroll.grid(row=2, column=2, sticky='nsew')
self.quitButton = Button(self, text="Cancel",command=self.quit)
self.quitButton.grid(pady=4,padx=5,sticky=W,row=3, column=1)
def handle(self):
self.area.delete(1.0, END)
self.area.insert(CURRENT,self.var.get())
if __name__ == "__main__":
root = Tk()
app = Example(root)
root.mainloop()
Declaring your widgets as attributes will save you a lot of pain an suffering as your application expands. Also keeping references to everything in Tk can stop some unwanted garbage collection, particularly with images in Label instances.
It is also worth noting that using grid and pack interchangeably is likely to lead to bugs later on.

Categories

Resources