/
multiPage_test.py
162 lines (132 loc) · 4.65 KB
/
multiPage_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import os
import tkinter as tk
from tkinter import Text, ttk, StringVar, OptionMenu
import paramiko
import threading
import time
LARGEFONT = ("Verdana", 35)
# SSH param
SSHparam = {
"port" : "22",
"hostname" : '192.168.1.131',
# "hostname" : 'raspberrypi.local',
"username" : 'pi',
"password" : 'raspberry'
}
runningCmd = False
#Pre-defined Motion List
handMovements = [
"Swing",
"Slash",
"Horizontal",
"Vertical"
]
kegMovements = [
"Stepping",
"Running"
]
def sshConnectionTest():
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print('Paramiko: Test SSH Connection')
client.connect(**SSHparam)
except:
print('Paramiko: Error')
finally:
print('Paramiko: Test Complete')
client.close()
# print('Paramiko: Test SSH Connection')
def paramikoSendCmd(command):
global runningCmd
if (runningCmd):
return
try:
runningCmd = True
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print('Paramiko: Initiate SSH Connection')
print("SSH Command: " + command)
client.connect(**SSHparam)
(stdin, stdout, stderr) = client.exec_command(command)
cmd_output = stdout.read()
with open('paramiko_log.txt', 'w+') as file:
file.write(str(cmd_output))
return 'paramiko_log.txt'
except:
print('Paramiko: Error')
finally:
print('Paramiko: Close SSH Connection')
client.close()
runningCmd = False
class tkinterApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side = "top", fill = "both", expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
self.frames = {}
for F in (MainPage, Page1, Page2):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky ="nsew")
self.show_frame(MainPage)
def show_frame(self, cont):
with open('test.txt', 'a') as f:
if cont == MainPage:
f.write('Main Page,\n')
elif cont == Page1:
f.write('Page 1,\n')
elif cont == Page2:
f.write('Page 2,\n')
frame = self.frames[cont]
frame.tkraise()
# first window frame MainPage
class MainPage(tk.Frame):
def __init__(self, parent, controller):
with open('test.txt', 'w') as f:
f.write('test txt created\n')
tk.Frame.__init__(self, parent)
testVar = StringVar(self)
testVar.set(handMovements[0]) # default value
label = ttk.Label(self, text ="MainPage", font = LARGEFONT)
label.grid(row = 0, column = 4, padx = 10, pady = 10)
button1 = ttk.Button(self, text ="Settings Page 1",command = lambda : controller.show_frame(Page1))
button1.grid(row = 1, column = 1, padx = 10, pady = 10)
button2 = ttk.Button(self, text ="Settings Page 2",command = lambda : controller.show_frame(Page2))
button2.grid(row=2, column=1, padx=10, pady=10)
button2 = ttk.Button(self, text="SSH Test", command = lambda : threading.Thread(target=paramikoSendCmd, args=("cd ~",), daemon=True).start())
button2.grid(row = 3, column = 2, padx = 10, pady = 10)
gridFrame = tk.Frame(self)
label = ttk.Label(gridFrame, text ="Motion:")
dropDownMenu1 = OptionMenu(gridFrame, testVar, *handMovements)
label.grid(row = 0, column = 0)
dropDownMenu1.grid(row=0, column=1)
gridFrame.grid(row = 4, column = 2, padx = 10, pady = 10)
# second window frame page1
class Page1(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = ttk.Label(self, text ="Settings Page 1", font = LARGEFONT)
label.grid(row = 0, column = 4, padx = 10, pady = 10)
button1 = ttk.Button(self, text ="MainPage",command = lambda : controller.show_frame(MainPage))
button1.grid(row = 1, column = 1, padx = 10, pady = 10)
button2 = ttk.Button(self, text ="Settings Page 2",command = lambda : controller.show_frame(Page2))
button2.grid(row = 2, column = 1, padx = 10, pady = 10)
# third window frame page2
class Page2(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = ttk.Label(self, text ="Settings Page 2", font = LARGEFONT)
label.grid(row = 0, column = 4, padx = 10, pady = 10)
button1 = ttk.Button(self, text ="Settings Page 1",command = lambda : controller.show_frame(Page1))
button1.grid(row = 1, column = 1, padx = 10, pady = 10)
button2 = ttk.Button(self, text ="MainPage",command = lambda : controller.show_frame(MainPage))
button2.grid(row = 2, column = 1, padx = 10, pady = 10)
app = tkinterApp()
app.after(2000, threading.Thread(target=sshConnectionTest, daemon=True).start)
app.mainloop()
# scp ${SSH_CLIENT%% *}:D:/HKUST/Year 2/ISDN2002/GUI/test.txt home/pi