How to change the layout of a Gtk application on fullscreen?

2024/9/21 20:57:24

I'm developing another image viewer using Python and Gtk and the viewer is currently very simple: it consists of a GtkWindow with a GtkTreeView on the left side displaying the list of my images, and a GtkImage on the right side, displaying the actual image. So far, so good.

Now, I would like to go fullscreen, and display only the image with a black background, etc.

I can see several ways of doing this:

  • I can hide() the window, and display a big GtkImage instead, but I'm loosing all the stuff that I setup on the window before (signals for example), and I'm hiding the window which goes fullscreen, which is a bit weird;
  • I can hide() the content of the window, remove the content of the window (which can have only one child, as far as I know?), and put a GtkImage inside instead (and do the reverse on exiting fullscreen);
  • I can try to play with the containers inside my window, and hiding/showing their content when the window is going fullscreen/exiting fullscreen. More precisely, I was thinking of adding another GtkHBox as a direct child of my window, with two child, and displaying only the first one on "normal" mode, and only the second one on fullscreen.

That all seem a bit hacky, so I wonder what would be the recommended way to handle this kind of situation. Thanks!

Answer

I think the simplest way to implement this is to have one layout with all your widgets setup and the signals setup. Then when you toggle in and out off fullscreen you have a set of widgets that you make visible and not visible. Try out the demonstration below. It's a simple implementation that goes in and out of fullscreen when you press F11. An HBox is used to make the layout, which contains a label on the left and an image on the right. I've filled the label with some dummy text so that it takes up a good amount of space. As you toggle in and out of fullscreen it will toggle the visibility of the label and thus make the image either take the full screen real estate or share it with the label. I just used one of the stock images that comes with gtk for demonstration purposes. Below are two screenshots showing the layout in and out of fullscreen.

Code

import gtkdef keypress(win, event):if event.keyval == gtk.keysyms.F11:win.is_fullscreen = not getattr(win, 'is_fullscreen', False)action = win.fullscreen if win.is_fullscreen else win.unfullscreenaction()label.set_visible(not win.is_fullscreen)win = gtk.Window()
win.connect("delete-event", gtk.main_quit)
win.connect('key-press-event', keypress)
image = gtk.image_new_from_stock(gtk.STOCK_ABOUT, gtk.ICON_SIZE_DIALOG)
label = gtk.Label(('test ' * 20 + '\n') * 20)
vbox = gtk.HBox()
vbox.add(label)
vbox.add(image)
win.add(vbox)
win.show_all()
gtk.main()

Normal window

enter image description here

Full screen

enter image description here

https://en.xdnf.cn/q/71948.html

Related Q&A

How to upload multiple file in django admin models

file = models.FileField(upload_to=settings.FILE_PATH)For uploading a file in django models I used the above line. But For uploading multiple file through django admin model what should I do? I found t…

Convert numpy array to list of datetimes

I have a 2D array of dates of the form:[Y Y Y ... ] [M M M ... ] [D D D ... ] [H H H ... ] [M M M ... ] [S S S ... ]So it looks likedata = np.array([[2015, 2015, 2015, 2015, 2015, 2015], # ...[ 1, …

PyQt: how to handle event without inheritance

How can I handle mouse event without a inheritance, the usecase can be described as follows:Suppose that I wanna let the QLabel object to handel MouseMoveEvent, the way in the tutorial often goes in th…

DHT22 Sensor import Adafruit_DHT error

So Ive properly attached DHT22 Humidity Sensor to my BeagleBone Black Rev C. Im running OS Mavericks on my MacBook Pro and I followed the directions provided by Adafruit on how to use my DHT22 The webs…

Whats the purpose of package.egg-info folder?

Im developing a python package foo. My project structure looks like this:. ├── foo │ ├── foo │ │ ├── bar.py │ │ ├── foo.py │ │ ├── __init__.py │ ├── README.md …

Implement Causal CNN in Keras for multivariate time-series prediction

This question is a followup to my previous question here: Multi-feature causal CNN - Keras implementation, however, there are numerous things that are unclear to me that I think it warrants a new quest…

How to decode a numpy array of dtype=numpy.string_?

I need to decode, with Python 3, a string that was encoded the following way:>>> s = numpy.asarray(numpy.string_("hello\nworld")) >>> s array(bhello\nworld, dtype=|S11)I tri…

Cosine similarity of word2vec more than 1

I used a word2vec algorithm of spark to compute documents vector of a text. I then used the findSynonyms function of the model object to get synonyms of few words. I see something like this: w2vmodel.f…

Handling empty case with tuple filtering and unpacking

I have a situation with some parallel lists that need to be filtered based on the values in one of the lists. Sometimes I write something like this to filter them:lista = [1, 2, 3] listb = [7, 8, 9] f…

pip3 install pyautogui fails with error code 1 Mac OS

I tried installing the autogui python extension:pip3 install pyautoguiAnd this installation attempt results in the following error message:Collecting pyautoguiUsing cached PyAutoGUI-0.9.33.zipComplete …