python socket通信--Apple的学习笔记
之后,我还需要为python制作界面。就先拿我之前的一个python工具,当时做的配置信息是手工填写到settings文件中,然后调用bat后通过python生成代码。那么我就为它做个界面练练手,主要是把基本控件都用下。摸底后,感觉方案可行,ttk的API还是比较友好的。之后还需要添加treeview控件,用来进行数据实时更新。
入门学习了下tinker和ttk的基本控件后,制作界面如下,感觉布局不灵活。
如下源码功能说明:从json文件中读取原来的配置信息,然后在界面中修改原来的配置信息,通过按钮可以保存为新的配置信息。其实按钮回调函数中我再添加直接call bat文件,即可通过配置文件直接生成c代码了,和之前的工具可以无缝链接~
image.png
动态演示
ttk界面.gif
from tkinter import *
from tkinter.ttk import *
from tkinter.messagebox import *
import json
class HMIFrame(Tk): #HMIFrame is the sub class of Tk
def __init__(self, parent):
self.infolist = ["Author", "ToolVer", "NameOfDBC1","TestNode","ByteOrder1", "ChannelNum","NameOfDBC2",
"ByteOrder2", "RXNMID", "TXNMID", "MappingFile"]
self.txtTag = [] # save
self.save_dict = {}
self.data_dict = {}
self.v = IntVar()
self.v2 = IntVar()
self.row=0
def SaveAndGencode(self,event):
for index, item in enumerate(self.infolist[:4]):
self.save_dict[item] = self.txtTag[index].get()
self.save_dict[self.infolist[6]] = self.txtTag[4].get() # NameOfDBC2
#print(self.save_dict)
self.savefile()
def readfile(self):
global data_dict
with open('setting_cfg.json', 'r', encoding='utf-8') as f:
self.data_dict = json.load(f)
self.save_dict = self.data_dict.copy() # deep copy
def savefile(self):
with open('setting_cfg.json', 'w') as file_obj:
json.dump(self.save_dict, file_obj)
showinfo(title='Info', message="Done!")
def change(self):
self.save_dict['ByteOrder1'] = self.v.get()
def change2(self):
self.save_dict['ByteOrder2'] = self.v2.get()
def setchm(self,parent):
self.save_dict['ChannelNum'] = self.c.get()
if(self.c.get() == "1"):
self.rdIntel2['state'] = 'disabled'
self.rdMotorola2['state'] = 'disabled'
self.txtTag[4]['state'] = 'disabled'
else:
self.rdIntel2['state'] = 'enable'
self.rdMotorola2['state'] = 'enable'
self.txtTag[4]['state'] = 'enable'
def setupWindow(self,parent):
parent.title("Apple Tools")
parent.geometry('600x350')
self.row = 6
# label settings
for index, lbitme in enumerate(self.infolist[:self.row+2]):
self.tag = "lb_" + lbitme # temp obj
self.tag = Label(parent, text=lbitme + ":", font=("Arial", 12), width=15)
self.tag.grid(row=index, sticky=W, padx=10)
# text settings
for index, txtitme in enumerate(self.infolist[:4]):
self.txtTag.append("txt_" + txtitme) # save obj name to list
self.txtTag[index] = Entry(parent,width=60)
self.txtTag[index].grid(row=index, column=1, sticky=E,pady=5) # position
self.txtTag[index].insert(0, self.data_dict[txtitme]) # default value
if(self.row == 6):
self.txtTag.append("txt_NameOfDBC2") # save obj name to list
self.txtTag[4] = Entry(parent,width=60)
self.txtTag[4].grid(row=6, column=1, sticky=E,pady=5) # position of NameOfDBC2
self.txtTag[4].insert(0, self.data_dict['NameOfDBC2']) # default value
# radio settings
self.rdIntel2 = Radiobutton(parent, text='Intel ', variable=self.v2, value=0, command=self.change2)
self.rdIntel2.grid(row=7, column=1, pady=5) # position of ByteOrder2
self.rdMotorola2 = Radiobutton(parent, text='Motorola', variable=self.v2, value=1, command=self.change2)
self.rdMotorola2.grid(row=7, column=1, sticky=W, pady=5)
self.v2.set(self.data_dict['ByteOrder2']) # add default value
# radio settings
self.rdIntel = Radiobutton(parent, text='Intel ', variable=self.v, value=0, command=self.change)
self.rdIntel.grid(row = 4,column=1,pady=5) # position of ByteOrder1
self.rdMotorola = Radiobutton(parent, text='Motorola', variable=self.v, value=1, command=self.change)
self.rdMotorola.grid(row = 4,column=1,sticky=W,pady=5)
self.v.set(self.data_dict['ByteOrder1']) # add default value
# button settings
self.btn = Button(parent, text="Generate Code", width=20)
self.btn.grid(row=10, column=1,pady=25,padx=15,sticky=E)
self.btn.bind("", self.SaveAndGencode)
# combo
self.c = Combobox(parent)
self.c.bind('<>', self.setchm)
self.c['value'] = ("1", "2")
self.c.current(int(self.data_dict['ChannelNum'])-1) # add default value
self.c.grid(row=5, column=1,sticky=W,pady=5) # position of ChannelNum
# "RXNMID", "TXNMID", "MappingFile" use default value
def HMI():
root = Tk()
HMIfm = HMIFrame(root)
HMIfm.readfile()
HMIfm.setupWindow(root)
root.mainloop()
if __name__ == '__main__':
HMI()