2017-06-27 2 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()) 

また、ウィジェットはデフォルトで非表示になっています。

+0

これは、新しいウィジェットをウィンドウに追加する方法ですが、既存のウィジェットを変更するにはどうしたらいいですか? –

+0

@Billjoe申し訳ありませんが、私はあなたの要求を誤解しました。私の編集したコードを試してみてください。 – theGtknerd

+0

このケースではラベル付きで動作しますが、私はより一般的なケースを望んでいました。私はいくつかの変数を持つボックスを持っていると仮定し、それらの変数の1つを変更します。今度はボックスを更新したいと思います。それ、どうやったら出来るの? –

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内にラベルとボタンをラップする必要はありませんでした。

関連する問題