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

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

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

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

Python

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

Q&A

解決済

1回答

1296閲覧

python 孫ウィンドウ作成時に親ウィンドウの表示が変わってしまう。

Kazu

総合スコア5

Tkinter

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

Python

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

1グッド

0クリップ

投稿2022/03/01 02:16

前提・実現したいこと

親ウィンドウ⇒子ウィンドウ⇒孫ウインドウと開き
孫ウィンドウにはカレンダーを表示したい。
よろしくお願い致します。

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

親ウィンドウ⇒子ウィンドウ⇒孫ウインドウと遷移しているのですが 孫ウインドウを表示すると親ウィンドウの表示が変わってしまい ※長さすぎるためコードを一部削除しております。

初期画面(親)
イメージ説明
検索画面(子)
イメージ説明
修正画面(孫)
イメージ説明
この時の親画面
イメージ説明

該当のソースコード

python

1import importlib 2import shutil 3import re, openpyxl 4import tkinter as tkm 5import pandas as pd 6import datetime 7from tkinter import ttk 8from tkinter import * 9from tkinter.ttk import * 10from tkinter import messagebox 11from tkinter import filedialog 12from tkcalendar import DateEntry 13 14class Application(tkm.Frame): 15 def __init__(self,master): 16 super().__init__(master) 17 self.pack() 18 self.master.geometry("550x280+500+350") 19 self.mainwidget() 20 self.mainwindow = [] 21 self.subwindow = [] 22 style = ttk.Style() 23 style.theme_use("default") 24 style.map("Treeview") 25 26 #---------ボタン動作-START---------- 27 #ファイル参照ボタン 28 def select_files(self): 29 30 #検索ボタン(検索画面表示) 31 def search_window(self): 32 self.mainwindow.append(tkm.Toplevel(highlightthickness=5, highlightbackground="white", highlightcolor="red")) 33 self.subwindow.append(SubWindow(self.mainwindow[len(self.mainwindow)-1],len(self.mainwindow))) 34 35 #---------ボタン動作-END---------- 36 37 #-----------パーツ設定-START---------- 38 def mainwidget(self): 39 self.lbl01 = ttk.Label(self.master, text="登録者社員番号", width=17, anchor="e", justify=RIGHT) 40 self.lbl02 = ttk.Label(self.master, text="支店", width=17, anchor="e", justify=RIGHT) 41 self.lbl03 = ttk.Label(self.master, text="種別", width=13, anchor="e", justify=RIGHT) 42 self.lbl04 = ttk.Label(self.master, text="日付", width=17, anchor="e", justify=RIGHT) 43 self.lbl04_1 = ttk.Label(self.master, text="※電子データに記載がある日付を入力", width=30, anchor="e", justify=RIGHT) 44 self.lbl05 = ttk.Label(self.master, text="プロジェクト番号", width=17, anchor= "e", justify=RIGHT) 45 self.lbl06 = ttk.Label(self.master, text="枝番", width=17, anchor="e", justify=RIGHT) 46 self.lbl07 = ttk.Label(self.master, text="顧客名", width=17, anchor="e", justify=RIGHT) 47 self.lbl08 = ttk.Label(self.master, text="金額(税込)", width=17, anchor="e", justify=RIGHT) 48 self.lbl08_1 = ttk.Label(self.master, text="※金額が発生しない場合でも 0 を入力", width=32, anchor="e", justify=RIGHT) 49 self.lbl09 = ttk.Label(self.master, text="対象ファイル", width=17, anchor="e", justify=RIGHT) 50 self.lbl10 = ttk.Label(self.master, text="ファイル保存先", width=17, anchor="e", justify=RIGHT) 51 self.lbl11 = ttk.Label(self.master, text="変更後のファイル名", width=17, anchor="e", justify=RIGHT) 52 53 self.lbl01.place(x=10, y=10) 54 self.lbl02.place(x=10, y=35) 55 self.lbl03.place(x=190, y=35) 56 self.lbl04.place(x=10, y=60) 57 self.lbl04_1.place(x=235, y=60) 58 self.lbl05.place(x=10, y=85) 59 self.lbl06.place(x=166, y=85) 60 self.lbl07.place(x=10, y=110) 61 self.lbl08.place(x=10, y=135) 62 self.lbl08_1.place(x=220, y=135) 63 self.lbl09.place(x=10, y=160) 64 self.lbl10.place(x=10, y=185) 65 self.lbl11.place(x=10, y=210) 66 67 self.entry01 = ttk.Entry(self.master, justify=tkm.LEFT, width=13) 68 self.combo02 = ttk.Combobox(self.master, justify=tkm.CENTER, width=13) 69 self.combo03 = ttk.Combobox(self.master, justify=tkm.CENTER, width=16) 70 self.date04 = DateEntry(date_pattern='yyyy/mm/dd', width=13) 71 self.entry05 = ttk.Entry(self.master, justify = tkm.LEFT, width = 13) 72 self.entry06 = ttk.Entry(self.master, justify=tkm.LEFT, width=5) 73 self.entry07 = ttk.Entry(self.master, justify=tkm.LEFT, width=60) 74 self.entry08 = ttk.Entry(self.master, justify=tkm.RIGHT, width=13) 75 self.entry09 = ttk.Entry(self.master, justify=tkm.LEFT, width=60) 76 self.btn12 = ttk.Button(self.master, text="参照", width=6, command=self.select_files) 77 self.entry10 = ttk.Entry(self.master, justify=tkm.LEFT, width=60, state="readonly") 78 self.entry11 = ttk.Entry(self.master, justify=tkm.LEFT, width=60, state="readonly") 79 self.btn13 = ttk.Button(self.master, text="検索", width=10, state="enable", command=self.search_window) 80 self.btn14 = ttk.Button(self.master, text="確認", width=10, state="enable", command=self.check) 81 self.btn15 = ttk.Button(self.master, text="実行", width=10, state="disable", command=self.renameexe) 82 self.btn16 = ttk.Button(self.master, text="クリア", width=10, command=self.initclear) 83 self.entry01.place(x=130, y=10) 84 self.combo02.place(x=130, y=35) 85 self.combo03.place(x=280, y=35) 86 self.date04.place(x=130, y=60) 87 self.entry05.place(x=130, y=85) 88 self.entry06.place(x=280, y=85) 89 self.entry07.place(x=130, y=110) 90 self.entry08.place(x=130, y=135) 91 self.entry09.place(x=130, y=160) 92 self.entry10.place(x=130, y=185) 93 self.entry11.place(x=130, y=210) 94 self.btn12.place(x=500, y=158) 95 self.btn13.place(x=190, y=240) 96 self.btn14.place(x=270, y=240) 97 self.btn15.place(x=350, y=240) 98 self.btn16.place(x=430, y=240) 99 100 #-----------パーツ設定-END---------- 101#-----------サブウィンドウ画面(検索)START---------- 102class SubWindow(tkm.Frame): 103 def __init__(self,master,num): 104 super().__init__(master) 105 self.pack() 106 self.CorrectWindow = [] 107 master.geometry("1500x400+100+300") 108 master.title("検索画面") 109 master.grab_set() 110 self.set_data() 111 self.sub_widget() 112 self.subwindow = [] 113 self.CorrectWindow = [] 114 115 #各種パーツ読込 116 def sub_widget(self): 117 self.sw_main = ttk.PanedWindow(self.master, orient="vertical") 118 self.sw_main.pack(expand=True, fill=tkm.BOTH, side="left") 119 self.sw_top = ttk.PanedWindow(self.sw_main, orient="horizontal", height=25) 120 self.sw_main.add(self.sw_top) 121 self.sw_bottom = ttk.PanedWindow(self.sw_main, orient="vertical") 122 self.sw_main.add(self.sw_bottom) 123 self.creat_input_frame(self.sw_top) 124 self.create_tree(self.sw_bottom) 125 126 #修正画面表示 127 def onDuble(self, event): 128 self.subwindow.append(tkm.Toplevel(highlightthickness=5, highlightbackground="white", highlightcolor="red")) 129 self.CorrectWindow.append(CorrectWindow(self.subwindow[len(self.subwindow)-1],len(self.subwindow))) 130 131#-----------修正ウィンドウ画面 START---------- 132class CorrectWindow(tkm.Frame): 133 def __init__(self,master,num): 134 super().__init__(master) 135 self.pack() 136 master.geometry("550x250+500+350") 137 master.title("修正画面") 138 master.grab_set() 139 self.correct_widget() 140 self.set_correct_data() 141 #-----------パーツ設定-START---------- 142 def correct_widget(self): 143 self.correct_lbl00 = ttk.Label(self.master, text="ID", width=17, anchor="e", justify=RIGHT) 144 self.correct_lbl01 = ttk.Label(self.master, text="登録者社員番号", width=17, anchor="e", justify=RIGHT) 145 self.correct_lbl02 = ttk.Label(self.master, text="支店", width=17, anchor="e", justify=RIGHT) 146 self.correct_lbl03 = ttk.Label(self.master, text="種別", width=13, anchor="e", justify=RIGHT) 147 self.correct_lbl04 = ttk.Label(self.master, text="日付", width=17, anchor="e", justify=RIGHT) 148 self.correct_lbl04_1 = ttk.Label(self.master, text="※電子データに記載がある日付を入力", width=30, anchor="e", justify=RIGHT) 149 self.correct_lbl05 = ttk.Label(self.master, text="プロジェクト番号", width=17, anchor= "e", justify=RIGHT) 150 self.correct_lbl06 = ttk.Label(self.master, text="枝番", width=17, anchor="e", justify=RIGHT) 151 self.correct_lbl07 = ttk.Label(self.master, text="顧客名", width=17, anchor="e", justify=RIGHT) 152 self.correct_lbl08 = ttk.Label(self.master, text="金額(税込)", width=17, anchor="e", justify=RIGHT) 153 self.correct_lbl08_1 = ttk.Label(self.master, text="※金額が発生しない場合でも 0 を入力", width=32, anchor="e", justify=RIGHT) 154 155 self.correct_lbl00.place(x=10, y=10) 156 self.correct_lbl01.place(x=10, y=35) 157 self.correct_lbl02.place(x=10, y=60) 158 self.correct_lbl03.place(x=190, y=60) 159 self.correct_lbl04.place(x=10, y=85) 160 self.correct_lbl04_1.place(x=235, y=85) 161 self.correct_lbl05.place(x=10, y=110) 162 self.correct_lbl06.place(x=166, y=110) 163 self.correct_lbl07.place(x=10, y=135) 164 self.correct_lbl08.place(x=10, y=160) 165 self.correct_lbl08_1.place(x=220, y=160) 166 167 self.correct_entry00 = ttk.Entry(self.master, justify=tkm.LEFT, width=13) 168 self.correct_entry01 = ttk.Entry(self.master, justify=tkm.LEFT, width=13) 169 self.correct_combo02 = ttk.Combobox(self.master, justify=tkm.CENTER, width=13) 170 self.correct_combo03 = ttk.Combobox(self.master, justify=tkm.CENTER, width=16) 171 self.correct_date04 = DateEntry(date_pattern='yyyy/mm/dd', width=13) 172 self.correct_entry05 = ttk.Entry(self.master, justify = tkm.LEFT, width = 13) 173 self.correct_entry06 = ttk.Entry(self.master, justify=tkm.LEFT, width=5) 174 self.correct_entry07 = ttk.Entry(self.master, justify=tkm.LEFT, width=60) 175 self.correct_entry08 = ttk.Entry(self.master, justify=tkm.RIGHT, width=13) 176 # self.correct_btn13 = ttk.Button(self.master, text="更新", width=10, state="enable", command=self.correct_update) 177 # self.correct_btn14 = ttk.Button(self.master, text="キャンセル", width=10, state="enable", command=self.cancel) 178 self.correct_entry00.place(x=130, y=10) 179 self.correct_entry01.place(x=130, y=35) 180 self.correct_combo02.place(x=130, y=60) 181 self.correct_combo03.place(x=280, y=60) 182 self.correct_date04.place(x=130, y=85) 183 self.correct_entry05.place(x=130, y=110) 184 self.correct_entry06.place(x=280, y=110) 185 self.correct_entry07.place(x=130, y=135) 186 self.correct_entry08.place(x=130, y=160) 187 # self.correct_btn13.place(x=350, y=190) 188 # self.correct_btn14.place(x=430, y=190) 189 190 #-----------パーツ設定-END---------- 191def main(): 192 win = tkm.Tk() 193 app = Application(win) 194 app.mainloop() 195if __name__ == '__main__': 196 main()
teamikl👍を押しています

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

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

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

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

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

