こちらの記事を参考に、簡易なダイアログを適用しました。
質問
標題のとおり、現れるダイアログの 右上の×ボタンを無効化したいのですが どういった記述を行えばよいでしょうか?
尚、現行私のアプリケーションには tkアプリケーションのクラス変数を root として生成しており
root.resizable(0,0)
root.protocol('WM_DELETE_WINDOW', (lambda: 'pass')())
の記述があります。そのためroot上で いくつかのフレームを切り替える私オリジナルのフォームは右上の×ボタンは無効化されています。
現在 適用したダイアログの×ボタンが有効であるため、これを故意に操作してしまうと以下エラーを招いてしまいます。
初心者ゆえ、世間に紹介されている有用な技術の適用に苦戦しています、どなたかご見解を頂けますと幸いです。
python
1import tkinter as tk 2import tkinter.ttk as ttk 3import math 4import os 5import tkinter.messagebox as tkmb 6from functools import partial 7import datetime 8from tkinter import simpledialog 9 10#★ 11class MyDialog(simpledialog.Dialog): 12 13 def body(self, master): 14 self.var = tk.IntVar() 15 val_cmd = self.master.register(self.switchButtonState) 16 self.a = tk.Entry(master, validate="key", validatecommand=(val_cmd, '%P')) 17 self.a.pack() 18 19 self.a.focus_set() 20 21 def switchButtonState(self, P): 22 if len(P) == 0: 23 self.button1['state'] = tk.DISABLED 24 else: 25 self.button1['state'] = tk.NORMAL 26 return True 27 28 def buttonbox(self): 29 box = tk.Frame(self) 30 self.button1 = tk.Button(box, text="OK", width=10, command=self.ok, state=tk.DISABLED) 31 self.button1.pack(side=tk.LEFT, padx=5, pady=5) 32 33 box.pack() 34 35 def apply(self): 36 self.title = self.a.get() 37 38 def answer(self): 39 return self.title 40 41#★ 42def resource_path(relative_path): 43 try: 44 base_path = sys._MEIPASS 45 except Exception: 46 base_path = os.path.abspath(".") 47 48 return os.path.join(base_path, relative_path) 49 50def adjust_windowsize(root): 51 ww = root.winfo_screenwidth() 52 wh = root.winfo_screenheight() 53 54 lw = math.ceil(ww * 0.3208) 55 lh = math.ceil(wh * 0.477) 56 57 root.geometry(str(lw)+"x"+str(lh)+"+"+str(int(ww/2-lw/2))+"+"+str(int(wh/2-lh/2))) 58 59def info_message(msg): 60 tkmb.showinfo("お知らせ", msg) 61 62def abort_message(place, msg): 63 return tkmb.showerror(place, msg) 64 65 66#★ 67def generate_frame(root): 68 69 def change_frame(frame): 70 71 frame.tkraise() 72 73 style = ttk.Style() 74 style.theme_use('winnative') 75 style.map("TCombobox",selectbackground=[('!readonly','!focus','SystemWindow'),('readonly','!focus','SystemButtonFace'),],) 76 style.configure("TButton", font=("Arial", 16)) 77 style.configure("TFrame", background="#FFFFCC") 78 style.configure("TRadiobutton", background="#FFFFCC") 79 style.configure("TLabel", font=("Arial", 16), anchor='', background="#FFFFCC") 80 style.configure("TLabelframe", background="#FFFFCC", relief="sunken") 81 style.configure("TLabelframe.Label", foreground="red", font=("Arial", 16), background="#FFFFCC") 82 83 84 def get_Answer(): 85 Q = MyDialog(frmConvMenu) 86 87 try: 88 89 #con = db() 90 #connection = con.db_Connect() 91 #cursor = connection.cursor() 92 93 #★★★ teamikleさん回答をうけ、ダイアログ未回答でも下記条件 TRUE動作になっていることを初めて把握...(ありがとうございます) 94 #★★★ 未回答をNoneで確認するのが正解かもしれない。 いずれにしても未回答が発生させないようにするには を学ぶ 95 if Q.answer() != "": 96 data = (Q.answer(), cmbox_Tbl1.get()) 97 sql = "UPDATE M_TABLES SET NAME=? WHERE RECNO=?" 98 #cursor.execute(sql, data) 99 100 info_message("A table was updated") 101 else: 102 raise Exception("coudn't get an answer") 103 104 except Exception as e: 105 abort_message("E510", e) 106 return 107 108 finally: 109 None 110 #connection.commit() 111 #connection.close() 112 113 114 #★ 115 frmMain = ttk.Frame(root, name="frmMain") 116 frmMain.grid(row=0, column=0, sticky=tk.E + tk.W + tk.N + tk.S) 117 118 btn_SettingMenu = ttk.Button(frmMain, text = "入出力定義") 119 btn_SettingMenu.pack(fill = tk.BOTH, expand=True) 120 btn_SettingMenu.bind('<Return>', lambda a: change_frame(frmIOMenu)) 121 122 btn_SettingMenu = ttk.Button(frmMain, text = "変換定義", command=lambda: change_frame(frmConvMenu)) 123 btn_SettingMenu.pack(fill = tk.BOTH, expand=True) 124 btn_SettingMenu.bind('<Return>', lambda a: change_frame(frmConvMenu)) 125 126 btn_RunMenu = ttk.Button(frmMain, text = "実行メニュー") 127 btn_RunMenu.pack(fill = tk.BOTH, expand=True) 128 btn_RunMenu.focus_set() 129 130 btn_Close = ttk.Button(frmMain, text = "終了", command=root.destroy) 131 btn_Close.pack(fill = tk.BOTH, expand=True) 132 133 #★ 134 frmConvMenu = ttk.Frame(root, name="frmConvMenu") 135 frmConvMenu.grid_rowconfigure([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], weight=1) 136 frmConvMenu.grid_columnconfigure([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], weight=1, minsize=40) 137 frmConvMenu.grid(row=0, column=0, sticky=tk.E + tk.W + tk.N + tk.S) 138 139 140 lbfrm1 = ttk.LabelFrame(frmConvMenu, text="対応表登録", labelanchor="n") 141 lbfrm1.grid(row=1, column=0, rowspan=5, columnspan=10, sticky=tk.W + tk.E + tk.N + tk.S, padx=(10,10)) 142 143 144 lst_tmp = [1,2,3] 145 cmbox_Tbl1 = ttk.Combobox(frmConvMenu, height=3, font=("Arial", 16), state="readonly", values=lst_tmp) 146 cmbox_Tbl1.grid(row=2, column=1, columnspan=8, sticky=tk.E + tk.W, pady=(10,0), padx=(10,10)) 147 148 frmTmp = ttk.Frame(frmConvMenu, name="frmTmp") 149 frmTmp.grid(row=3, column=1, columnspan=8, sticky=tk.E + tk.W + tk.N + tk.S, padx=(10,10), pady=(10,0)) 150 151 lab_Select = ttk.Label(frmTmp, text="種類", font=("Arial", 11)) 152 lab_Select.grid(row=0, column=1, sticky=tk.E + tk.W) 153 154 lab_Sheet = ttk.Label(frmTmp, text="シート", font=("Arial", 11)) 155 lab_Sheet.grid(row=0, column=3, sticky=tk.E + tk.W) 156 157 lab_FCell = ttk.Label(frmTmp, text="開始", font=("Arial", 11)) 158 lab_FCell.grid(row=0, column=4, sticky=tk.E + tk.W) 159 160 lab_TCell = ttk.Label(frmTmp, text="終了", font=("Arial", 11)) 161 lab_TCell.grid(row=0, column=5, sticky=tk.E + tk.W) 162 163 lab_Key = ttk.Label(frmTmp, text="キー", font=("Arial", 11)) 164 lab_Key.grid(row=0, column=6, sticky=tk.E + tk.W) 165 166 lab_Initial = ttk.Label(frmTmp, text="初期化", font=("Arial", 11)) 167 lab_Initial.grid(row=0, column=7, sticky=tk.E + tk.W) 168 169 global radioValue 170 radioValue = tk.IntVar() 171 rb1 = ttk.Radiobutton(frmTmp, text="Tab", variable=radioValue, value=0) 172 rb1.grid(row=1, column=0, sticky=tk.S) 173 174 rb2 = ttk.Radiobutton(frmTmp, text="Comma", variable=radioValue, value=1) 175 rb2.grid(row=1, column=1, sticky=tk.S) 176 177 rb3 = ttk.Radiobutton(frmTmp, text="Excel", variable=radioValue, value=2) 178 rb3.grid(row=1, column=2, sticky=tk.S) 179 radioValue.set(2) 180 181 btn_Select = ttk.Button(frmConvMenu, text = "選択", command=get_Answer) 182 btn_Select.grid(row=4, column=1, columnspan=1, sticky=tk.E + tk.W, padx=(10,0), pady=(0,10)) 183 184 btn_Upload = ttk.Button(frmConvMenu, text = "Up", state=tk.DISABLED, command=lambda : importCtrl(radioValue.get())) 185 btn_Upload.grid(row=4, column=2, columnspan=1, sticky=tk.E + tk.W, pady=(0,10)) 186 187 ent_UpPath = ttk.Entry(frmConvMenu, state=tk.DISABLED, font=("Arial", 11), width=100) 188 ent_UpPath.grid(row=4, column=3, columnspan=6, sticky=tk.E + tk.W, padx=(0,10), pady=(0,10)) 189 190 cmbox_Sheet = ttk.Combobox(frmTmp, width=7, height=3, font=("Arial", 16), state=tk.DISABLED) 191 cmbox_Sheet.grid(row=1, column=3, sticky=tk.E + tk.W + tk.S) 192 193 cmbox_FCell = ttk.Combobox(frmTmp, width=3, height=3, font=("Arial", 16), state=tk.DISABLED) 194 cmbox_FCell.grid(row=1, column=4, sticky=tk.E + tk.W + tk.S) 195 196 cmbox_TCell = ttk.Combobox(frmTmp, width=3, height=3, font=("Arial", 16), state=tk.DISABLED) 197 cmbox_TCell.grid(row=1, column=5, sticky=tk.E + tk.W + tk.S) 198 199 cmbox_Key = ttk.Combobox(frmTmp, width=3, height=3, font=("Arial", 16), state=tk.DISABLED) 200 cmbox_Key.grid(row=1, column=6, sticky=tk.E + tk.W + tk.S) 201 202 reqinitial = tk.BooleanVar() 203 reqinitial.set(False) 204 chk_Initial = tk.Checkbutton(frmTmp, variable=reqinitial, bg="#FFFFCC", state=tk.DISABLED) 205 chk_Initial.grid(row=1, column=7, sticky=tk.E + tk.W + tk.S) 206 207 btn_TblM = ttk.Button(frmConvMenu, text = "個別編集", state=tk.DISABLED) 208 btn_TblM.grid(row=5, column=1, columnspan=8, sticky=tk.E + tk.W, padx=(10,10)) 209 210 lbfrm2 = ttk.LabelFrame(frmConvMenu, text="変換定義", labelanchor="n") 211 lbfrm2.grid(row=6, column=0, rowspan=5, columnspan=10, sticky=tk.W + tk.E + tk.N + tk.S, padx=(10,10), pady=(20,0)) 212 213 cmbox_Tbl2 = ttk.Combobox(frmConvMenu, height=3, font=("Arial", 16), state="readonly") 214 cmbox_Tbl2.grid(row=7, column=1, columnspan=8, sticky=tk.E + tk.W, pady=(30,0), padx=(10,10)) 215 216 btn_ConvM = ttk.Button(frmConvMenu, text = "変更定義の 追加 / 変更 / 削除", state=tk.DISABLED) 217 btn_ConvM.grid(row=8, column=1, columnspan=8, sticky=tk.E + tk.W, padx=(10,10)) 218 219 btn_Inq = ttk.Button(frmConvMenu, text = "変更定義の 一覧参照", state=tk.DISABLED) 220 btn_Inq.grid(row=9, column=1, columnspan=8, sticky=tk.E + tk.W, padx=(10,10)) 221 222 btn_ReturnMenu = ttk.Button(frmConvMenu, text = "閉じる", command=lambda: change_frame(frmMain)) 223 btn_ReturnMenu.grid(row=11, column=8, sticky=tk.E + tk.W, padx=(0,10)) 224 225 #★ 226 frmMain.tkraise() 227 228#★ 229if __name__ == "__main__": 230 root = tk.Tk() 231 adjust_windowsize(root) 232 root.title("TkInterの勉強") 233 root.grid_columnconfigure([0,1, 2, 3, 4, 5, 6, 7, 8, 9], weight=1) 234 root.grid_rowconfigure([0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], weight=1) 235 root.resizable(0,0) 236 root.protocol('WM_DELETE_WINDOW', (lambda: 'pass')()) 237 root["cursor"] = "hand2" 238 generate_frame(root) 239 root.mainloop()
回答1件
あなたの回答
tips
プレビュー