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

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

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

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

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

pandas

Pandasは、PythonでRにおけるデータフレームに似た型を持たせることができるライブラリです。 行列計算の負担が大幅に軽減されるため、Rで行っていた集計作業をPythonでも比較的簡単に行えます。 データ構造を変更したりデータ分析したりするときにも便利です。

Q&A

解決済

1回答

1112閲覧

python pandasデータをダブルクリックで別画面に表示したい

Kazu

総合スコア5

Tkinter

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

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

pandas

Pandasは、PythonでRにおけるデータフレームに似た型を持たせることができるライブラリです。 行列計算の負担が大幅に軽減されるため、Rで行っていた集計作業をPythonでも比較的簡単に行えます。 データ構造を変更したりデータ分析したりするときにも便利です。

0グッド

0クリップ

投稿2022/03/07 01:43

編集2022/03/07 06:37

前提・実現したいこと

pandasを利用しcsvデータ表示を行っており
そのデータをダブルクリックすることで別画面(tkinter)で表示したい

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

def set_correct_data(self)でデータ格納をしているのですが class CorrectWindow(tkm.Frame)でデータ参照が出来ていない状況です。

該当のソースコード

python

1import tkinter as tkm 2import pandas as pd 3from tkinter import ttk 4from tkinter import * 5from tkinter.ttk import * 6from tkcalendar import DateEntry 7 8#-----------サブウィンドウ画面(検索)START---------- 9class SubWindow(tkm.Frame): 10 def __init__(self,master): 11 super().__init__(master) 12 self.pack() 13 self.CorrectWindow = [] 14 master.geometry("1500x400+100+300") 15 master.title("検索画面") 16 master.grab_set() 17 self.set_data() 18 self.sub_widget() 19 self.subwindow = [] 20 self.CorrectWindow = [] 21 22 #CSV情報読込 23 def set_data(self): 24 self.data = pd.read_csv("c://test.csv", dtype=object, encoding="utf_8_sig", na_filter=False) 25 self.colname_list = ["ID", "日付","登録者社員番号","支店","種別","プロジェクト番号","枝番","顧客名","金額","最終更新者社員番号", "最終更新日時", "登録実行日時"] # 検索結果に表示させる列名 26 self.width_list = [10, 15, 30, 10, 40, 40, 10, 200, 50, 40, 100, 100] 27 self.search_col = "検索キーワード" # 検索キーワードの入力されている列名 28 29 #各種パーツ読込 30 def sub_widget(self): 31 self.sw_main = ttk.PanedWindow(self.master, orient="vertical") 32 self.sw_main.pack(expand=True, fill=tkm.BOTH, side="left") 33 self.sw_top = ttk.PanedWindow(self.sw_main, orient="horizontal", height=25) 34 self.sw_main.add(self.sw_top) 35 self.sw_bottom = ttk.PanedWindow(self.sw_main, orient="vertical") 36 self.sw_main.add(self.sw_bottom) 37 self.creat_input_frame(self.sw_top) 38 self.create_tree(self.sw_bottom) 39 40 #キーワード入力部分作成 41 def creat_input_frame(self, parent): 42 fm_input = ttk.Frame(parent) 43 parent.add(fm_input) 44 lbl_keyword = ttk.Label(fm_input, text="キーワード", width=7) 45 lbl_keyword.grid(row=1, column=1, padx=2, pady=2) 46 self.keyword = tkm.StringVar() 47 ent_keyword = ttk.Entry(fm_input, justify="left", textvariable=self.keyword, width= 50) 48 ent_keyword.grid(row=1, column=2, padx=2, pady=2) 49 ent_keyword.bind("<Return>", self.search) 50 51 #CSVファイル表示 52 def create_tree(self, parent): 53 self.result_text = tkm.StringVar() 54 lbl_result = ttk.Label(parent, textvariable=self.result_text) 55 parent.add(lbl_result) 56 self.tree = ttk.Treeview(parent) 57 self.tree["column"] = self.colname_list 58 self.tree["show"] = "headings" 59 self.tree.bind("<Double-1>", self.onDuble) 60 61 for i, (colname, width) in enumerate(zip(self.colname_list, self.width_list)): 62 self.tree.heading(i, text=colname) 63 self.tree.column(i, width=width) 64 #セル位置調整 65 self.tree.column(0, anchor=E) 66 self.tree.column(1, anchor=CENTER) 67 self.tree.column(2, anchor=CENTER) 68 self.tree.column(3, anchor=CENTER) 69 self.tree.column(4, anchor=CENTER) 70 self.tree.column(5, anchor=E) 71 self.tree.column(6, anchor=E) 72 self.tree.column(7, anchor=W) 73 self.tree.column(8, anchor=E) 74 self.tree.column(9, anchor=CENTER) 75 parent.add(self.tree) 76 77 #検索実施 78 def search(self, event=None): 79 keyword = self.keyword.get() 80 result = self.data[self.data[self.search_col].str.contains(keyword, na=False)] 81 self.update_tree_by_search_result(result) 82 83 #検索結果更新 84 def update_tree_by_search_result(self, result): 85 self.tree.delete(*self.tree.get_children()) 86 self.result_text.set(f"検索結果:{len(result)}") 87 88 for _, row in result.iterrows(): 89 values = row[self.colname_list].to_list() 90 values[0] = f"{int(values[0]):06}" 91 values[6] = f"{int(values[6]):03}" 92 self.tree.insert("", "end", values=values) 93 94 #修正画面表示 95 def onDuble(self, event): 96 self.subwindow.append(tkm.Toplevel(highlightthickness=5, highlightbackground="white", highlightcolor="red")) 97 self.set_correct_data() 98 self.CorrectWindow.append(CorrectWindow(self.subwindow[len(self.subwindow)-1],len(self.subwindow))) 99 100 #-----------修正データ読込-START---------- 101 def set_correct_data(self): 102 selected_item = self.tree.selection()[0] #選択した行取得 103 values_0 = selected_item # 値の取得(行取得) 104 values_00 = self.tree.item(selected_item)['values'][0] # 値の取得(ID取得) 105 values_01 = self.tree.item(selected_item)['values'][2] # 値の取得(登録社員番号取得) 106 values_02 = self.tree.item(selected_item)['values'][3] # 値の取得(支店取得) 107 values_03 = self.tree.item(selected_item)['values'][4] # 値の取得(種別取得) 108 values_05 = self.tree.item(selected_item)['values'][5] # 値の取得(プロジェクト番号取得) 109 values_06 = self.tree.item(selected_item)['values'][6] # 値の取得(枝番取得) 110 values_07 = self.tree.item(selected_item)['values'][7] # 値の取得(顧客名取得) 111 values_08 = self.tree.item(selected_item)['values'][8] # 値の取得(金額取得) 112 113 114class CorrectWindow(tkm.Frame): 115 def __init__(self,master,num): 116 super().__init__(master) 117 self.pack() 118 master.geometry("550x250+500+350") 119 master.title("修正画面") 120 master.grab_set() 121 self.correct_widget() 122 123 def correct_widget(self): 124 self.correct_lbl00 = ttk.Label(self.master, text="ID", width=17, anchor="e", justify=RIGHT) 125 self.correct_lbl01 = ttk.Label(self.master, text="登録者社員番号", width=17, anchor="e", justify=RIGHT) 126 self.correct_lbl02 = ttk.Label(self.master, text="支店", width=17, anchor="e", justify=RIGHT) 127 self.correct_lbl03 = ttk.Label(self.master, text="種別", width=13, anchor="e", justify=RIGHT) 128 self.correct_lbl04 = ttk.Label(self.master, text="日付", width=17, anchor="e", justify=RIGHT) 129 self.correct_lbl04_1 = ttk.Label(self.master, text="※電子データに記載がある日付を入力", width=30, anchor="e", justify=RIGHT) 130 131 self.correct_lbl05 = ttk.Label(self.master, text="プロジェクト番号", width=17, anchor= "e", justify=RIGHT) 132 self.correct_lbl06 = ttk.Label(self.master, text="枝番", width=17, anchor="e", justify=RIGHT) 133 self.correct_lbl07 = ttk.Label(self.master, text="顧客名", width=17, anchor="e", justify=RIGHT) 134 self.correct_lbl08 = ttk.Label(self.master, text="金額(税込)", width=17, anchor="e", justify=RIGHT) 135 self.correct_lbl08_1 = ttk.Label(self.master, text="※金額が発生しない場合でも 0 を入力", width=32, anchor="e", justify=RIGHT) 136 137 self.correct_lbl00.place(x=10, y=10) 138 self.correct_lbl01.place(x=10, y=35) 139 self.correct_lbl02.place(x=10, y=60) 140 self.correct_lbl03.place(x=190, y=60) 141 self.correct_lbl04.place(x=10, y=85) 142 self.correct_lbl04_1.place(x=235, y=85) 143 self.correct_lbl05.place(x=10, y=110) 144 self.correct_lbl06.place(x=166, y=110) 145 self.correct_lbl07.place(x=10, y=135) 146 self.correct_lbl08.place(x=10, y=160) 147 self.correct_lbl08_1.place(x=220, y=160) 148 149 self.correct_entry00 = ttk.Entry(self.master, justify=tkm.LEFT, width=13) 150 self.correct_entry01 = ttk.Entry(self.master, justify=tkm.LEFT, width=13) 151 self.correct_combo02 = ttk.Combobox(self.master, justify=tkm.CENTER, width=13) 152 self.correct_combo03 = ttk.Combobox(self.master, justify=tkm.CENTER, width=16) 153 self.correct_date04 = DateEntry(self.master, date_pattern='yyyy/mm/dd', width=13) 154 self.correct_entry05 = ttk.Entry(self.master, justify = tkm.LEFT, width = 13) 155 self.correct_entry06 = ttk.Entry(self.master, justify=tkm.LEFT, width=5) 156 self.correct_entry07 = ttk.Entry(self.master, justify=tkm.LEFT, width=60) 157 self.correct_entry08 = ttk.Entry(self.master, justify=tkm.RIGHT, width=13) 158 159 self.correct_entry00.place(x=130, y=10) 160 self.correct_entry01.place(x=130, y=35) 161 self.correct_combo02.place(x=130, y=60) 162 self.correct_combo03.place(x=280, y=60) 163 self.correct_date04.place(x=130, y=85) 164 self.correct_entry05.place(x=130, y=110) 165 self.correct_entry06.place(x=280, y=110) 166 self.correct_entry07.place(x=130, y=135) 167 self.correct_entry08.place(x=130, y=160) 168 169 self.correct_entry00.insert = SubWindow.set_correct_data.values_00 170 171def main(): 172 win = tkm.Tk() 173 app = SubWindow(win) 174 app.mainloop() 175 176if __name__ == '__main__': 177 main() 178

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

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

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

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

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

