実現したいこと
Tkinterで表形式の入力ウインドウを作りたいのですが、スクロールバーを入れると、タイトルバーと表の幅がズレてしまいます。
次のコードで「class Title_bar_a」で作るタイトルバーと、「class Tabulation_a」の表の幅を合わせる方法を教えてください。
該当のソースコード
Pythonimport
1from tkinter import ttk 2 3def main(): 4 width_size_lst = [3, 12, 20, 40, 25, 20, 12, 30] 5 i = 99 6 rt = tk.Tk() 7 rt.geometry("1800x1200") 8 9 window = Window(rt) 10 window.title_a(width_size_lst) # タイトルを生成 11 window.tabulation(width_size_lst, i) # 表を作る 12 13 rt.mainloop() 14 15 16class Window(tk.Frame): 17 def __init__(self, master=None): 18 super().__init__(master) 19 self.grid(sticky="nsew") 20 self.create_widgets() 21 22 # フレーム作り 23 def create_widgets(self): 24 padx_i = 0 25 pady_i = 1 26 ipadx_i = 0 27 ipady_i = 0 28 self.frm_aa = tk.Frame(self, width=20, height=900) 29 self.frm_aa.grid(row=0, column=0, rowspan=9, padx=padx_i, pady=pady_i, ipadx=ipadx_i, ipady=ipady_i) 30 31 self.frm_ad = tk.Frame(self, bg='green') 32 self.frm_ad.grid(row=3, column=1, padx=padx_i, pady=pady_i, ipadx=ipadx_i, ipady=ipady_i, sticky=tk.W) 33 34 self.frm_ae = tk.Frame(self) 35 self.frm_ae.grid(row=4, column=1, padx=padx_i, pady=pady_i, ipadx=ipadx_i, ipady=ipady_i, sticky=tk.W) 36 37 self.frm_af = tk.Frame(self) 38 self.frm_af.grid(row=5, column=1, padx=padx_i, pady=pady_i, ipadx=ipadx_i, ipady=ipady_i, sticky=tk.W) 39 40 # タイトルバー 41 def title_a(self, width_size_lst): 42 title_lst_a = ['No', 'Price_a', 'Account', 'App1', 'App2', 'Account', 'Price_b', 'Remarks'] 43 self.title_a = Title_bar_a(self.frm_af, title_lst_a, width_size_lst) 44 45 # 表作り 46 def tabulation(self, width_size_lst, i): 47 self.tabulation = Tabulation_a(self.frm_af, width_size_lst, i) 48 49 50# タイトルバ 51class Title_bar_a(tk.Frame): 52 def __init__(self, master, title_lst_a, width_size_lst): 53 super().__init__(master) 54 self.grid(sticky="nsew") 55 56 bar_style_h = ttk.Style() 57 bar_style_h.configure("defoh.TSeparator", background='black') 58 59 bar_style_v = ttk.Style() 60 bar_style_v.configure("defov.TSeparator", background='black') 61 62 sprt = ttk.Separator(self, orient="horizontal", style="defov.TSeparator") 63 sprt.grid(row=0, column=0, columnspan=17, sticky='nsew') 64 65 y_pos = 0 66 for i in range(len(title_lst_a)): 67 sprt = ttk.Separator(self, orient="vertical", style="defoh.TSeparator") 68 sprt.grid(row=1, column=y_pos, rowspan=3, sticky='nsew') 69 y_pos += 1 70 71 lbl = tk.Label(self, 72 text=title_lst_a[i], 73 width=width_size_lst[i], 74 height=2, 75 font=('BIZ UDGothic', 14)) 76 lbl.grid(row=1, column=y_pos) 77 y_pos += 1 78 79 sprt = ttk.Separator(self, orient="vertical", style="defoh.TSeparator") 80 sprt.grid(row=0, column=y_pos, rowspan=3, sticky='nsew') 81 82 sprt = ttk.Separator(self, orient="horizontal", style="defov.TSeparator") 83 sprt.grid(row=2, column=0, columnspan=17, sticky='nsew') 84 85 86# 表 87class Tabulation_a(tk.Frame): 88 def __init__(self, master, width_size_lst, i): 89 super().__init__(master) 90 self.grid(sticky="nsew") 91 92 cnv = tk.Canvas(self, bg='blue', width=1700, height=800, relief=tk.FLAT) 93 cnv.grid(sticky="nsew") 94 95 frm_xa = tk.Frame(cnv, bg='yellow', width=1700, height=800) 96 frm_xa.grid(sticky="nsew") 97 cnv.create_window(0, 0, window=frm_xa, anchor=tk.NW) 98 99 ybar = tk.Scrollbar(frm_xa, orient=tk.VERTICAL) 100 ybar.grid(column=19, rowspan=50, sticky='nsew') 101 ybar.config(command=cnv.yview) 102 cnv.config(yscrollcommand=ybar.set) 103 104 cnv.config(scrollregion=(0, 0, 0, i * 40)) 105 106 bar_style_h = ttk.Style() 107 bar_style_h.configure("defoh.TSeparator", background='black') 108 bar_style_v = ttk.Style() 109 bar_style_v.configure("defov.TSeparator", background='black') 110 111 x_pos = 0 112 y_pos = 0 113 padx_i = 0 114 pady_i = 0 115 ipadx_i = 0 116 ipady_i = 0 117 height_i = 2 118 119 for i_a in range(1, i + 1): 120 back_color = 'white' 121 122 sprt = ttk.Separator(frm_xa, orient="vertical", style="defov.TSeparator") 123 sprt.grid(row=x_pos, column=y_pos, rowspan=3, sticky='nsew') 124 y_pos += 1 125 126 # No 127 lbl_aa = tk.Label(frm_xa, 128 text=i_a, 129 width=width_size_lst[0], 130 height=height_i, 131 bg=back_color, 132 fg='black', 133 font=('BIZ UDGothic', 14), 134 relief=tk.FLAT) 135 lbl_aa.grid(row=x_pos, column=y_pos, padx=padx_i, pady=pady_i, ipadx=ipadx_i, ipady=ipady_i) 136 y_pos += 1 137 138 sprt = ttk.Separator(frm_xa, orient="vertical", style="defov.TSeparator") 139 sprt.grid(row=x_pos, column=y_pos, rowspan=3, sticky='nsew') 140 y_pos += 1 141 142 # Price_a 143 txt_aa = tk.Entry(frm_xa, 144 width=width_size_lst[1], 145 bg=back_color, 146 fg='black', 147 font=('BIZ UDGothic', 14), 148 relief=tk.FLAT) 149 txt_aa.grid(row=x_pos, column=y_pos, padx=padx_i, pady=pady_i, ipadx=ipadx_i, ipady=10) 150 y_pos += 1 151 152 sprt = ttk.Separator(frm_xa, orient="vertical", style="defov.TSeparator") 153 sprt.grid(row=x_pos, column=y_pos, rowspan=3, sticky='nsew') 154 y_pos += 1 155 156 # Account 157 lbl_ab = tk.Label(frm_xa, 158 text='', 159 width=width_size_lst[2], 160 height=height_i, 161 bg=back_color, 162 fg='black', 163 font=('BIZ UDGothic', 14), 164 relief=tk.FLAT) 165 lbl_ab.grid(row=x_pos, column=y_pos, padx=padx_i, pady=pady_i, ipadx=ipadx_i, ipady=ipady_i) 166 y_pos += 1 167 168 sprt = ttk.Separator(frm_xa, orient="vertical", style="defov.TSeparator") 169 sprt.grid(row=x_pos, column=y_pos, rowspan=3, sticky='nsew') 170 y_pos += 1 171 172 # App1 173 txt_ab = tk.Entry(frm_xa, 174 width=width_size_lst[3], 175 bg=back_color, 176 fg='black', 177 font=('BIZ UDGothic', 14), 178 relief=tk.FLAT) 179 txt_ab.grid(row=x_pos, column=y_pos, padx=padx_i, pady=pady_i, ipadx=ipadx_i, ipady=10) 180 y_pos += 1 181 182 sprt = ttk.Separator(frm_xa, orient="vertical", style="defov.TSeparator") 183 sprt.grid(row=x_pos, column=y_pos, rowspan=3, sticky='nsew') 184 y_pos += 1 185 186 # App2 187 txt_ac = tk.Entry(frm_xa, 188 width=width_size_lst[4], 189 bg=back_color, 190 fg='black', 191 font=('BIZ UDGothic', 14), 192 relief=tk.FLAT) 193 txt_ac.grid(row=x_pos, column=y_pos, padx=padx_i, pady=pady_i, ipadx=ipadx_i, ipady=10) 194 y_pos += 1 195 196 sprt = ttk.Separator(frm_xa, orient="vertical", style="defov.TSeparator") 197 sprt.grid(row=x_pos, column=y_pos, rowspan=3, sticky='nsew') 198 y_pos += 1 199 200 # Account 201 lbl_ac = tk.Label(frm_xa, 202 text='', 203 width=width_size_lst[5], 204 height=height_i, 205 bg=back_color, 206 fg='black', 207 font=('BIZ UDGothic', 14), 208 relief=tk.FLAT) 209 lbl_ac.grid(row=x_pos, column=y_pos, padx=padx_i, pady=pady_i, ipadx=ipadx_i, ipady=ipady_i) 210 y_pos += 1 211 212 sprt = ttk.Separator(frm_xa, orient="vertical", style="defov.TSeparator") 213 sprt.grid(row=x_pos, column=y_pos, rowspan=3, sticky='nsew') 214 y_pos += 1 215 216 # Price_b 217 txt_ad = tk.Entry(frm_xa, 218 width=width_size_lst[6], 219 bg=back_color, 220 fg='black', 221 font=('BIZ UDGothic', 14), 222 relief=tk.FLAT) 223 txt_ad.grid(row=x_pos, column=y_pos, padx=padx_i, pady=pady_i, ipadx=ipadx_i, ipady=10) 224 y_pos += 1 225 226 sprt = ttk.Separator(frm_xa, orient="vertical", style="defov.TSeparator") 227 sprt.grid(row=x_pos, column=y_pos, rowspan=3, sticky='nsew') 228 y_pos += 1 229 230 # Remarks 231 txt_ae = tk.Entry(frm_xa, 232 width=width_size_lst[7], 233 bg=back_color, 234 fg='black', 235 font=('BIZ UDGothic', 14), 236 relief=tk.FLAT) 237 txt_ae.grid(row=x_pos, column=y_pos, padx=padx_i, pady=pady_i, ipadx=ipadx_i, ipady=10) 238 y_pos += 1 239 240 sprt = ttk.Separator(frm_xa, orient="vertical", style="defov.TSeparator") 241 sprt.grid(row=x_pos, column=y_pos, rowspan=3, sticky='nsew') 242 y_pos += 1 243 244 # 横バー 245 x_pos += 1 246 sprt = ttk.Separator(frm_xa, orient="horizontal", style="defoh.TSeparator") 247 sprt.grid(row=x_pos + 1, column=0, columnspan=16, sticky='nsew') 248 249 y_pos = 0 250 x_pos += 2 251 252main()
補足情報(FW/ツールのバージョンなど)
Python 3.10
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。