質問するログイン新規登録

質問編集履歴

5

追加

2021/04/08 07:50

投稿

person
person

スコア224

title CHANGED
File without changes
body CHANGED
@@ -159,4 +159,6 @@
159
159
 
160
160
  if __name__ == "__main__":
161
161
  main()
162
- ```
162
+ ```
163
+
164
+ ![イメージ説明](3acaf48927c5ed6f218df62d3a28251b.jpeg)

4

追加

2021/04/08 07:49

投稿

person
person

スコア224

title CHANGED
File without changes
body CHANGED
@@ -78,4 +78,85 @@
78
78
 
79
79
  if __name__ == "__main__":
80
80
  main()
81
+ ```
82
+
83
+ # 追記
84
+ ```Python
85
+ from tkinter import font
86
+ from tkinter import ttk
87
+ import tkinter as tk
88
+
89
+ class App:
90
+ def __init__(self, win):
91
+ self.win = win
92
+ self.win.geometry("1000x700")
93
+ self.create_view()
94
+
95
+ def create_view(self):
96
+ self.win.rowconfigure(tuple(range(2)), weight=1)
97
+ self.win.columnconfigure(tuple(range(2)), weight=1)
98
+
99
+ columns = 5
100
+ data = []
101
+ for i in range(0, 100, 5):
102
+ data.append([str(i)+"abcdefghijklmnopqrstuvwxyzaaaaaaaaaaaa", str(i+1), str(i+2), str(i+3), str(i+4)])
103
+ self.tree = ttk.Treeview(self.win)
104
+ ttk.Style().configure("Treeview.Heading", font=("", 30))
105
+ fontsize = 30
106
+ treefont = font.Font(size=fontsize)
107
+ ttk.Style().configure("Treeview", font=("", fontsize), rowheight=treefont.metrics()["linespace"])
108
+ self.tree.grid(row=0, column=0, rowspan=2, columnspan=2, sticky="nsew", padx=5, pady=5)
109
+ self.tree["columns"] = tuple(range(1, columns + 1))
110
+ self.tree["show"] = "headings"
111
+ width_li = [0, 0, 0, 0, 0]
112
+ #w = self.win.winfo_width()
113
+ #ww = int(w / 7)
114
+ for i in range(1, columns + 1):
115
+ txt = "Data"+str(i)
116
+ self.tree.heading(i, text=txt)
117
+ if width_li[i-1] < treefont.measure(txt):
118
+ width_li[i-1] = treefont.measure(txt)
119
+ #id = None
120
+
121
+ for i in data:
122
+ for j in range(len(i)):
123
+ if width_li[j] < treefont.measure(i[j]):
124
+ width_li[j] = treefont.measure(i[j])
125
+ self.tree.insert("", "end", value=(i))
126
+
127
+ for i in range(1, columns + 1):
128
+ self.tree.column(i, stretch=False, width=width_li[i-1])
129
+
130
+ #self.tree.see(id)
131
+
132
+ self.button1 = tk.Button(self.win, text="▲", command=self.push_button1)
133
+ self.button1.grid(row=0, column=2, sticky="nsew")
134
+ self.button2 = tk.Button(self.win, text="▼", command=self.push_button2)
135
+ self.button2.grid(row=1, column=2, sticky="nsew")
136
+ self.button3 = tk.Button(self.win, text="◀", command=self.push_button3)
137
+ self.button3.grid(row=2, column=0, sticky="nsew")
138
+ self.button4 = tk.Button(self.win, text="▶", command=self.push_button4)
139
+ self.button4.grid(row=2, column=1, sticky="nsew")
140
+
141
+ def push_button1(self):
142
+ self.tree.yview("scroll", -1, "units")
143
+
144
+ def push_button2(self):
145
+ self.tree.yview("scroll", +1, "units")
146
+
147
+ def push_button3(self):
148
+ self.tree.xview("scroll", -1, "pages")
149
+
150
+ def push_button4(self):
151
+ self.tree.xview("scroll", +1, "pages")
152
+
153
+
154
+
155
+ def main():
156
+ win = tk.Tk()
157
+ App(win)
158
+ win.mainloop()
159
+
160
+ if __name__ == "__main__":
161
+ main()
81
162
  ```

3

変更

2021/04/08 07:48

投稿

person
person

スコア224

title CHANGED
File without changes
body CHANGED
@@ -23,7 +23,7 @@
23
23
  columns = 5
24
24
  data = []
25
25
  for i in range(0, 100, 5):
26
- data.append([str(i)+"abcdefghijklmnopqrstuvwxyz", str(i+1), str(i+2), str(i+3), str(i+4)])
26
+ data.append([str(i)+"abcdefghijklmnopqrstuvwxyzaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", str(i+1), str(i+2), str(i+3), str(i+4)])
27
27
  self.tree = ttk.Treeview(self.win)
28
28
  ttk.Style().configure("Treeview.Heading", font=("", 30))
29
29
  fontsize = 30
@@ -43,6 +43,7 @@
43
43
  for j in range(len(i)):
44
44
  if width_li[j] < len(i[j]):
45
45
  width_li[j] = len(i[j])
46
+ print(width_li)
46
47
  id = self.tree.insert("", "end", value=(i))
47
48
 
48
49
  self.tree.see(id)

2

変更

2021/04/08 05:27

投稿

person
person

スコア224

title CHANGED
File without changes
body CHANGED
@@ -3,7 +3,10 @@
3
3
 
4
4
  おそらくwidthはピクセル単位のため、最大文字列長を取得してwidthに指定しても意味ないと思いました。フォントサイズを変えたりしたときもwidthに設定すべき数値が変わると思います。
5
5
 
6
+ 以下、len()で取得した値をwidthに入れようとしたときのコードです。間違っていたらすみません。
7
+
6
8
  ```Python
