我只是按照一个小教程来用pyton制作了一个小gui程序。我正在尝试制作一个python gui程序来设置/更新其他服务器上我主Python程序中的变量。
我正在尝试将一些变量放在本地文件中(以连接到服务器)。但是我似乎无法使其正常运行,我可以访问文件中的变量,可以写入文件,但是在程序运行时无法更新变量,我希望变量文件由我重新加载gui python脚本,以便使用最新的变量。我还可以将局部变量设置为更新后的值,但是我宁愿重新加载整个文件,以防其他变量也已更新。
Python GUI脚本:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from os import path
import os
import paramiko
from var import *
vars = 'C:/Users/nick/Desktop/var.py'
window = Tk()
window.title("Welcome to LikeGeeks app")
__file__ = 'C:/Users/nick/.ssh/'
def ppkFile():
file = filedialog.askopenfilename(initialdir= path.dirname(__file__), filetypes = (("Primary Key Files","*.rsa"),("all files","*.*")))
print(file)
if file:
ssh_key = file
t = open(vars ,'w')
t.write('ssh_key = \'' + file + '\'')
t.close()
def readVarFile():
t = open('C:/Users/nick/Desktop/var.py' ,'r')
tempstr = t.read()
t.close()
print(tempstr)
def checkVar():
print(ssh_key)
menu = Menu(window)
new_item = Menu(menu,tearoff=0)
new_item.add_command(label='Add Primary key file', command=ppkFile)
new_item.add_command(label='Connect ssh', command=readVarFile)
new_item.add_command(label='Print ssh_key', command=checkVar)
menu.add_cascade(label='File', menu=new_item)
window.config(menu=menu)
tab_control = ttk.Notebook(window)
tab1 = ttk.Frame(tab_control)
tab2 = ttk.Frame(tab_control)
tab_control.add(tab1, text='First')
tab_control.add(tab2, text='Second')
lbl1 = Label(tab1, text= 'label1')
lbl1.grid(column=0, row=0)
lbl2 = Label(tab2, text= 'label2')
lbl2.grid(column=0, row=0)
tab_control.grid(column=0, row=1)
#tab_control.pack(expand=1, fill='both')
#file = filedialog.askopenfilename(initialdir= path.dirname(__file__))
window.mainloop()
var.py文件:
ssh_key = 'C:/Users/nick/.ssh/id_rsa'
因此,当我第一次检查两个变量都很好时,但是当我选择一个新键时,文本文件会更新,但本地变量不会。
输出:
readVarFile = ssh_key = 'C:/Users/nick/.ssh/id_rsa'
checkVar = C:/Users/nick/.ssh/id_rsa
ppkFile = C:/Users/nick/.ssh/known_hosts
#Picked a new ssh key
readVarFile = ssh_key = 'C:/Users/nick/.ssh/known_hosts'
#updated to the new value
checkVar = C:/Users/nick/.ssh/id_rsa
#Still the old value
我以一种“肮脏”的方式工作:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from os import path
import os
import paramiko
from importlib import reload
import fileinput
def ppkFile():
file = filedialog.askopenfilename(initialdir= path.dirname(__file__), filetypes = (("Primary Key Files",""),("all files","*.*")))
print(file)
if file:
updateVarFile('__ssh_key__', file)
refreshVarFromFile()
#t = open(vars ,'a')
#t.write('\n' + '__ssh_key__ = \'' + file + '\'')
#t.close()
#os.system("ssh -i " + file + " root@94.130.187.163")
#os.system("ls -al")
#os.system("pscp -i " + file + " C:/Users/nick/Desktop/var.txt root@94.130.187.163:/var/lib/asterisk/agi-bin/winvar.py")
#os.system("ls -al")
def updateVarFile(svar, s):
print(s)
content_file = []
var_file = open((vars) , 'r+')
for line in var_file:
if line.startswith(svar):
content_file.append(svar + ' = \'' + s + '\'\n')
else:
content_file.append(line)
var_file.seek(0)
var_file.truncate()
print(content_file)
var_file.writelines(content_file)
var_file.close()
def refreshVarFromFile():
import imp
f = open(vars)
global data
data = imp.load_source('data', vars)
f.close
print(data)
refreshGui()
def refreshGui():
#global sshkey
#sshkey.set(data.__ssh_key__)
sshText.delete(0,END)
sshText.insert(0, data.__ssh_key__)
sshHostText.delete(0,END)
sshHostText.insert(0, data.__ssh_host__)
sshUserText.delete(0,END)
sshUserText.insert(0, data.__ssh_user__)
tab2.update()
def checkVar():
print(data.__ssh_key__)
def connect():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(data.__ssh_host__, username=data.__ssh_user__, key_filename= data.__ssh_key__ )
stdin, stdout, stderr = ssh.exec_command('cd /var/lib/asterisk/agi-bin')
stdin, stdout, stderr = ssh.exec_command('ls -al')
print(stdout.readlines())
ssh.close()
def validateSSHAUTH():
updateVarFile('__ssh_key__' , sshText.get())
updateVarFile('__ssh_user__' , sshUserText.get())
updateVarFile('__ssh_host__' , sshHostText.get())
refreshVarFromFile()
def saveConfig():
if chkDoSomething_state.get() == 0:
disableFunction('doSomething')
else:
print('enable function')
enableFunction('doSomething')
def disableFunction(s):
content_file = []
disable = False
var_file = open((bot) , 'r+')
for line in var_file:
if disable:
if line.startswith(' ' or ' '):
content_file.append('#' + line)
else:
disable = False
content_file.append(line)
else:
if line.startswith('def ' + s):
disable = True
content_file.append('#' + line)
else:
content_file.append(line)
var_file.seek(0)
var_file.truncate()
print(content_file)
var_file.writelines(content_file)
var_file.close()
def enableFunction(s):
content_file = []
enable = False
var_file = open((bot), 'r+')
for line in var_file:
if enable:
if line.startswith('# ' or '# '):
content_file.append(line.replace('#',''))
else:
enable = False
content_file.append(line)
else:
if line.startswith('#def ' + s):
enable = True
content_file.append(line.replace('#',''))
else:
content_file.append(line)
var_file.seek(0)
var_file.truncate()
print(content_file)
var_file.writelines(content_file)
var_file.close()
vars = 'C:/Users/nick/Desktop/var.txt'
bot = 'C:/Users/nick/Desktop/var2.txt'
window = Tk()
window.title("Chatbot managment app")
__file__ = 'C:/Users/nick/.ssh/'
menu = Menu(window)
new_item = Menu(menu,tearoff=0)
new_item.add_command(label='Add Primary key file', command=ppkFile)
new_item.add_command(label='Connect ssh', command=connect)
new_item.add_command(label='Reload', command=refreshVarFromFile)
menu.add_cascade(label='File', menu=new_item)
window.config(menu=menu)
tab_control = ttk.Notebook(window)
tab1 = ttk.Frame(tab_control)
tab2 = ttk.Frame(tab_control)
tab_control.add(tab1, text='First')
tab_control.add(tab2, text='SSH-AUTH')
lbl1 = Label(tab1, text= 'label1')
lbl1.grid(column=0, row=0)
chkDoSomething_state = BooleanVar()
chkDoSomething_state.set(True)
chkDoSomething = Checkbutton(tab1, text='Enable doSomething', var=chkDoSomething_state)
chkDoSomething.grid(column=0,row=1)
configFunctionBtn = Button(tab1, text="Save config", command=saveConfig)
configFunctionBtn.grid(column=1, row=1)
lbl2 = Label(tab2, text= 'SSH-key path :')
lbl2.grid(column=0, row=0)
#sshkey = StringVar()
#sshkey.set(data.__ssh_key__)
sshText = Entry(tab2,width=50)
sshText.grid(column=1, row=0)
sshUserLbl = Label(tab2, text='SSH user :')
sshUserLbl.grid(column=0,row=1)
sshUserText = Entry(tab2,width=50)
sshUserText.grid(column=1,row=1)
sshHostLbl = Label(tab2, text='SSH host :')
sshHostLbl.grid(column=0,row=2)
sshHostText = Entry(tab2, width=50)
sshHostText.grid(column=1, row=2)
validatAuthBtn = Button(tab2, text="Update SSH credentials", command=validateSSHAUTH)
validatAuthBtn.grid(column=1,row=3)
#lbl3 = Label(tab2, textvariable=sshkey)
#lbl3.grid(column=1, row = 0)
tab_control.grid(column=0, row=1)
#tab_control.pack(expand=1, fill='both')
refreshVarFromFile()
#file = filedialog.askopenfilename(initialdir= path.dirname(__file__))
window.mainloop()