質問編集履歴

3

追記

2022/05/06 18:59

投稿

netz-eng
netz-eng

スコア105

test CHANGED
File without changes
test CHANGED
@@ -26,10 +26,13 @@
26
26
  app.mainloop()
27
27
  ```
28
28
  ### 試したこと
29
+ ~~フォントを`游明朝 Demibold`とし、~~
29
- フォントを`游明朝 Demibold`とし、`font.measure`メソッドで文字列長さを算出しました。
30
+ `font.measure`メソッドで文字列長さを算出しました。
30
31
 
31
- 折り返し位置に指定した`wraplength`の値は175ピクセルですが、
32
+ ~~折り返し位置に指定した`wraplength`の値は175ピクセルですが、
32
- `font.measure`メソッドで算出した1行分の値は252~274(単位不明)でした。
33
+ `font.measure`メソッドで算出した1行分の値は252~274(単位不明)でした。~~
34
+
35
+ (1)デフォルトのフォントで文字列長さを比較したところ、`wraplength`<1行の文字列長となることを確認しました。
33
36
 
34
37
  ```Python
35
38
  from tkinter import font as tkFont
@@ -38,22 +41,36 @@
38
41
  tex = "文字列文字列文字列文字列文字"
39
42
  tex2 = "StringStringStringStringStringStri"
40
43
 
41
- f = tkFont.Font(family="游明朝 Demibold")
44
+ # f = tkFont.Font(family="游明朝 Demibold")
45
+ f = tkFont.nametofont(lbl["font"])   <- ラベルのデフォルトフォントを取得
42
46
  # font.measureメソッドで文字列長さを算出
43
47
  w1 = f.measure(tex)
44
48
  w2 = f.measure(tex2)
45
49
 
46
- # 日本語、英語の1行分の長さ(単位:
50
+ # 日本語、英語の1行分の長さ(単位:ピクセル
47
51
  print(w1)
48
- >252
52
+ >168
49
53
  print(w2)
50
- >274
54
+ >172
51
55
 
52
56
  # wraplengthで指定した文字列折り返しの長さ(単位:ピクセル)
53
57
  print(w*140/20)
54
58
  >175
55
59
 
56
60
  ```
61
+ (2)**「游明朝 Demibold」フォントで長さを比較しようとしたところ、エラーが発生**してしまいました。
62
+ ```Python
63
+ # 游明朝 Demiboldに合わせた1行分の折り返しピクセル数
64
+ wraplength = w * 8
65
+
66
+ lbl = tk.Label(app, text=tex2, width=w, font=("游明朝 Demibold", 10, ""),
67
+ wraplength=wraplength, anchor=tk.W, justify=tk.LEFT)
68
+ font = tkFont.nametofont("游明朝 Demibold")
69
+
70
+ >> _tkinter.TclError: named font 游明朝 Demibold does not already exist
71
+
72
+ ```
73
+
57
74
 
58
75
  ### 補足情報(FW/ツールのバージョンなど)
59
76
  Windows11

2

訂正

2022/05/06 13:46

投稿

netz-eng
netz-eng

スコア105

test CHANGED
File without changes
test CHANGED
@@ -35,15 +35,15 @@
35
35
  from tkinter import font as tkFont
36
36
 
37
37
  # 日本語、英語でそれぞれTextウィジェット幅×1行分の長さ
38
- tex = "文字列文字列文字列文字列文字列文字列文字列文字列文字列文"
38
+ tex = "文字列文字列文字列文字列文字"
39
- tex2 = "StringStringStringStringStringStringStringStringStringStringStringS"
39
+ tex2 = "StringStringStringStringStringStri"
40
40
 
41
41
  f = tkFont.Font(family="游明朝 Demibold")
42
42
  # font.measureメソッドで文字列長さを算出
43
43
  w1 = f.measure(tex)
44
44
  w2 = f.measure(tex2)
45
45
 
46
- # 日本語、英語の2行分の長さ(単位:?)
46
+ # 日本語、英語の1行分の長さ(単位:?)
47
47
  print(w1)
48
48
  >252
49
49
  print(w2)

1

追記

2022/05/06 12:37

投稿

netz-eng
netz-eng

スコア105

test CHANGED
File without changes
test CHANGED
@@ -25,7 +25,35 @@
25
25
 
26
26
  app.mainloop()
27
27
  ```
28
+ ### 試したこと
29
+ フォントを`游明朝 Demibold`とし、`font.measure`メソッドで文字列長さを算出しました。
28
30
 
31
+ 折り返し位置に指定した`wraplength`の値は175ピクセルですが、
32
+ `font.measure`メソッドで算出した1行分の値は252~274(単位不明)でした。
33
+
34
+ ```Python
35
+ from tkinter import font as tkFont
36
+
37
+ # 日本語、英語でそれぞれTextウィジェット幅×1行分の長さ
38
+ tex = "文字列文字列文字列文字列文字列文字列文字列文字列文字列文"
39
+ tex2 = "StringStringStringStringStringStringStringStringStringStringStringS"
40
+
41
+ f = tkFont.Font(family="游明朝 Demibold")
42
+ # font.measureメソッドで文字列長さを算出
43
+ w1 = f.measure(tex)
44
+ w2 = f.measure(tex2)
45
+
46
+ # 日本語、英語の2行分の長さ(単位:?)
47
+ print(w1)
48
+ >252
49
+ print(w2)
50
+ >274
51
+
52
+ # wraplengthで指定した文字列折り返しの長さ(単位:ピクセル)
53
+ print(w*140/20)
54
+ >175
55
+
56
+ ```
29
57
 
30
58
  ### 補足情報(FW/ツールのバージョンなど)
31
59
  Windows11