Resize Tkinter listbox when window resizes with Grid - python

I'm working on a Tkinter application using the Grid geometry manager (It's my first time using Grid, and I love it! :D) that contains a scrolling listbox that displays options whenever a user selects an option.
It's working well, but the window is small and ugly. When I maximize it, everything else resizes fine (thanks to columnconfigure) but the listbox stays the same height. Is there a simple way to fix this?
(I have seen this question but it's for Pack, not Grid)
Thanks in advance.
Code sample because one was asked for:
self.tasklist = Listbox(self.frame, exportselection=0)
self.tasklist.grid(row=1, sticky=W+E+N+S)
yscroll = Scrollbar(self.frame, command=self.tasklist.yview, orient=VERTICAL)
yscroll.grid(row=1, column=1, sticky=N+S)

Without seeing more of your code it's impossible to say. Most likely your listbox is expanding properly, but your self.frame is not. Though, I don't see you giving any weight to the row and column that the listbox is in, so that could be a factor.
An easy way to debug this is to give self.frame a garish color that will stick out (red, bright green, etc). Then it will be easy to see if the listbox is properly resizing inside the frame, and if the frame is properly resizing inside its parent.

Related

how to set window size with frame? [duplicate]

This is the code that's giving me trouble.
f = Frame(root, width=1000, bg="blue")
f.pack(fill=X, expand=True)
l = Label(f, text="hi", width=10, bg="red", fg="white")
l.pack()
If I comment out the lines with the Label, the Frame displays with the right width. However, adding the Label seems to shrink the Frame down to the Label's size. Is there a way to prevent that from happening?
By default, both pack and grid shrink or grow a widget to fit its contents, which is what you want 99.9% of the time. The term that describes this feature is geometry propagation. There is a command to turn geometry propagation on or off when using pack (pack_propagate) and grid (grid_propagate).
Since you are using pack the syntax would be:
f.pack_propagate(0)
or maybe root.pack_propagate(0), depending on which widgets you actually want to affect. However, because you haven't given the frame height, its default height is one pixel so you still may not see the interior widgets. To get the full effect of what you want, you need to give the containing frame both a width and a height.
That being said, the vast majority of the time you should let Tkinter compute the size. When you turn geometry propagation off your GUI won't respond well to changes in resolution, changes in fonts, etc. Tkinter's geometry managers (pack, place and grid) are remarkably powerful. You should learn to take advantage of that power by using the right tool for the job.

Tkinter scrolling (scrollbar on frames)

I am still new to this and I need some help in inserting a scrollbar to scroll the circled area just in case the list gets long in future. Thanks in advance.
The image is included here.(the circled area is actually an individual frame so is there any method to add scrollbar to that?
The usual way to do this is to create a canvas with scrollbars and then place the frame which contains the checkbox widgets on the canvas with create_window.

Tkinter - scale all components on resize?

Is it possible that when a Tkinter window is made bigger or larger, everyhting in the window is scaled?
So all the proportions stay the same but their sizes vary.
Now when the window is resized all the buttons etc stay the same so I disabled resize because there is no point, it just looks bad.
No, it is likely not possible, depending on what you mean by "everything" and what you mean by "scaled". Any widget can be made to stretch to fill its allotted space. Text widgets and canvas widgets, for example, scale nicely. A button or label will fill the space it's in, but the text inside the widget won't change (that is also true of text and canvas widgets).
It's possible to organize your GUI so that when the window resizes, everything remains in its proper place at its original size. Having buttons that automatically scale is not something most people would expect. Disabling resizing usually results in a poor user experience -- users should have the ability to make a window larger or smaller.

python tkinter canvas 'transparent'

I think transparent is the right word I'm looking for. Hopefully my description will explain it properly no matter what.
I have a graph of time based data going back decades. Pretty much simply setup to show one day per x-pixel, unless zoomed in closer. I would like to have a box, maybe along the lines of 5x100, appear on top of the graph so when I move the mouse over the graph the box will move and keep pace with the mouse. Anotherwords showing what was happening in the 5 furthest days 'x number of days prior'. Anotherwords when computing an average going forward what are the next values to be falling off as new data arrives. Naturally I want the underlying graphed data to be displayed with the transparent box on top of it outlining the days in question. This may get crazy enough to be a much wider box with two areas that are colored light grey or something like that to show the areas in question but the colored areas are separated by numerous days(could be multiple transparent windows that are tracked together as well. Is this feasible with tkinter? From the research I've been doing it's questionable if using
root.attributes('alpha', .30)
would work or not. It doesn't sound like I could do something like as it would end up making the graph transparent to whatever is underneath it.
self.Graph.create_line()
self.Box.attributes('alpha', .30)
If I understand correctly I have to use
attributes
right at the root level versus the individual 'window' level so the above (severely chopped down) code wouldn't work...or would it. I haven't had a chance to try anything out yet to see what happens...that will be later on this evening. Kinda hoping to save myself a little time by asking now and you never know who else may need the help sometime.
If I understand what you're trying to do, it's going to be pretty hard.
Setting window attributes works just fine to make a window transparent. You've got a minor problem in the code—attributes start with a hyphen—but otherwise you've got it right:
self.Box.attributes("-alpha", .30)
However, it sounds like you want that Box to be an embedded part of the graph window, not its own top-level window that can be dragged around by the user, etc. Unfortunately, Tkinter does not have any notion of child windows, and it doesn't expose nearly enough of the native stuff you'd need to fake them by creating an immobile window and manually moving it to track the movements of another window. So, you don't have a window, which means you don't have window transparency.
The obvious thing for Box to be is some kind of widget, like a Frame or Canvas. But widgets don't have transparency.
Box could instead be just a collection of elements drawn onto the same Canvas as the Graph. That seems promising… but none of the Canvas methods handle alpha transparency. (Some of them do handle all-or-nothing transparency, but that doesn't help.)
One thing that does handle transparency is PhotoImage. So, if you draw Box off-screen, get the resulting contents as a PhotoImage, add the alpha (e.g., via PIL), then create_image the result… close, but no cigar. Depending on the settings of the underlying Tk library, Tkinter may just draw the pixmap with 1-bit transparency or ignore transparency completely. (Experiment with loading alpha-transparent PNG files in PIL and drawing them on a Canvas.) So, unless you want an app that looks right on some systems, doesn't draw the Box at all on others, and draws it opaque on others, this is a dead end.
So, what's left? Only manual compositing: Draw the Graph and the Box on separate off-screen windows, get the pixmaps, use PIL to compose them, and create_image the result.
At which point you're probably better off just using something like PIL's ImageDraw or a more powerful library to construct the pixmap in the first place. Or, of course, using a more powerful GUI library than Tk, like Qt or wx.
Maybe this can give you some ideas to play with:
from tkinter import *
root = Tk()
c = Canvas(root, width=640, height=480, bd=0, highlightthickness=0)
c.create_line(0,240,640,240, fill='blue')
c.pack()
#pil image with transparency
try:
from PIL import Image, ImageTk
except ImportError:
pass
else:
pim = Image.new('RGBA', (5,100), (0,255,0,64))
photo = ImageTk.PhotoImage(pim)
c.create_image(200,200, image=photo, anchor='nw')
#blank standard photoimage with red vertical borders
im = PhotoImage(width=7, height=480)
dat = ('red',)*480
im.put(dat, to=(0,0))
im.put(dat, to=(6,0))
box = c.create_image(0, 0, image=im, anchor='nw')
def on_motion(event):
left,top = c.coords(box)
dx = event.x - (left+7)
c.move(box, dx, 0)
c.bind('<Motion>', on_motion)
root.mainloop()

tkinter create_window erases previous window

For some reason, when I go to create_window in my Tkinter canvas, it erases everything that was previously in said window, and jams the window in the top left corner (even though I set it somewhere else.
canvas.create_window(30, height - 40, anchor = NW, width = 40,
window = canvas.data.buildSquareButton)
precedes
canvas.create_rectangle(0,0,width, 40, fill = "#888888888",
outline = "#888888888")
canvas.create_rectangle(0, height, width, (height-40), fill = "#888888888",
outline = "#888888888")
canvas.create_rectangle(0, 40, width, (height - 40), fill = "#fffffffff",
outline = "#fffffffff")
and an image.
I put in a 1 second time.sleep after the create_window, and I could see that the button was put in the right place. Then after the time.sleep was over, the button threw itself in the top right corner and the rectangle never appeared. I commented out the window, and the rectangles appeared fine.
Am I doing something wrong when I call the window, or is there a Tkinter glitch?
There's not enough information in your question to know for sure. However, my guess is that you are packing or griding a widget in the canvas, and that's causing the canvas to shrink to fit its contents. Or, you're doing something else to cause the canvas to shrink.
To compound the problem, your canvas probably has the same background color as your main window, so you think the contents are being erased, but in reality you're looking at the widget that the canvas is in rather than the canvas itself.
To help prove or disprove that theory, give your canvas a garish background color, such as a bright red. Then run your code and see what happens to the red part of the screen.
Bottom line: there is no bug in tkinter that would cause the behavior you describe. There is a bug in some code that you aren't showing us.
The best thing is for you to create the smallest possible program that reproduces the problem. The mere act of trying to do that may expose the bug in your code. If you are able to reproduce it in a dozen or two lines of code, update the question and we can probably spot the error.
I'm not entirely sure what your question is, but it looks like you're trying to remove everything from a canvas widget at one point in your code to allow for other things to override it, yes?
Try this to reset the canvas: canvas.delete("all")

Categories

Resources