質問編集履歴

1

def on_scrollを書き直しました

2023/05/24 09:15

投稿

shinobuKouno
shinobuKouno

スコア31

test CHANGED
File without changes
test CHANGED
@@ -23,10 +23,13 @@
23
23
  def create_table():
24
24
  object_list = []
25
25
  day_dic = {}
26
-
26
+ scroll_pos = 0
27
+ list_index = 0
28
+ # 今日の日付を取得
27
29
  dt = datetime.datetime.today()
28
30
 
29
-
31
+ # 翌日
32
+ next_day = dt + datetime.timedelta(days=1)
30
33
 
31
34
  yy = dt.year
32
35
  mm = dt.month
@@ -37,7 +40,7 @@
37
40
  week_b = week_dic[week_a]
38
41
 
39
42
  test_list = []
40
- for i in range(1, 1100):
43
+ for i in range(1, 1200):
41
44
  mm = dt.month
42
45
  dd = dt.day
43
46
  week_a = dt.weekday()
@@ -50,28 +53,47 @@
50
53
  dt = dt + datetime.timedelta(days=1)
51
54
 
52
55
  def on_scroll(*args):
53
- # スクロール位置を取得
54
- scroll_pos = int(ybar.get()[1] * len(test_list))
55
56
 
57
+ # スクロールのポジションを整数化
56
- print(scroll_pos) #←動かしても0しか返ってこない
58
+ sc_pos = int(ybar.get()[0] * len(test_list))
57
59
 
58
- # ウィジェットの表示を更新
59
- for i in range(len(object_list)):
60
+ dif = ybar.get()[1] - ybar.get()[0]
60
- obj = object_list[i]
61
- text_idx = (i + scroll_pos) % len(test_list)
62
- text_values = test_list[text_idx][1:]
63
61
 
62
+ print(sc_pos) # ←スライドしても0しか表示されない
63
+
64
+
65
+ # 書き変え
66
+ # ウィジェットを書き換えるtest_listの最初のインデックス
67
+ nonlocal scroll_pos
68
+ nonlocal list_index
69
+
70
+ # 上に移動したのか下に移動したのか
71
+ # スクロールバーが下に移動していれば
72
+ if sc_pos > scroll_pos:
73
+ list_index += sc_pos
74
+ scroll_pos = sc_pos / len(test_list)
75
+
76
+ # スクロールバーが上に移動していれば
77
+ elif sc_pos < scroll_pos:
78
+ list_index -= sc_pos
79
+ scroll_pos = sc_pos / len(test_list)
80
+
81
+ # マウスボタンを離すと元に戻るため、スクロールバーを移動した所でとどめておく。
82
+ ybar.set(scroll_pos, scroll_pos + dif)
83
+
84
+ i = list_index
85
+
86
+ for v in object_list:
87
+ # 日付ラベルの色
88
+ back_color = 'green'
89
+ v[0].config(text=test_list[i][0])
90
+ v[0].config(bg=back_color)
91
+
64
- for j in range(len(obj)):
92
+ for r in range(1,len(v)):
65
- widget = obj[j]
66
- if isinstance(widget, tk.Label):
67
- widget.config(text=test_list[text_idx][0])
68
- elif isinstance(widget, tk.Text):
69
- if j - 1 < len(text_values):
70
- widget.delete('1.0', tk.END)
93
+ v[r].delete('1.0', 'end')
71
- widget.insert(tk.END, text_values[j - 1])
72
- else:
73
- widget.delete('1.0', tk.END)
94
+ v[r].insert('1.0',test_list[i][r])
74
- widget.insert(tk.END, '')
95
+ v[r].config(bg=back_color)
96
+ i+=1
75
97
 
76
98
  root = tk.Tk()
77
99
  root.title("Table")
@@ -80,26 +102,31 @@
80
102
  frm_a = tk.Frame(root)
81
103
  frm_a.grid(row=0, column=0)
82
104
 
83
- frm_d = tk.Frame(root)
105
+ frm_b = tk.Frame(root)
84
- frm_d.grid(row=1, column=0)
106
+ frm_b.grid(row=1, column=0)
85
107
 
108
+ cnv = tk.Canvas(frm_b)
109
+ cnv.grid(row=0,column=0)
86
110
 
111
+ ybar = tk.Scrollbar(frm_b, orient=tk.VERTICAL)
112
+ ybar.grid(row=0, column=2, rowspan=1, sticky='ns')
113
+ ybar.config(command=on_scroll)
114
+
115
+ cnv.configure(yscrollcommand=ybar.set)
116
+
87
- lbl = tk.Label(frm_d, text='2023年', font=('BIZ UDGothic', 12))
117
+ lbl = tk.Label(cnv, text='2023年', font=('BIZ UDGothic', 12))
88
- lbl.grid(row=0, column=0)
118
+ lbl.grid(row=1, column=0)
89
119
 
90
120
  for i in range(6):
91
- lbl = tk.Label(frm_d, width=30, text='氏名' + str(i + 1), font=('BIZ UDGothic', 12))
121
+ lbl = tk.Label(cnv, width=30, text='氏名' + str(i + 1), font=('BIZ UDGothic', 12))
92
122
  lbl.grid(row=0, column=i + 1)
93
123
 
94
- x_pos = 1
95
- y_pos = 0
96
-
97
124
  for i in range(22):
98
- lbl = tk.Label(frm_d, text=test_list[i][0], font=('BIZ UDGothic', 12))
125
+ lbl = tk.Label(cnv, text=test_list[i][0], font=('BIZ UDGothic', 12))
99
126
  lbl.grid(row=i + 1, column=0)
100
127
  tmp_list = [lbl]
101
128
  for r in range(1, 7):
102
- txt = tk.Text(frm_d, height=3, width=30, font=('BIZ UDGothic', 12))
129
+ txt = tk.Text(cnv, height=3, width=30, font=('BIZ UDGothic', 12))
103
130
  txt.grid(row=i + 1, column=r)
104
131
  txt.insert(tk.END, test_list[i][r])
105
132
  tmp_list.append(txt)
@@ -107,18 +134,15 @@
107
134
  object_list.append(tmp_list)
108
135
 
109
136
 
110
- ybar = tk.Scrollbar(frm_d, orient=tk.VERTICAL)
137
+ ybar = tk.Scrollbar(cnv, orient=tk.VERTICAL)
111
- ybar.grid(row=1, column=7, rowspan=24, sticky='ns')
138
+ ybar.grid(row=1, column=8, rowspan=22, sticky='ns')
112
139
  ybar.config(command=on_scroll)
113
-
114
140
 
115
141
  root.mainloop()
116
142
 
117
143
  create_table()
118
144
  ```
119
145
 
120
- ### 試したこと
121
- def on_scroll(*args)はchatgptで書いてもらいました。
122
146
 
123
147
 
124
148
  ### 補足情報(FW/ツールのバージョンなど)