2017-12-19 3 views
0

다양한 위젯이있는 GUI를 생성하는 자동 생성 코드가 있습니다. 위젯 중 하나는 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를 호출 할 때마다 업데이트하는 함수를 작성하고 싶습니다. 필자는 파이썬에 비교적 익숙하지 않으며 이것을 어떻게 할 수 있는지 알고 싶습니다.

먼저 속성으로 Scrolledlistbox4 개체를 만들 필요가

답변

2

: "Outside Button"가 변경 예를 아래에서

a = New_Toplevel_1() 

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

:

self.scrolled_listbox = Scrolledlistbox4(...) 

는 다음과 같이 바깥 쪽 범위에있는 모든를 구성 할 수 있습니다 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() 
관련 문제