2017-06-27 83 views
1

我有一個設置了一些窗口小部件的窗口,但我希望這些窗口小部件的信息在用戶輸入(例如按鈕點擊)後被更新。另外,我希望事件處理程序能夠將新小部件添加到窗口中。 我附上了一個嘗試以下簡單版本的問題。顯然它不起作用。如何更新窗口中的窗口小部件並將新的窗口小部件添加到PyGObject中的窗口中?

import gi 
gi.require_version("Gtk", "3.0") 
from gi.repository import Gdk, Gtk 

class Button(Gtk.Box): 

    def __init__(self, message, label, window_grid): 
     Gtk.Box.__init__(self, spacing=6) 
     self.set_border_width(10) 
     self.label = label 
     self.window_grid = window_grid 

     button = Gtk.Button.new_with_label(message) 
     button.connect("clicked", self.on_click) 
     self.pack_start(button, True, True, 0) 

    def on_click(self, widget): 
     # Change/update a label in the window_grid 
     self.label = LabelBox("Changed the label") 
     self.label.queue_draw() 
     # Add a new label to the window_grid 
     new_label = LabelBox("New label") 
     self.window_grid.attach(new_label, 0, 2, 1, 1) 

class LabelBox(Gtk.Box): 

    def __init__(self, message): 
     Gtk.Box.__init__(self, spacing=6) 
     self.set_border_width(10) 
     label = Gtk.Label(message) 
     self.pack_start(label, True, True, 0) 

win = Gtk.Window() 
window_grid = Gtk.Grid() 

label = LabelBox("This is a label") 
button = Button("Test", label, window_grid) 

window_grid.attach(label, 0, 0, 1, 1) 
window_grid.attach(button, 0, 1, 2, 2) 

win.add(window_grid) 

win.connect("delete-event", Gtk.main_quit) 
win.show_all() 
Gtk.main() 
+1

控件默認爲隱藏。 – andlabs

回答

0

您的代碼結構格式不正確。此代碼對我有用:

import gi 
gi.require_version("Gtk", "3.0") 
from gi.repository import Gtk 
import sys 

class GUI: 
    def __init__(self): 

     self.win = Gtk.Window() 
     self.window_grid = Gtk.Grid() 
     box = Gtk.Box() 

     button = Gtk.Button.new_with_label("Test") 
     button.connect("clicked", self.on_click) 

     self.label = Gtk.Label("This is a label") 

     self.window_grid.attach(self.label, 0, 0, 1, 1) 
     self.window_grid.attach(button, 0, 1, 2, 2) 

     self.win.add(self.window_grid) 

     self.win.connect("delete-event", Gtk.main_quit) 
     self.win.show_all() 

    def on_click(self, widget): 
     # Change/update a label in the window_grid 
     self.label.set_label('Label changed') 
     label = Gtk.Label("Another label") 
     self.window_grid.attach(label, 2, 1, 2, 2) 
     self.win.show_all() 


def main(): 
    app = GUI() 
    Gtk.main() 

if __name__ == "__main__": 
    sys.exit(main()) 

此外,請記住@andlabs註釋,小部件默認情況下處於隱藏狀態。

+0

這是我們如何在窗口中添加新窗口小部件的方法,但我們如何修改現有窗口小部件(請參閱原始問題)? –

+0

@Billjoe對不起,我誤解了你的要求。試試我編輯的代碼。 – theGtknerd

+0

在這種情況下,它可以用標籤工作,但我希望能有更普遍的情況。假設我有一個包含一些變量的框,然後我改變其中的一個變量。現在我想更新該框。我怎麼做? –

0

如前所述,您的代碼邏輯不好。您必須嘗試瞭解如何設計您的應用程序。無論如何,我已經設法讓您的代碼適合您的問題:

import gi 
gi.require_version("Gtk", "3.0") 
from gi.repository import Gtk 

class Button(Gtk.Box): 

    def __init__(self, message, label, window_grid): 
     Gtk.Box.__init__(self, spacing=6) 
     self.set_border_width(10) 
     self.label = label 
     self.window_grid = window_grid 

     button = Gtk.Button.new_with_label(message) 
     button.connect("clicked", self.on_click) 
     self.pack_start(button, True, True, 0) 

    def on_click(self, widget): 
     # Change/update a label in the window_grid 
     self.label.label.set_text("Changed the lable") 
     # Add a new label to the window_grid 
     new_label = LabelBox("New label") 
     self.window_grid.attach(new_label, 0, 2, 1, 1) 
     new_label.show_all() 

class LabelBox(Gtk.Box): 

    def __init__(self, message): 
     Gtk.Box.__init__(self, spacing=6) 
     self.set_border_width(10) 
     self.label = Gtk.Label(message) 
     self.pack_start(self.label, True, True, 0) 

win = Gtk.Window() 
window_grid = Gtk.Grid() 

label = LabelBox("This is a label") 
button = Button("Test", label, window_grid) 

window_grid.attach(label, 0, 0, 1, 1) 
window_grid.attach(button, 0, 1, 1, 1) 

win.add(window_grid) 
win.connect("destroy", Gtk.main_quit) 
win.show_all() 

Gtk.main() 

將它與您的相比,以查看您所犯的錯誤。沒有必要將標籤和按鈕包裹在GtkBox中。

相關問題