y_waiwai

2022/03/01 03:34

なにがどう変わるんでしょう。 問題が見えません
Kazu

2022/03/01 03:57

分かりにくくて申し訳ございません。 初期画面(親)⇒検索画面(子)⇒修正画面(孫)と遷移させると 初期画面(親)のプロジェクト番号部分にカレンダーが表示されてしまいます。 また、孫の画面には日付部分にカレンダーが表示できない状態となっております。
guest

回答1

0

ベストアンサー

コードは実行可能なものを掲載お願いします。

コードを見た限りでの問題点は、
ウィジェットの親子関係が正しく設定されてません。

  • Toplevel 生成時に親ウィンドウが指定されていない。

 省略した場合、ルート・ウィンドウが親となるので、「孫ウィンドウ」とはなりません。

  • ウィジェットは Frame (self)上に配置すべきところ、Toplevel (self.master)上になっている。

親ウィンドウの表示が変わるというところから、
親ウィンドウを親ウィジェットにして、各部品を生成されてるのではないでしょうか(推測)

CorrectWindow 内で print(self) や print(self.master) を表示し、
意図通りの親子関係になっているかを確認してみてください。

tk のウィジェットは、ドット区切りの階層で表示されます。

python

1. 親ウィンドウ 2.!toplevel 子ウィンドウ 3.!toplevel.!toplevel 孫ウィンドウ

