所以我有2個檔案。
app.py 是包含所有與 tk 相關的 tkinter 檔案。
app_functions.py 只是函式。
因此,當我運行 app.py 并單擊 tk 按鈕時,該命令會執行 app_functions.py 檔案中的一個函式,但是在該函式中,它需要將 .insert() 文本插入應用程式中的 tk Text() 小部件.py 檔案。但我收到錯誤。
繼承人的錯誤:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Phil-\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "c:\Users\Phil-\python_main\gsc script building app\app.py", line 30, in <lambda>
button1 = Button(content_frame1, text="INIT_Function", command=lambda: app_functions.display_raw_gsc_code("INIT_FUNCTION_START", "INIT_FUNCTION_END"))
File "c:\Users\Phil-\python_main\gsc script building app\app_functions.py", line 45, in display_raw_gsc_code
content_frame2_text_area.insert(tk.END, line)
NameError: name 'content_frame2_text_area' is not defined
當我在 app_functions.py 檔案中匯入 app.py 檔案然后運行 ??app.py 檔案時,它會加載 gui,然后一旦我單擊按鈕,它就會再次打開 tk gui,這樣就不好了。
所以簡而言之,當我成功匯入該函式時,我能夠從 tk 按鈕執行另一個檔案中的函式。但是在該函式中,它需要將 .insert() 文本插入另一個檔案中的 tk 小部件,但這對我來說不起作用,所有在線示例都包括將該函式與 tk 按鈕和 tk Text() 小部件放在同一個檔案中并確保它有效,但我想將 tk 的東西和功能保存在單獨的檔案中。
//我試圖完成的基本概念:
- 單擊 app.py 中的按鈕,該按鈕執行 app_functions.py 中名為“display_raw_gsc_code”的函式
- app_functions.py 中的“display_raw_gsc_code”函式完成其作業,然后將文本插入到 app.py 中的 Text() 小部件
- app.py 中的 Text() 小部件顯示接收到的文本
//TK(app.py) 檔案中的按鈕
button1 = Button(content_frame1, text="INIT_Function", command=lambda: app_functions.display_raw_gsc_code("INIT_FUNCTION_START", "INIT_FUNCTION_END"))
//FUNCTIONS(app_functions.py) 檔案中的函式
def display_raw_gsc_code(start, end):
""" grab gsc 'example code' from raw file & display in output(frame2) area """
f = open(join(dirname(realpath(__file__)), "raw_gsc_code.txt"), 'rt')
with f as file:
copy = False
for line in file:
if line.strip() == start:
copy = True
continue
elif line.strip() == end:
break
elif copy:
content_frame2_text_area.insert(tk.END, line)
f.close()
//TK(app.py) 檔案中的文本小部件
content_frame2_text_area = Text(content_frame2, relief="ridge", bd=2) #GROOVE
content_frame2_text_area.grid(column=2, row=1, sticky="ns", padx=5, pady=5)
uj5u.com熱心網友回復:
您需要將 content_frame2_text_area 作為 display_raw_gsc_code() 的引數傳遞。– acw1668 3 月 16 日 0:49
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/446477.html
