我已经尝试通过遵循像here这样的一些指南,在Xubuntu 14.04上使用Python进行GUI编程。我希望在我的GUI中有一个背景图像,上面有任何按钮。 I searched on stackoverflow and came across a person having a similar issue。他的解决方案有效,但网格填满整个窗口,我再也看不到背景图像了。我该如何解决?这是我的代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-:
from gi.repository import Gtk #Here we are importing the GTK3 library to use in our program
class mywindow(Gtk.Window):
def __init__(self):
#In the class's constructor, we have to call the constructor of the super class. In addition, we tell it to set
#the value of the property title to Media Manager v1.0
Gtk.Window.__init__(self, title="Media Manager v1.0") #Creates an Empty Window
#use an overlay for the background image
self.overlay = Gtk.Overlay()
self.add(self.overlay)
self.background = Gtk.Image.new_from_file('Background.jpg')
self.overlay.add(self.background)
self.grid = Gtk.Grid()
self.button1 = Gtk.Button(label="Hello")
self.button1.connect("clicked", self.on_button1_clicked)
self.grid.add(self.button1)
self.overlay.add_overlay(self.grid)
#Gtk.Window.set_default_size(self, 400,325)
Gtk.Window.set_position(self, Gtk.WindowPosition.CENTER)
def on_button1_clicked(self, button):
print "Hello"
def on_button2_clicked(self, button):
print "Exiting..."
def main():
window = mywindow()
#connect to the window's delete event to ensure that the application is terminated if we click on the x to close the window
window.connect("delete-event", Gtk.main_quit)
#Display the window
window.show_all()
#Finally, start the GTK+ processing loop which we quit when the winndow is closed.
Gtk.main()
return 0
if __name__ == '__main__':
main()
答案 0 :(得分:0)
在创建网格和/或按钮时传递valign
和halign
属性的值。这些属性的默认值为Gtk.Align.FILL
,这使得它们可以覆盖给予它们的所有空间。而是选择Gtk.Align.START
,Gtk.Align.END
或Gtk.Align.CENTER
。