Python Gtk 按钮

标签 python python-2.7 python-3.x gtk gtk3

我正在使用 GUI 创建这个示例工具,代码运行没有任何问题。但是,有一件事我想不通,就是如何让按钮始终出现在 GUI 的中心,而不是事情 GUI 如何调整大小?我尝试了不同的方法来实现,但没有一个有效。有人能帮帮我吗!!!非常感谢您。

import gi
gi.require_version('Gtk','3.0')
from gi.repository import Gtk as gtk
import os

software_list = [("Firefox", 2002,  "C++"),
             ("Eclipse", 2004, "Java" ),
             ("Pitivi", 2004, "Python"),
             ("Netbeans", 1996, "Java"),
             ("Chrome", 2008, "C++"),
             ("Filezilla", 2001, "C++"),
             ("Bazaar", 2005, "Python"),
             ("Git", 2005, "C"),
             ("Linux Kernel", 1991, "C"),
             ("GCC", 1987, "C"),
             ("Frostwire", 2004, "Java")]

class WB_Window(gtk.Window):
     def __init__(self):
     gtk.Window.__init__(self, title="Write Blocker")
     self.set_border_width(10)
     self.set_position(gtk.WindowPosition.CENTER)
     self.set_default_size(1000, 450)

     self.outter_box = gtk.Box(gtk.Orientation.HORIZONTAL, spacing=10)
     self.outter_box.set_homogeneous(True)
     self.add(self.outter_box)

     grid = gtk.Grid()
     grid.set_row_spacing(10)
     grid.set_column_homogeneous(True)
     self.outter_box.add(grid)


    self.software_liststore = gtk.ListStore(str, int, str)
    for software_ref in software_list:
        self.software_liststore.append(list(software_ref))
    tree = gtk.TreeView(self.software_liststore)

    for i, column_title in enumerate(["Software", "Release Year", "Programming Language"]):
        renderer = gtk.CellRendererText()
        column = gtk.TreeViewColumn(column_title, renderer, text=i)
        tree.append_column(column)

    self.scrollable_treelist = gtk.ScrolledWindow()
    self.scrollable_treelist.set_vexpand(True)
    grid.add(self.scrollable_treelist)
    self.scrollable_treelist.add(tree)

    hbox = gtk.ButtonBox.new(gtk.Orientation.HORIZONTAL)
    hbox.set_homogeneous(True)
    valign = gtk.Alignment(xalign=1.0, yalign=1.0, xscale=1.0, yscale=1.0)
    hbox.pack_end(valign,False,False,0)
    grid_2 = gtk.Grid()

    grid.attach_next_to(grid_2,None,gtk.PositionType.BOTTOM,1,1)
    grid_2.add(hbox)


    button_mount = gtk.Button(label="Mount")
    hbox.add(button_mount)
    button_ro = gtk.Button(label="Read-Only")
    hbox.add(button_ro)
    button_rw = gtk.Button(label="Read-Write")
    hbox.add(button_rw)
    button_quit = gtk.Button(label="Quit",stock=gtk.STOCK_QUIT)
    button_quit.show()
    hbox.add(button_quit)

win = WB_Window()
win.connect("delete-event",gtk.main_quit)
win.show_all()
gtk.main()

最佳答案

您有许多冗余容器。删除了一些:

import gi
gi.require_version('Gtk','3.0')
from gi.repository import Gtk as gtk
import os

software_list = [("Firefox", 2002,  "C++"),
             ("Eclipse", 2004, "Java" ),
             ("Pitivi", 2004, "Python"),
             ("Netbeans", 1996, "Java"),
             ("Chrome", 2008, "C++"),
             ("Filezilla", 2001, "C++"),
             ("Bazaar", 2005, "Python"),
             ("Git", 2005, "C"),
             ("Linux Kernel", 1991, "C"),
             ("GCC", 1987, "C"),
             ("Frostwire", 2004, "Java")]

class WB_Window(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self, title="Write Blocker")
        self.set_border_width(10)
        self.set_position(gtk.WindowPosition.CENTER)
        self.set_default_size(1000, 450)

        #self.outter_box = gtk.Box(gtk.Orientation.HORIZONTAL, spacing=10)
        self.outter_box = gtk.VBox(False,spacing=10)
        self.add(self.outter_box)

        self.software_liststore = gtk.ListStore(str, int, str)
        for software_ref in software_list:
            self.software_liststore.append(list(software_ref))
        tree = gtk.TreeView(self.software_liststore)

        for i, column_title in enumerate(["Software", "Release Year", "Programming Language"]):
            renderer = gtk.CellRendererText()
            column = gtk.TreeViewColumn(column_title, renderer, text=i)
            tree.append_column(column)

        self.scrollable_treelist = gtk.ScrolledWindow()
        self.scrollable_treelist.set_vexpand(True)
        self.scrollable_treelist.set_hexpand(True)
        self.outter_box.pack_start(self.scrollable_treelist, False, True, 0)
        self.scrollable_treelist.add(tree)

        hbox = gtk.ButtonBox.new(gtk.Orientation.HORIZONTAL)
        hbox.set_layout(gtk.ButtonBoxStyle.CENTER) 
        self.outter_box.pack_start(hbox, False, True, 0)

        # Add CSS "linked" class
        hbox.get_style_context().add_class("linked")


        button_mount = gtk.Button(label="Mount")
        hbox.add(button_mount)
        button_ro = gtk.Button(label="Read-Only")
        hbox.add(button_ro)
        button_rw = gtk.Button(label="Read-Write")
        hbox.add(button_rw)
        button_quit = gtk.Button(label="Quit",stock=gtk.STOCK_QUIT)
        button_quit.show()
        hbox.add(button_quit)

win = WB_Window()
win.connect("delete-event",gtk.main_quit)
win.show_all()
gtk.main()

最终的布局将是:

enter image description here

我必须使用 Gtk.VBox,因为 Gtk.Box 方向没有改变 (Fedora23)。我还添加了 CSS linked 类来使按钮“连接”。

关于Python Gtk 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45007702/

相关文章:

Python - 匹配字符组之间字符的所有重复

python - 使用 Python 抓取数据点

python - 在 Linux 上从 Python 连接到 protected WiFi

python - django-mptt 多树和查询集

python-2.7 - 在 Windows 8 上安装 Python 2.7

python-3.x - Tkinter - 将选项卡序列限制为聚焦框架

python - 值 0 时触发用户错误

Python 将 (iPhone) 表情符号写入文件

python - 如何从 Superbible Opengl 读取、解析 SBM 文件格式

python - 如何在 Ubuntu 上使用 Python 查找 Apache2 的配置错误?