python语言可换主题ttk界面从服务器拿日志文件

今天 546阅读

简单界面tkinter库,稍微美化界面就安装一下ttkthemes

pip3 install ttkthemes

简单使用

themes = ttkthemes.THEMES
themesName = themes[random.randint(0, len(themes) - 1)]
print('主题 ' + themesName)
top = ttkthemes.ThemedTk(theme=themesName, toplevel=True, themebg=True)
# top = Tk()

这样代码只能是启动的时候就定下来主题,运行中要更换记得要同时设置一下背景和字体

采用vb插件画好基础界面,使用中又添加了些小功能

https://github.com/cdhigh/tkinter-designer
class Application_ui(Frame):
# The class will create all widgets for UI.
def __init__(self, master=None):
Frame.__init__(self, master)
self.master.title('拿日志文件')
self.master.protocol('WM_DELETE_WINDOW', self.EV_WM_DELETE_WINDOW)
# To center the window on the screen.
w = 800
h = 300
ws = self.master.winfo_screenwidth()
hs = self.master.winfo_screenheight()
x = (ws / 2) - (w / 2)
y = (hs / 2) - (h / 2)
self.master.geometry('%dx%d+%d+%d' % (w, h, x, y))

self.createWidgets()
self.Combo1.current(0)

def createWidgets(self):
self.top = self.winfo_toplevel()
if os.path.exists("sd.png"):
self.top.iconphoto(True, PhotoImage(file='sd.png'))

self.style = Style()
self.Text1Font = Font(font=('阿里巴巴普惠体', 11))
self.style.configure('.', font=self.Text1Font)
self.Label1Var = StringVar(value='目标')
self.style.configure('TLabel1.TLabel', anchor='w', font=self.Text1Font)
self.Label1 = Label(self.top, text='目标', textvariable=self.Label1Var, style='TLabel1.TLabel')
self.Label1.setText = lambda x: self.Label1Var.set(x)
self.Label1.text = lambda: self.Label1Var.get()
self.Label1.place(relx=0.026, rely=0.054, relwidth=0.06, relheight=0.08)

self.Combo1List = ['192.168.45.116', '192.168.45.251', '192.168.45.235', '192.168.70.23', '192.168.70.24', ]
self.Combo1Var = StringVar(value='192.168.45.116')
self.Combo1 = Combobox(self.top, text='192.168.45.116', textvariable=self.Combo1Var, values=self.Combo1List, font=self.Text1Font)
self.Combo1.setText = lambda x: self.Combo1Var.set(x)
self.Combo1.text = lambda: self.Combo1Var.get()
self.Combo1.place(relx=0.1, rely=0.054, relwidth=0.22)

self.Command2Var = StringVar(value='拿文件')
self.style.configure('TCommand2.TButton', font=self.Text1Font)
self.Command2 = Button(self.top, text='拿文件', textvariable=self.Command2Var, command=self.Command2_Cmd, style='TCommand2.TButton')
self.Command2.setText = lambda x: self.Command2Var.set(x)
self.Command2.text = lambda: self.Command2Var.get()
self.Command2.place(relx=0.82, rely=0.4, relwidth=0.14)

self.Command1Var = StringVar(value='退出')
self.style.configure('TCommand1.TButton', font=self.Text1Font)
self.Command1 = Button(self.top, text='退出', textvariable=self.Command1Var, command=self.Command1_Cmd, style='TCommand1.TButton')
self.Command1.setText = lambda x: self.Command1Var.set(x)
self.Command1.text = lambda: self.Command1Var.get()
self.Command1.place(relx=0.82, rely=0.7, relwidth=0.14)

self.Text1 = Text(self.top, font=self.Text1Font)
self.Text1.place(relx=0.026, rely=0.187, relwidth=0.776, relheight=0.726)
self.Text1.insert('1.0', 'Text1')

self.topRadioVar = StringVar(value='1')
self.style.configure('TOption2.TRadiobutton', font=self.Text1Font)
self.Option2 = Radiobutton(self.top, text='缓存', value='1', variable=self.topRadioVar, style='TOption2.TRadiobutton')
self.Option2.setValue = lambda x: self.topRadioVar.set('1' if x else '0')
self.Option2.value = lambda: 1 if self.topRadioVar.get() == '1' else 0
self.Option2.place(relx=0.43, rely=0.054, relwidth=0.09, relheight=0.1)