投稿2022/03/01 04:18

teamikl

総合スコア8664

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

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

Kazu

2022/03/01 04:41

ご回答ありがとうございます。 実際のコードを載せたかったのですが文字制限があり全てのコードを載せる事が出来ませんでした。 申し訳ございません。(10000文字以内制限) CorrectWindow 内で print(self) や print(self.master) を表示を実施してみたところ print(self) ⇒ .!toplevel2.!correctwindow print(self.master)  ⇒ .!toplevel2 となりました。 これはやはり正常な状態ではないでしょうか? また、ご指摘いただきました ・ウィジェットは Frame (self)上に配置すべきところ、Toplevel (self.master)上になっている。 ですが具体的には下記の部分の変更になるのでしょうか? まだまだ、不慣れなことが多々ありまして申し訳ございませんがご教授いただければ幸いです。 class CorrectWindow(tkm.Frame): def __init__(self,master,num): super().__init__(master) よろしくお願い致します。
teamikl

2022/03/01 07:54 編集

コードは、「実行可能」「問題の再現が可能かどうか」という点が重要なので、 長すぎて掲載できない場合は、質問用に新たに作成してみてください。 省略された部分が原因だった場合や、実行できないと問題の特定が困難な場合があります。 - 不要な外部ライブラリ (pandas, openpxel) を省く。  カレンダーも今回は要点でなければ、テスト用にラベルを表示等の代替手段を取る。  追記: tkcalendar.DateEntry でのみ問題が起きる場合は必要になるので、その場合は別途調査。 - ウィンドウに配置するのは、問題の再現に必要な部分のみにする。  レイアウトが絡む場合は重要になることもありませんが、必要最低限でよい部分です。 - コードの実行に必要な部分は省かない。search, check メソッド等が未定義でエラーになります  省く場合は、呼び出す側のボタンも省きます。 > 具体的には下記の部分の変更になるのでしょうか? Label(self.master, ...) としてる部分です。クラスは Frame を継承してるのですが、 その上に配置される各ウィジェットは Frame 上に配置されてません。 ※ place での配置なので、見た目的にはここは特に問題有りません。  親ウインドウの表示が変わるのは別の問題です。  (ウィジェットの親子関係が、想定されてるような「孫」になってないという指摘です)
teamikl

