Automatic DrawingArea scaling on resize with (Py)GTK - python

How can i scale the gtk.DrawingArea on window resize? I'm writing a code that will draw some colors in DrawingArea that will be added to an gtk.Table cell. I just need, that the image will be stretched in all of space of the cell.
sorry for my english.
thx

First create the table with homogeneous turned off:
table = gtk.Table(rows=1, columns=1, homogeneous=False)
If homogeneous is not turned off, every table cell will be as wide and tall as your DrawingArea.
To attach the DrawingArea to the table:
table.attach(child, left_attach, right_attach, top_attach, bottom_attach,
xoptions=EXPAND|FILL, yoptions=EXPAND|FILL, xpadding=0, ypadding=0)
Leave xoptions and yoptions as EXPAND|FILL to make the DrawingArea expand. If the table is also set to expand, the default when doing toplevelWindow.add(table), then the DrawingArea should automatically resize when the window is.
See this site for more info: pygtk table documentation

Related

How can I make a tkinter canvas hold an image much greater than the available screen size?

I'm attempting to adapt martineau's excellent answer on how to use tkinter to highlight and select an area of an image with a mouse. My goal is to modify the code to support huge images, such as 10000x6000 pixels or more!
This is my first attempt at using tkinter, and it's much more challenging than expected.
The three options which I've considered and experimented with are:
Load the image, then scale the image to the size available in the window before placing it on the canvas. This in turn requires that I know the scale factor, so I can reverse the scaling when operating on the original image.
Add scrollbars to the side/button on the visible part of the image in the canvas. (My preferred option).
Don't add scrollbars, but use the cursor keys to scroll the visible part of the image in the canvas.
I've tried to do all of them, but ran into various kinds of trouble:
I used root.state('zoomed') to maximize the window, then placed a button to top of the window using tk.Button(...).pack(fill=tk.X) before placing the canvas as in the linked code using self.canvas = tk.Canvas(...). At this time I discovered two issues: It turns out that I can't maximize the canvas to the remaining space in the window using self.canvas.pack(fill=tk.BOTH) (it only expands the width). Another more crucial issue was that I could not read the actual width/height value that the canvas was expanded to, (or get there available space in the window before placing the canvas), and without these values, I can't calculate the scaling factor. :-(
I tried to follow various methods of adding scrollbars to the packed canvas but they all, with one exception, required to use the grid method and I quickly found that pack and grid methods were conflicting methods. After several attempts at rewriting the code to use the grid method, I gave up. The place method seemed promising, except that it will require me to size the canvas manually and then place the scrollbars at calculated offsets to the canvas... and all of this required that I have the same values that were needed to calculate the scaling factor above. :-(
I didn't get very far with this, as it seems I can't get (keyboard?) binds to work at all (On windows with Python 3.10.6). Shouldn't something like self.canvas.bind("<Right>", lambda event: print("Ping")) followed by self.canvas.focus_set() (perhaps with root instead of the canvas) work?
I'm sure I'm overlooking something basic, but after a few evenings looking at this I've gotten nowhere. :-(
I hope some of you can provide help and/or pointers allowing me to get further.
Ohh, for completeness, I'm trying to hack together a small tool that does the following:
On startup requests and load an image file. (Adding a button to perform the load/re-load is nice to have, but not strictly needed. )
Display the image file and allow an area of the image to be selected with the mouse.
Perform some software analysis on the selected part of the image when a button is pressed.

Add border to table in pygtk

I am trying to use gtk table in one of my projects(GUI based) to show a pair of columns and its values. Since gtk table doesn't have borders by default, my GUI is not coming up well.
So,
Is there anyway I can add borders to gtk table and its cells?
If no can I create a customized widget with borders by extending gtk Table?
How can I create a customized widget in pygtk?
P.S: I have tried gtk Treeview and gtk Frames and thats not what I want. Also I have created a gtk treeview for each pair of columns and its values but the GUI works very very slow.
Assuming you want five pixels of spacing, if you would like to have space between the different cells of your table, you can try table.set_row_spacings(5); table.set_col_spacings(5); If you would like to have space around the entire table, you can do table.set_padding(5).

wxPython Slider background colour incomplete change

wxPython 2.8.10, Python 2.6.2, Windows 7 x64
So I am having a bit of a wxPython issue I am hoping someone can say where I am going wrong. In my application I have Sliders where the background colour changes as you move the slider around. I do this via SetBackgroundColour from within a EVT_SLIDER handler. It mostly works, but I end up with a border of unchanged colour around the outer edge of the widget.
I have tried Refresh up the chain, RefreshRect on the top level Frame around the widget (and a few pixels further out), various combinations of Update and Layout, but nothing seems to erase it. The best I have managed is if I use something like:
frame = wx.GetTopLevelParent(slider)
rect = slider.GetRect()
slider.ClearBackground()
slider.SetBackgroundColour(wx.Colour(red,green,blue))
frame.RefreshRect(rect)
It will correctly draw the background box with the new colour without the border issue, but it will not repaint the slider itself.
However, if I click away so the focus is elsewhere, the border snaps to the new colour.
I could probably use an overlay or custom paint function but I am hoping there is something I am missing here that will get me the desired results.
Any suggestions?
(edited to add)
Looks like resizing the window manually corrects the issue, but Layout() does not.
It also functions correctly if you do the client size trick:
w,h = slider.GetClientSize()
slider.SetClientSize( (w+1,h) )
slider.SetClientSize( (w,h) )

How do I disable resizable rows in a wxPython Grid?

I'm using a wx.grid.Grid in my application. Its never going to be useful to resize the rows of the grid, so I would love to disable the ability to resize them. Is this possible, and how would I go about doing it?
Justification
When testing the application I keep accidentally clicking the surprisingly large border between cells when I'm trying to click the cell body. The rows can't be much more than 20px tall, and the clickable (draggable) border is at least 3px across. In addition to the fact that I often accidentally rescale a row when I intended to select multiple cells, I really think it's worth disabling resizing. An acceptable alternative would be to disable dragging of the borders between the cells entirely, but still enable resizing of rows and columns by the border between the labels along the top and left edge.
DisableDragRowSize() is what you need.

How can I add scrollable whitespace at the bottom of a QTreeView

When expanding an item at the bottom of a QTreeView, the view does not automatically scroll to show the newly-expanded items. I can fix this by detecting expansion and performing the scroll myself when appropriate.
However, I would instead like to allow the user to scroll the view farther than is currently allowed. Currently, if the tree is too tall to fit in the visible area, the view can be scrolled only until the bottom-most row comes into view.
I believe this should be doable by tricking the QTreeView's size calculation, but even after source diving I don't understand the interaction between QTreeView and its base QAbstractScrollArea well enough to know what to poke, or where to start poking.
If all else fails I may just add some dummy, non-editable rows to my data model.
you can add extra white space to the treeview by increasing its vertical scrollbar maximum value. Smth like this:
max = self.treeview.verticalScrollBar().maximum()
self.treeview.verticalScrollBar().setMaximum(max*2)
hope this helps, regards

Categories

Resources