如何更改全屏Gtk应用程序的布局? [英] How to change the layout of a Gtk application on fullscreen?

查看:153
本文介绍了如何更改全屏Gtk应用程序的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用Python和Gtk的另一个图像查看器,查看器目前非常简单:它包含一个 GtkWindow 和一个 GtkTreeView 在左侧显示我的图像列表,右侧显示 GtkImage ,显示实际图像。到目前为止,这么好。



现在,我想要全屏显示,只显示黑色背景图像等。



我可以看到以下几种方法:


  • 我可以隐藏()窗口,并显示一个大的 GtkImage 取而代之,但是我放弃了之前在窗口上设置的所有东西(例如信号),并且我隐藏了全屏的窗口,这是有点奇怪;
  • 我可以隐藏()窗口的内容,删除窗口的内容(只有一个孩子,据我所知?),并把一个 GtkImage 在里面(并且在退出全屏时做相反的处理);
  • 我可以尝试使用窗口中的容器,并在窗口全屏/退出全屏时隐藏/显示其内容。更确切地说,我正在考虑添加另一个 GtkHBox 作为我的窗口的直接子对象,并带有两个子对象,并且只显示正常模式下的第一个子对象,第二个在全屏。



这些看起来都有些不好意思,所以我不知道推荐的方式来处理这种情况。感谢!

解决方案

我认为最简单的方法是使用一个布局来设置所有的小部件和信号设置。然后,当您全屏切换进入和退出时,您将看到一组您可以看到且不可见的小部件。尝试下面的演示。这是一个简单的实现,当你按下F11时,进入和退出全屏。 HBox用于制作布局,其中包含左侧的标签和右侧的图像。我用一些虚拟文本填充标签,以便占用大量空间。当您切入和切出全屏时,它将切换标签的可见性,从而使图像既可以全屏显示,也可以与标签共享。为了演示目的,我只是使用了gtk附带的一张图片。下面是两个屏幕截图,展示了全屏的布局。



代码

 进口gtk 

def 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.unfullscreen
action()
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 +'\\\
')* 20)
vbox = gtk.HBox()
vbox.add(标签)
vbox.add(图片)
win.add(vbox)
win.show_all()
gtk .main()

正常窗口





全屏


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!

解决方案

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 gtk

def 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.unfullscreen
        action()
        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

Full screen

这篇关于如何更改全屏Gtk应用程序的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