2022/03/01 07:18

一点気になって調べてみたのが、 tkcalendar.DateEntry のコンストラクタ内で スタイル(ttk.Style) が変更されている点。 https://github.com/j4321/tkcalendar/blob/0c6aa7cb4ee1e583bdf0faec62f99f760f192895/tkcalendar/dateentry.py#L184 どのように表示が変わるのか確認は取れないのですが、 DateEntry のみ表示が変わっているということはありませんか? 対策: 事前に style を固定する。DateEntry._setup_style を無効化する。 (サブクラス化してメソッドを上書き等)
Kazu

2022/03/03 10:12

teamikl様 色々なご質問にお答えいただきありがとうございます。 ご教授いただいた内容も見させていただいておりますが 今の自分では中々、理解するには難しいようです。 やはり初心者には質問することも難しいですね。 色々なサイトから調べてくっつけたコードになっているので 正直どの部分を省きどの部分をご質問すればよいかすら分からなくなってきております。 申し訳ございません。
teamikl

2022/03/03 11:34

コードを実行できないので、問題の再現がこちらで出来ないのが難しい所ですね。 推測ですが、DateEntry の親ウィジェット指定が無い為、親ウィンドウに表示されてるのだと思います。 (先のコメントではDateEntryのスタイル指定の影響と思いましたが、 親ウィンドウでもDateEntryを使っていた為撤回します) ウィジェット生成時に ttk.Combobox(self.master, justify=tkm.CENTER, width=16) のように、第一引数に親ウィジェット (このコードの場合 self.master) を指定するのですが、 省略時は大元のルート・ウィンドウ(この場合は親ウィンドウ)になります。 修正箇所として、具体的には correct window 内のDateEntry 生成の部分 DateEntry(self.master, date_pattern='yyyy/mm/dd', width=13) を試してみてください。 親ウィンドウのクラス内でも省略されてますが、元々親ウィンドウ内に配置の為問題にはなってません。 そちらも、同様に親を明示した方が良いです。
Kazu

2022/03/03 22:53

teamikl様 本当に親切丁寧にご教授いただきありがとうございます。 DateEntry(self.master, date_pattern='yyyy/mm/dd', width=13) を適用したところ親画面が意図しない動作からこちらが実施したい動作へとなりました。 本当にありがとうございました。 このプログラムに最後の登録内容の修正プログラムを加えて完成と考えております。 また、不明点が発生した際は新しく質問投稿いたします。 そのような際に目に留まるようなことがありましたらまたご教授いただければ幸いです。 本当にありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問