我想知道是否可以在方法內部聲明全局變量,以便可以在另一個UI方法內調用它並使用它。我在下面寫的代碼應該採用用戶選擇的閾值文件,然後在點擊運行按鈕時打印出來。從方法調用全局變量(Python)
class Handler:
def on_filechooserbutton1_file_set(self, widget):
self.Thresholds = widget.get_filename()
with self.Thresholds as a:
global Thresholds
Thresholds = pd.read_table(a, sep=',')
def on_RUN_clicked(self, widget):
print Thresholds
def onDeleteWindow(self, *args):
Gtk.main_quit(*args)
builder = Gtk.Builder()
builder.add_from_file("DataAPP.glade")
builder.connect_signals(Handler())
window = builder.get_object("MainWindow")
window.show_all()
Gtk.main()
print Thresholds
的閾值變量包含一個數據幀,並已被確定,但每當我嘗試調用它的方法的閾值變量之外,我得到一個錯誤,指出:
NameError: global name 'Thresholds' is not defined
任何suggenestions如何去正確地做這件事?
全局'Thresholds'不會存在,直到'on_filechooserbutton1_file_set'運行至少一次。 –
如果您認爲它需要全球化,請重新考慮您的設計。從你描述問題的方式來看,這聽起來像你可以簡單地做self.Thresholds –
我已經這樣做了,但我將過濾該文件,並將爲我正在構建的用戶界面的其他部分使用該信息。 – mdicrist