Kazu

2022/03/07 06:39

self.correct_entry00.insert = SubWindow.set_correct_data.values_00のような 記載で class SubWindowで設定した def set_correct_data(self): values_00 = self.tree.item(selected_item)['values'][0] # 値の取得(ID取得) のデータを class CorrectWindowの self.correct_entry00にinsertし表示を行いたいのですが 表示方法が分かりません。 よろしくお願い致します。
guest

回答1

0

ベストアンサー

self.correct_widget()master.grab_set() の順序を入れ替えて下さい。

python

1 class CorrectWindow(tkm.Frame): 2 def __init__(self,master,num): 3 super().__init__(master) 4 self.pack() 5 master.geometry("550x250+500+350") 6 master.title("修正画面") 7 self.correct_widget() 8 master.grab_set()

追記

StringVar() を使います。以下、変更部分だけを記載します。

python

1class SubWindow(tkm.Frame): 2 3 #修正画面表示 4 def onDuble(self, event): 5 self.subwindow.append(tkm.Toplevel(highlightthickness=5, highlightbackground="white", highlightcolor="red")) 6 self.CorrectWindow.append(CorrectWindow(self.subwindow[len(self.subwindow)-1],len(self.subwindow))) 7 self.set_correct_data() 8 9 #-----------修正データ読込-START---------- 10 def set_correct_data(self): 11 selected_item = self.tree.selection()[0] #選択した行取得 12 values_0 = selected_item # 値の取得(行取得) 13 for i, j in zip([*range(4), *range(5, 9)], [0, *range(2, 9)]): 14 self.CorrectWindow[0].entry_contents[i].set(self.tree.item(selected_item)['values'][j]) 15 16class CorrectWindow(tkm.Frame): 17 18 def correct_widget(self): 19 20 self.entry_contents = [StringVar() for _ in range(9)] 21 self.correct_entry00 = ttk.Entry(self.master, justify=tkm.LEFT, width=13, textvariable=self.entry_contents[0]) 22 self.correct_entry01 = ttk.Entry(self.master, justify=tkm.LEFT, width=13, textvariable=self.entry_contents[1]) 23 self.correct_combo02 = ttk.Combobox(self.master, justify=tkm.CENTER, width=13, textvariable=self.entry_contents[2]) 24 self.correct_combo03 = ttk.Combobox(self.master, justify=tkm.CENTER, width=16, textvariable=self.entry_contents[3]) 25 self.correct_date04 = DateEntry(self.master, date_pattern='yyyy/mm/dd', width=13, textvariable=self.entry_contents[4]) 26 self.correct_entry05 = ttk.Entry(self.master, justify = tkm.LEFT, width = 13, textvariable=self.entry_contents[5]) 27 self.correct_entry06 = ttk.Entry(self.master, justify=tkm.LEFT, width=5, textvariable=self.entry_contents[6]) 28 self.correct_entry07 = ttk.Entry(self.master, justify=tkm.LEFT, width=60, textvariable=self.entry_contents[7]) 29 self.correct_entry08 = ttk.Entry(self.master, justify=tkm.RIGHT, width=13, textvariable=self.entry_contents[8])

