2017-12-19 11 views
0

私は、様々なウィジェットを持つGUIを生成する自動生成コードを持っています。ウィジェットの1つはScrolledListBoxです。コードの一部を以下に示します。Python、クラスの外からウィジェットアイテムにアクセスする

class New_Toplevel_1: 
    def __init__(self, top=None): 
     self.Scrolledlistbox4.configure(background="white") 
     self.Scrolledlistbox4.configure(font="TkFixedFont") 
     self.Scrolledlistbox4.configure(highlightcolor="#d9d9d9") 
     self.Scrolledlistbox4.configure(selectbackground="#c4c4c4") 
     self.Scrolledlistbox4.configure(width=10) 

このクラスの外からScrolledlistbox4にアクセスしたいと思います。たとえば、私はScrolledListBoxを呼び出すたびにScrolledListBoxを更新する関数を書くよう書いています。私はPythonには比較的新しいので、これをどのように達成できるのか知りたいです。

答えて

2

あなたが最初の属性としてScrolledlistbox4オブジェクトを作成する必要があります。

self.scrolled_listbox = Scrolledlistbox4(...) 

その後、あなたのような最も外側のスコープ内のすべての設定しを行うことができます。以下の例では

a = New_Toplevel_1() 

a.scrolled_listbox.configure(background='white') 
... 

"Outside Button"が変わりますtext外部からのクラスのボタンのオプション:

import tkinter as tk 

class FrameWithButton(tk.Frame): 
    def __init__(self, master): 
     super().__init__(master) 

     self.btn = tk.Button(root, text="Button") 
     self.btn.pack() 

root = tk.Tk() 

an_instance = FrameWithButton(root) 
an_instance.pack() 

def update_button(): 
    global an_instance 
    an_instance.btn['text'] = "Button Text Updated!" 


tk.Button(root, text="Outside Button", command=update_button).pack() 

root.mainloop() 
関連する問題