回答編集履歴
4
文言修正
answer
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
> command = self.button_click("1")
|
3
3
|
> ```
|
4
4
|
|
5
|
-
これでは、`self.button_click("1")` を呼び出し
|
5
|
+
これでは、`self.button_click("1")` を呼び出して戻り値を command にセットしてしまいます。
|
6
6
|
commandには関数の戻り値ではなく、関数そのものを指定しないといけないです。
|
7
|
-
このプログラムであれば、lambda関数にすればいいですよ。
|
7
|
+
このプログラムであれば、lambda関数にして、button_clickを呼び出す処理を command に指定すればいいですよ。
|
8
8
|
それと、引数での`=`前後の空白は入れないようにしましょう。
|
9
9
|
参考: https://pep8-ja.readthedocs.io/ja/latest/
|
10
10
|
|
3
不要コード削除
answer
CHANGED
@@ -18,12 +18,12 @@
|
|
18
18
|
def __init__(self, master=None):
|
19
19
|
super().__init__(master)
|
20
20
|
|
21
|
-
|
21
|
+
master.title("ボタンの作成") # ウィンドウタイトル
|
22
|
-
|
22
|
+
master.geometry("1200x800") # ウィンドウサイズ(幅x高さ)
|
23
23
|
|
24
24
|
# --------------------------------------------------------
|
25
25
|
# ボタン1の作成
|
26
|
-
button1 = tk.Button(
|
26
|
+
button1 = tk.Button(master,
|
27
27
|
font=("", "20"),
|
28
28
|
text="ボタン1", # ボタンの表示名
|
29
29
|
command=lambda: self.button_click("1"), # クリックされたときに呼ばれるメソッド
|
@@ -33,7 +33,7 @@
|
|
33
33
|
button1.place(x=500, y=250)
|
34
34
|
# --------------------------------------------------------
|
35
35
|
# ボタン2の作成
|
36
|
-
button2 = tk.Button(
|
36
|
+
button2 = tk.Button(master,
|
37
37
|
font=("", "20"),
|
38
38
|
text="ボタン2", # ボタンの表示名
|
39
39
|
command=lambda: self.button_click("2"), # クリックされたときに呼ばれるメソッド
|
@@ -43,6 +43,7 @@
|
|
43
43
|
button2.place(x=800, y=250)
|
44
44
|
# --------------------------------------------------------
|
45
45
|
|
46
|
+
@staticmethod
|
46
47
|
def VideoPlayback():
|
47
48
|
print("成功")
|
48
49
|
|
@@ -50,12 +51,7 @@
|
|
50
51
|
print(a)
|
51
52
|
Application.VideoPlayback()
|
52
53
|
|
53
|
-
def on_press(key):
|
54
|
-
print('{0} pressed'.format(key))
|
55
|
-
if key.char == "a":
|
56
|
-
Application.VideoPlayback()
|
57
54
|
|
58
|
-
|
59
55
|
if __name__ == "__main__":
|
60
56
|
root = tk.Tk()
|
61
57
|
app = Application(master=root)
|
2
説明追記
answer
CHANGED
@@ -4,10 +4,12 @@
|
|
4
4
|
|
5
5
|
これでは、`self.button_click("1")` を呼び出した結果を command にセットしてしまいます。
|
6
6
|
commandには関数の戻り値ではなく、関数そのものを指定しないといけないです。
|
7
|
-
lambda関数にすればいいですよ。
|
7
|
+
このプログラムであれば、lambda関数にすればいいですよ。
|
8
8
|
それと、引数での`=`前後の空白は入れないようにしましょう。
|
9
9
|
参考: https://pep8-ja.readthedocs.io/ja/latest/
|
10
10
|
|
11
|
+
私の環境には pynput がないので、それを削除して動作確認したコードを示します。
|
12
|
+
|
11
13
|
```python
|
12
14
|
import tkinter as tk
|
13
15
|
|
1
ソースコード追加
answer
CHANGED
@@ -9,5 +9,53 @@
|
|
9
9
|
参考: https://pep8-ja.readthedocs.io/ja/latest/
|
10
10
|
|
11
11
|
```python
|
12
|
+
import tkinter as tk
|
13
|
+
|
14
|
+
|
15
|
+
class Application(tk.Frame):
|
16
|
+
def __init__(self, master=None):
|
17
|
+
super().__init__(master)
|
18
|
+
|
19
|
+
self.master.title("ボタンの作成") # ウィンドウタイトル
|
20
|
+
self.master.geometry("1200x800") # ウィンドウサイズ(幅x高さ)
|
21
|
+
|
22
|
+
# --------------------------------------------------------
|
23
|
+
# ボタン1の作成
|
24
|
+
button1 = tk.Button(self.master,
|
25
|
+
font=("", "20"),
|
26
|
+
text="ボタン1", # ボタンの表示名
|
12
|
-
command=lambda: self.button_click("1")
|
27
|
+
command=lambda: self.button_click("1"), # クリックされたときに呼ばれるメソッド
|
28
|
+
width=10,
|
29
|
+
height=5
|
30
|
+
)
|
31
|
+
button1.place(x=500, y=250)
|
32
|
+
# --------------------------------------------------------
|
33
|
+
# ボタン2の作成
|
34
|
+
button2 = tk.Button(self.master,
|
35
|
+
font=("", "20"),
|
36
|
+
text="ボタン2", # ボタンの表示名
|
37
|
+
command=lambda: self.button_click("2"), # クリックされたときに呼ばれるメソッド
|
38
|
+
width=10,
|
39
|
+
height=5
|
40
|
+
)
|
41
|
+
button2.place(x=800, y=250)
|
42
|
+
# --------------------------------------------------------
|
43
|
+
|
44
|
+
def VideoPlayback():
|
45
|
+
print("成功")
|
46
|
+
|
47
|
+
def button_click(self, a):
|
48
|
+
print(a)
|
49
|
+
Application.VideoPlayback()
|
50
|
+
|
51
|
+
def on_press(key):
|
52
|
+
print('{0} pressed'.format(key))
|
53
|
+
if key.char == "a":
|
54
|
+
Application.VideoPlayback()
|
55
|
+
|
56
|
+
|
57
|
+
if __name__ == "__main__":
|
58
|
+
root = tk.Tk()
|
59
|
+
app = Application(master=root)
|
60
|
+
app.mainloop()
|
13
61
|
```
|