投稿2022/03/07 02:07

編集2022/03/07 16:57
melian

総合スコア19703

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

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

Kazu

2022/03/07 02:37

ご回答ありがとうございます。 質問の仕方が悪くて申し訳ございません。 現在、検索画面を作成しておりCSVデータ表示は可能をなっております。 その表示されたデータをダブルクリックし別のTkinter画面に選択したデータを insertし表示したいのですがうまくいっていない(出来ない)状況になっております。
melian

2022/03/07 02:48

すみません、こちらこそ質問内容を誤って捉えてしまっていました。回答を修正します。
Kazu

2022/03/07 10:18

修正ご回答ありがとうございます。 再度の質問の仕方が悪く、意図が伝わらなくて申し訳ございません。 上記コードは 検索画面表示【class SubWindow(tkm.Frame)】しCSVデータを表示しております。 ※こちらは表示可能となっております。 この表示されたデータより修正を行いたいデータ部分をダブルクリックし 修正用画面【class CorrectWindow(tkm.Frame)】にて それぞれの値を参照し表示したいのですが 【class SubWindow(tkm.Frame)】のデータを【class CorrectWindow(tkm.Frame)】で 上手く参照することが出来ません。 参照方法をご教授いただければ幸いです。
melian

2022/03/07 14:58

手元の環境では、 master.grab_set() self.correct_widget() の順序で実行するとエラーになってしまいます(修正画面を作成(pack)する前に grab を実行するため)。
Kazu

2022/03/08 00:04

melian様 拙い質問にも拘らず、何度もご教授いただきありがとうございました。 おかげさまで無事に動作させることができました。 本当にありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問