9
+ from tkinter import font
7
10
  from tkinter import ttk
8
11
  import tkinter as tk
9
12
 
@@ -22,6 +25,10 @@
22
25
  for i in range(0, 100, 5):
23
26
  data.append([str(i)+"abcdefghijklmnopqrstuvwxyz", str(i+1), str(i+2), str(i+3), str(i+4)])
24
27
  self.tree = ttk.Treeview(self.win)
28
+ ttk.Style().configure("Treeview.Heading", font=("", 30))
29
+ fontsize = 30
30
+ treefont = font.Font(size=fontsize)
31
+ ttk.Style().configure("Treeview", font=("", fontsize), rowheight=treefont.metrics()["linespace"])
25
32
  self.tree.grid(row=0, column=0, rowspan=2, columnspan=2, sticky="nsew", padx=5, pady=5)
26
33
  self.tree["columns"] = tuple(range(1, columns + 1))
27
34
  self.tree["show"] = "headings"
@@ -30,11 +37,14 @@
30
37
  for i in range(1, columns + 1):
31
38
  self.tree.heading(i, text="Data"+str(i))
32
39
  #self.tree.column(i, width=ww)
33
- self.tree.column(i, width=5)
34
40
  id = None
41
+ width_li = [100, 100, 100, 100, 100] # width初期値
35
42
  for i in data:
43
+ for j in range(len(i)):
44
+ if width_li[j] < len(i[j]):
45
+ width_li[j] = len(i[j])
36
46
  id = self.tree.insert("", "end", value=(i))
37
-
47
+
38
48
  self.tree.see(id)
39
49
 
40
50
  self.button1 = tk.Button(self.win, text="▲", command=self.push_button1)
@@ -57,9 +67,9 @@
57
67
 
58
68
  def push_button4(self):
59
69
  self.tree.xview("scroll", +1, "pages")
60
-
61
70
 
62
71
 
72
+
63
73
  def main():
64
74
  win = tk.Tk()
65
75
  App(win)

1

追記

2021/04/08 05:25

投稿

person
person

スコア224

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,5 @@
1
1
  TkinterのTreeviewで表を作成したときに、初回表示時はデータのすべてが見えるようにするオプションはありますか?
2
- width指定ない場合、文字よっては実行時に列幅を広げないと全部見れません
2
+ (ウィンドウそのものの大きさ、Treeviewの大きさ全体的に広げるのは無。あとの方の列についてはスクロールして見えればいいですが、列幅は自分で操作したくない。
3
3
 
4
4
  おそらくwidthはピクセル単位のため、最大文字列長を取得してwidthに指定しても意味ないと思いました。フォントサイズを変えたりしたときもwidthに設定すべき数値が変わると思います。
5
5