from tkinter import *
def printSomething():
inputValue=textBox.get("1.0","end-1c")
res=response(inputValue)
label = Label(root, text=res)
#this creates a new label to the GUI
label.pack()
root = Tk()
button = Button(root, text="Print Me", command=printSomething)
button.pack()
textBox=Text(root, height=2, width=10)
textBox.pack()
root.mainloop()
テキストを返すPythonコードを作成しました。それをtkinterlabelに出力します。実行しようとすると、labelに「None」と表示されます。
グローバル名前空間にラベルを一度作成してから、ボタンを押すたびにラベルを更新する方がよいでしょう。
また、import tkinter as tk
vsfrom tkinter import *
を使用することをお勧めします。これは、コードが大きくなり、組み込みメソッドを上書きしてしまうことがないため、保守性が向上するためです。
私はあなたのコードを更新し、PEP8標準により良く適合するようにいくつかのことを変更しました。
import tkinter as tk
def print_something():
label.config(text=text_box.get("1.0", "end-1c"))
root = tk.Tk()
tk.Button(root, text="Print Me", command=print_something).pack()
text_box = tk.Text(root, height=2, width=10)
text_box.pack()
label = tk.Label(root)
label.pack()
root.mainloop()
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加