質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

Q&A

解決済

1回答

5282閲覧

Tkinterにグラフとテキストを表示させたい

RYOMAKUBO1

総合スコア18

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

0グッド

0クリップ

投稿2018/12/12 08:36

編集2018/12/12 16:35

前提・実現したいこと

pythonのTkinterを使ってUIのシステムを作っているのですが、キーワードの横にあるテキストボックスtxtに文字を入力し、ボタンを押すと、グラフが表示され、下にあるテキストボックスco_wordにメッセージを表示させたいです。

発生している問題・エラーメッセージ

UIの画面は表示されるのですが、文字を入力してボタンを押しても何も変化が現れません。

該当のソースコード

python

1import os 2import sys 3import string 4from collections import OrderedDict 5import io 6import tkinter as tk 7import numpy as np 8import matplotlib 9import matplotlib.pyplot as plt 10from matplotlib.figure import Figure 11from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg 12 13def word_search(): 14 root = tk.Tk() 15 root.title("search") 16 root.geometry("600x600") 17 18 19 title = tk.Entry(width=20) 20 title.place(x=100, y=70) 21 22 23 lbl = tk.Label(text='キーワード') 24 lbl.place(x=30, y=140) 25 26 txt = tk.Entry(width=20) 27 txt.place(x=100, y=140) 28 29 30 co_word=tk.Text(width=30) 31 co_word.place(x=30, y=250) 32 33 34 button = tk.Button(root, text="search", command="pushed") 35 button.place(x=100, y=200) 36  root.mainloop() 37 38 def pushed(): 39 left=np.array([1,2,3,4]) 40 height=np.array([50,66,77,88]) 41 plt.plot(left,height) 42 plt.xlabel('week') 43 plt.ylabel('count') 44 plt.show() 45 46 twext=txt.get() 47 if text==[]: 48 raise Exception('Error!') 49 message-"グラフが表示されました" 50 co_word.insert(tk.END,message) 51 52 53word_search()

試したこと

一度UIを閉じて再度表示させればうまくいくとネットで見たので(URLは覚えておりません)
最後の行に
root.destroy()
root.mainloop()
を入れ、実行しても何も変化しませんでした。

補足情報(FW/ツールのバージョンなど)

動作環境はubuntu18.04です

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

とりあえずグラフが表示できるとよいのであれば、

(1) tk.Button() の command パラメータには 文字列ではなくて関数を渡す

誤:button = tk.Button(root, text="search", command="pushed")
正:button = tk.Button(root, text="search", command=pushed)

(2) 上記の command パラメータを設定する に command関数を定義しておく

の2点を修正すると動作すると思います。

一応修正済みコードは以下のようになります

テキストの処理部は仕様不明の為、一切修正しておりません

Python

1import tkinter as tk 2import numpy as np 3import matplotlib.pyplot as plt 4 5def word_search(): 6 root = tk.Tk() 7 root.title("search") 8 root.geometry("600x600") 9 10 11 title = tk.Entry(width=20) 12 title.place(x=100, y=70) 13 14 15 lbl = tk.Label(text='キーワード') 16 lbl.place(x=30, y=140) 17 18 txt = tk.Entry(width=20) 19 txt.place(x=100, y=140) 20 21 22 co_word=tk.Text(width=30) 23 co_word.place(x=30, y=250) 24 25 def pushed(): 26 left=np.array([1,2,3,4]) 27 height=np.array([50,66,77,88]) 28 plt.plot(left,height) 29 plt.xlabel('week') 30 plt.ylabel('count') 31 plt.show() 32 33 twext=txt.get() 34 if text==[]: 35 raise Exception('Error!') 36 message-"グラフが表示されました" 37 co_word.insert(tk.END,message) 38 39 button = tk.Button(root, text="search", command=pushed) 40 button.place(x=100, y=200) 41 root.mainloop() 42 43 44 45word_search()

投稿2018/12/12 23:22

magichan

総合スコア15898

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

RYOMAKUBO1

2018/12/16 03:38

回答ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問