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

質問編集履歴

2

コードが見やすいように修正

2021/06/26 11:40

投稿

makoi
makoi

スコア4

title CHANGED
File without changes
body CHANGED
@@ -45,17 +45,17 @@
45
45
  if (not run_text.process()):
46
46
  run_text.print_open()
47
47
 
48
- コード
48
+
49
49
  ```
50
50
 
51
51
  参考にしたコードからの変更点 コードの11行目
52
-
52
+ ```
53
53
  self.parser.add_argument("-t", "--text", help="The text to scroll on the RGB LED panel", default = "Hello World")
54
-
54
+ ```
55
55
  から、文字列を記入していたdefault = "Hello World")の部分を変更すればよいと考え,openを使いテキストファイルを取り出そうと思いコードを
56
-
56
+ ```
57
57
  self.parser.add_argument("-t", "--text", help="The text to scroll on the RGB LED panel", default = "open('data.txt','r')")
58
-
58
+ ```
59
59
  に変更しました。
60
60
  しかし、LEDマトリクスパネルに表示されるのはテキストファイルの結果ではなくopen('data.txt','r')という文字列でした。
61
61
 

1

コードの書き方。これまでの修正点など

2021/06/26 11:40

投稿

makoi
makoi

スコア4

title CHANGED
File without changes
body CHANGED
@@ -7,19 +7,18 @@
7
7
 
8
8
  外部ファイル(テキストファイル)から文字列のデータを入手する機能を実装するときにエラーメッセージが発生しました。
9
9
 
10
- <ソースコード>
10
+ ```#!/usr/bin/env python
11
11
  #!/usr/bin/env python
12
12
  # Display a runtext with double-buffering.
13
- # -*- coding: utf-8 -*-
13
+ # coding: utf-8
14
14
  from samplebase import SampleBase
15
15
  from rgbmatrix import graphics
16
16
  import time
17
17
 
18
-
19
- class RunText(SampleBase):
18
+ class RunText (SampleBase):
20
19
  def __init__(self, *args, **kwargs):
21
20
  super(RunText, self).__init__(*args, **kwargs)
22
- self.parser.add_argument("-t", "--text", help="The text to scroll on the $
21
+ self.parser.add_argument("-t", "--text", help="The text to scroll on the RGB LED panel", default = "open('data.txt','r')")
23
22
 
24
23
  def run(self):
25
24
  offscreen_canvas = self.matrix.CreateFrameCanvas()
@@ -31,7 +30,7 @@
31
30
 
32
31
  while True:
33
32
  offscreen_canvas.Clear()
34
- len = graphics.DrawText(offscreen_canvas, font, pos, 20, textColor, m$
33
+ len = graphics.DrawText(offscreen_canvas, font, pos, 20, textColor, my_text)
35
34
  pos -= 1
36
35
  if (pos + len < 0):
37
36
  pos = offscreen_canvas.width
@@ -44,10 +43,22 @@
44
43
  if __name__ == "__main__":
45
44
  run_text = RunText()
46
45
  if (not run_text.process()):
47
- run_text.print_help()
46
+ run_text.print_open()
48
47
 
48
+ コード
49
+ ```
50
+
51
+ 参考にしたコードからの変更点 コードの11行目
49
52
 
53
+ self.parser.add_argument("-t", "--text", help="The text to scroll on the RGB LED panel", default = "Hello World")
54
+
50
- <エラーコード line 12>
55
+ から、文字列を記入していたdefault = "Hello World")の部分を変更すればよいと考え,openを使いテキストファイルを取り出そうと思いコード
56
+
51
- self.parser.add_argument("-t", "--text", help="The text to scroll on the RGB LED panel", default = open"('data.txt','r')")
57
+ self.parser.add_argument("-t", "--text", help="The text to scroll on the RGB LED panel", default = "open('data.txt','r')")
58
+
52
-                             ^
59
+ に変更しました。
60
+ しかし、LEDマトリクスパネルに表示されるのはテキストファイルの結果ではなくopen('data.txt','r')という文字列でした。
61
+
62
+ LEDマトリクスパネルに表示される文字列を外部ファイル(テキストファイル)から取り出すにはどの部分を改良するべきなのかアドバイスをいただきたく質問させていただきました。
63
+
53
- SyntaxError: invalid syntax
64
+ よろしくお願いします。