Python Gtk 按钮

Posted

技术标签:

【中文标题】Python Gtk 按钮【英文标题】:Python Gtk buttons 【发布时间】:2017-07-10 08:56:30 【问题描述】:

我正在使用 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()

【问题讨论】:

你玩过Glade吗?这是找出容器做什么的简单方法。 【参考方案1】:

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

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()

生成的布局将是:

我不得不使用 Gtk.VBox,因为 Gtk.Box 的方向没有改变(Fedora23)。我还添加了 CSS linkedclass 以使按钮“连接”。

【讨论】:

以上是关于Python Gtk 按钮的主要内容,如果未能解决你的问题,请参考以下文章

GTK3 在按钮标签中换行

GTK构件之微调按钮

GTK# 图像按钮在运行时不显示图像

仅在选中时读取 GTK 单选按钮信号

如何在 GTK3 中禁用按钮

修改 GTK 按钮样式/CSS 并立即更新/刷新