self.style.configure('TOption1.TRadiobutton', font=self.Text1Font)
self.Option1 = Radiobutton(self.top, text='实时', value='0', variable=self.topRadioVar, style='TOption1.TRadiobutton')
self.Option1.setValue = lambda x: self.topRadioVar.set('0' if x else '1')
self.Option1.value = lambda: 0 if self.topRadioVar.get() == '0' else 1
self.Option1.place(relx=0.34, rely=0.054, relwidth=0.09, relheight=0.1)

self.style.configure('TLabel2.TLabel', anchor='w', font=self.Text1Font)
self.Label2 = Label(self.top, text='主题', style='TLabel2.TLabel')
self.Label2.place(relx=0.74, rely=0.054, relwidth=0.06, relheight=0.08)

self.Combo2List = ['adapta', 'arc', 'winxpblue', 'smog', 'scidblue', 'yaru', ]
self.Combo2Var = StringVar(value='arc')
self.Combo2 = Combobox(self.top, text='arc', textvariable=self.Combo2Var, values=self.Combo2List, font=self.Text1Font)
self.Combo2.setText = lambda x: self.Combo2Var.set(x)
self.Combo2.text = lambda: self.Combo2Var.get()
self.Combo2.place(relx=0.82, rely=0.054, relwidth=0.14)
self.Combo2Var.trace('w', self.Combo2_Change)
self.Combo2.bind('<<ComboboxSelected>>', self.Combo2_Change)

然后是具体调用功能代码

class Application(Application_ui):
# The class will implement callback function for events and your logical code.
def __init__(self, master=None):
Application_ui.__init__(self, master)
self.host_ip = extract_ip()
self.Combo1.set(self.host_ip)

def change_theme(root, themes_name):
"""
更改风格
"""
print(themes_name)
style = ttkthemes.ThemedStyle(root)
style.set_theme(themes_name)
color = Style(root).lookup("TFrame", "background", default="white")
root.top.config(background=color)
root.style.configure('.', font=root.Text1Font)

def EV_WM_DELETE_WINDOW(self, event=None):
if askyesno(title='退出确认', message='请问是否要退出&#xff1f;'):
self.master.destroy()

def Command2_Cmd(self, event=None):
ip = self.Combo1.get()
print(ip)
r = requests.get('http://%s:8080/uploadFile?ext=1&type=%s' % (ip, self.topRadioVar.get()))
file = r.content.decode()
self.Text1.insert(INSERT, file + '\n')
if ip == self.host_ip:
os.system('scp qzc@%s:%s %s' % (ip, file, os.getcwd()))
else:
os.system('scp root@%s:%s %s' % (ip, file, os.getcwd()))
self.Text1.insert(INSERT, os.getcwd() + '\n')

def Combo2_Change(self, *args):
print(self.Combo2.get())
self.change_theme(self.Combo2.get())

def Command1_Cmd(self, event=None):
e = askyesno(title='退出确认', message='请问是否要退出&#xff1f;')
if e:
self.quit()

原理很简单&#xff0c;通过http接口拿到服务器文件名&#xff0c;然后用scp命令把文件下载到本地&#xff0c;记得是通过命令ssh-copy-id -i root@192.168.70.23事先向服务器认证过了&#xff0c;否则拿文件时因为没输入密码失败

用到一个查询本机ip地址公用函数

def extract_ip():
"""
查询本机ip地址
:return:
"""
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('10.255.255.255', 1))
ip = s.getsockname()[0]
except Exception:
ip = '127.0.0.1'
finally:
s.close()
return ip

可以编译成独立文件脱离python环境使用

pyinstaller -F -w getjclog.py

linux下面文件本身没有图标&#xff0c;编译命令就不需要加ico了&#xff0c;运行起来如果想再任务栏显示图标&#xff0c;可以放一个sd.png文件到可执行文件同路径下