簡體   English   中英

如何從 Jupyter 筆記本中的 Tkinter Text 小部件獲取文本?

[英]How to get text from Tkinter Text widget in Jupyter notebook?

我用一個簡單的GUI代碼tkinterJupyter

from tkinter import *

root = Tk()
text = Text(root, width = 40, height = 15)
text.pack()
root.mainloop()

如何獲取用戶輸入的文本?
如果我將text.get('1.0', 'end')到該代碼的末尾,則它不起作用,當我將text.get('1.0', 'end')到另一個單元格時,它也不起作用開始執行,直到我關閉root窗口,然后它會出現錯誤。
像這樣:

In[1]: from tkinter import *
       root = Tk()
       text = Text(root, width = 40, height = 15)
       text.pack()
       root.mainloop()
In[2]: text.get('1.0', 'end')

In[2]直到我關閉Tk()窗口才開始執行,在關閉窗口並開始運行In[2]它給出了這個錯誤:

TclError: invalid command name ".!text"

您無法與ipython交互中的tkinter mainloopipython進行交互。
如果您添加獲取文本按鈕,按下它將檢索文本小部件內容:

像這樣:

import tkinter as tk
root = tk.Tk()
text = tk.Text(root, width=40, height = 15)
text.pack()
tk.Button(root, text='get text', command=lambda: print(text.get('1.0', tk.END))).pack()
root.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM