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

質問編集履歴

2

表記方法の変更

2020/05/25 13:10

投稿

temtemtemtem
temtemtemtem

スコア9

title CHANGED
File without changes
body CHANGED
@@ -14,6 +14,7 @@
14
14
  ご指導のほどよろしくお願いします。
15
15
 
16
16
  手続き型では下記のように記入しました。
17
+ ```python
17
18
  # GUIの作成
18
19
  import tkinter as tk
19
20
  import tkinter.ttk as ttk
@@ -25,7 +26,7 @@
25
26
  Canvas Widget を生成
26
27
  canvas = tk.Canvas(root, width=2000, height=1500)
27
28
 
28
- Scrollbar を生成して配置
29
+ #Scrollbar を生成して配置
29
30
  bar_y = tk.Scrollbar(root, orient=tk.VERTICAL)
30
31
  bar_y.pack(side=tk.RIGHT, fill=tk.Y)
31
32
  bar_y.config(command=canvas.yview)
@@ -34,15 +35,14 @@
34
35
  bar_x.pack(side=tk.BOTTOM, fill=tk.X)
35
36
  bar_x.config(command=canvas.xview)
36
37
 
37
- Canvas Widget を配置
38
38
  canvas.config(yscrollcommand=bar_y.set, xscrollcommand=bar_x.set)
39
39
  canvas.config(scrollregion=(0, 0, 4000, 5000)) # スクロール範囲
40
40
  canvas.pack(fill=tk.BOTH, expand=tk.YES)
41
41
 
42
- Frame Widgetを 生成
42
+ # Frame Widgetを 生成
43
43
  frame = tk.Frame(canvas)
44
44
 
45
- Frame Widgetを Canvas Widget上に配置
45
+ # Frame Widgetを Canvas Widget上に配置
46
46
  canvas.create_window((0, 0), window=frame, anchor=tk.NW, width=canvas.cget('width'))
47
47
 
48
48
  notebook = ttk.Notebook(frame)
@@ -72,7 +72,7 @@
72
72
  super().__init__(master, width=2000, height=1000)
73
73
  self.master.title('CBD')
74
74
 
75
- Scrollbar を生成して配置
75
+ # Scrollbar を生成して配置
76
76
  self.bar_y = tk.Scrollbar(self, orient=tk.VERTICAL)
77
77
  self.bar_y.pack(side=tk.RIGHT, fill=tk.Y)
78
78
  self.bar_y.config(command=self.yview)
@@ -82,7 +82,7 @@
82
82
  self.bar_x.config(command=self.xview)
83
83
 
84
84
  self.config(yscrollcommand=self.bar_y.set, xscrollcommand=self.bar_x.set)
85
- self.config(scrollregion=(0, 0, 4000, 5000)) スクロール範囲
85
+ self.config(scrollregion=(0, 0, 4000, 5000)) # スクロール範囲
86
86
  self.pack(fill=tk.BOTH, expand=tk.YES)
87
87
 
88
88
  self.frame = tk.Frame(self)
@@ -116,4 +116,5 @@
116
116
 
117
117
 
118
118
  if __name__ == "__main__":
119
- main()
119
+ main()
120
+ ```

1

オブジェクト指向として書いたコードを追記

2020/05/25 13:10

投稿

temtemtemtem
temtemtemtem

スコア9

title CHANGED
File without changes
body CHANGED
@@ -15,6 +15,9 @@
15
15
 
16
16
  手続き型では下記のように記入しました。
17
17
  # GUIの作成
18
+ import tkinter as tk
19
+ import tkinter.ttk as ttk
20
+
18
21
  root = tk.Tk()
19
22
  root.geometry('2000x1500')
20
23
  root.title('テスト')
@@ -51,10 +54,66 @@
51
54
  tab5 = tk.Frame(notebook)
52
55
  tab6 = tk.Frame(notebook)
53
56
 
54
- notebook.add(tab1, text='main')
57
+ notebook.add(tab1, text='tab1')
55
- notebook.add(tab2, text='Upper')
58
+ notebook.add(tab2, text='tab2')
56
- notebook.add(tab3, text='Sole')
59
+ notebook.add(tab3, text='tab3')
57
- notebook.add(tab4, text='Sole Paint')
60
+ notebook.add(tab4, text='tab4')
58
- notebook.add(tab5, text='Sundries')
61
+ notebook.add(tab5, text='tab5')
59
- notebook.add(tab6, text='Packing')
62
+ notebook.add(tab6, text='tab6')
60
- notebook.pack(expand=True, fill='both')
63
+ notebook.pack(expand=True, fill='both')
64
+
65
+ # オブジェクト指向での記入
66
+ import tkinter as tk
67
+ import tkinter.ttk as ttk
68
+
69
+
70
+ class Application(tk.Canvas):
71
+ def __init__(self, master=None):
72
+ super().__init__(master, width=2000, height=1000)
73
+ self.master.title('CBD')
74
+
75
+ Scrollbar を生成して配置
76
+ self.bar_y = tk.Scrollbar(self, orient=tk.VERTICAL)
77
+ self.bar_y.pack(side=tk.RIGHT, fill=tk.Y)
78
+ self.bar_y.config(command=self.yview)
79
+
80
+ self.bar_x = tk.Scrollbar(self, orient=tk.HORIZONTAL)
81
+ self.bar_x.pack(side=tk.BOTTOM, fill=tk.X)
82
+ self.bar_x.config(command=self.xview)
83
+
84
+ self.config(yscrollcommand=self.bar_y.set, xscrollcommand=self.bar_x.set)
85
+ self.config(scrollregion=(0, 0, 4000, 5000)) スクロール範囲
86
+ self.pack(fill=tk.BOTH, expand=tk.YES)
87
+
88
+ self.frame = tk.Frame(self)
89
+ self.create_window((0, 0), window=self.frame, anchor=tk.NW, width=self.cget('width'))
90
+
91
+ nootebook = ttk.Notebook(self.frame)
92
+ Note(master=nootebook)
93
+
94
+ class Note(nootebook):
95
+
96
+ def __init__(self, master=None):
97
+ super().__init__(master)
98
+ self.master.title('window')
99
+
100
+ tab1 = tk.Frame(self.master)
101
+ self.add(tab1, text="tab1")
102
+ Tab1(master=tab1)
103
+
104
+ tab2 = tk.Frame(self.master)
105
+ self.add(tab2, text="tab2")
106
+ Tab2(master=tab2)
107
+
108
+ self._quit_outside_widget()
109
+ self.pack()
110
+
111
+
112
+ def main():
113
+ root = tk.Tk()
114
+ app = Application(master=root) # Inherit
115
+ app.mainloop()
116
+
117
+
118
+ if __name__ == "__main__":
119
+ main()