|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
点击目录里的“工具”下的“闭锁载频查询”,会自动弹出文件选择窗口,本意是通过按钮控制,有没有大佬解答一下?
源代码如下:
from tkinter import *
from tkinter import filedialog
class Mainpage:
def __init__(self, master):
self.root = master
# 设置窗口标题
self.root.title('诺基亚GSM网优工具V_1')
# 设置窗口宽和高是否可变
self.root.resizable(width=False, height=False)
# 获取屏幕尺寸以计算布局参数,使窗口居屏幕中央
self.root.width = 800
self.root.height = 600
width = self.root.width
height = self.root.height
screenwidth = self.root.winfo_screenwidth()
screenheight = self.root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
self.root.geometry(alignstr)
self.creatpage()
self.fra = Frame(self.root,width=800,height=600)
self.fra.pack()
def crateviewpage(self):
Label(self.fra, text="打开需要读取的Log文件:").place(x=20, y=15)
self.file_text1 = Text(self.fra)
self.file_text1.place(x=20, y=40, width=380, height=25)
Button(self.fra, text='打开转换Log文件...', command=self.read_log).place(x=420, y=40)
# 插入文本框
self.file_text2 = Text(self.fra)
self.file_text2.place(x=20, y=90, width=740, height=480)
self.file_text2.insert("insert", self.read_log())
def read_log(self):
self.file_path = filedialog.askopenfilename()
self.file_text1.insert("insert", self.file_path)
file = open(self.file_path, 'r')
list = []
counts = 0
str1 = ""
#file_name = os.path.basename(self.file_path)
for each_line in file:
list.append(each_line)
if 'FlexiBSC' in each_line:
bsc = each_line[9:19].replace("\n", "")
if 'BCF-' in each_line:
bcf = each_line[4:8]
bcf_state = each_line[15:21].replace("\n", "")
if len(bcf_state) < 6:
bcf_state += ((6 - len(bcf_state)) * (" "))
if ' BTS-' in each_line:
bts = each_line[5:9]
bts_state = each_line[15:21].replace("\n", "")
bts_name = list[counts - 1].replace("\n", "")
if len(bts_state) < 6:
bts_state += ((6 - len(bts_state)) * " ")
if len(bts_name) < 20:
bts_name += ((20 - len(bts_name)) * " ")
if ' TRX-' in each_line:
trx = each_line[6:9]
trx_state = each_line[15:21].replace("\n", "")
if len(trx_state) < 6:
trx_state += ((6 - len(trx_state)) * " ")
str1 += (
"退服载频:" + bsc + " BCF-" + bcf + " " + bcf_state + " BTS-" + bts + " " + bts_state + " " + bts_name + "TRX-" + trx + " " + trx_state + "\n")
counts += 1
return str1
def creatpage(self):
# 创建一级子目录
menubar = Menu(self.root)
fmenu1 = Menu(self.root, tearoff=0)
fmenu2 = Menu(self.root, tearoff=0)
fmenu3 = Menu(self.root, tearoff=0)
fmenu4 = Menu(self.root, tearoff=0)
# 创建‘工具’下二级子目录
file_menu = ['打开', '保存', '另存为', '设置']
for item in file_menu:
fmenu1.add_command(label=item)
for item in ['注册用户']:
fmenu3.add_command(label=item)
for item in ["版权信息", "软件说明"]:
fmenu4.add_command(label=item)
# add_cascade 的一个很重要的属性就是 menu 属性,它指明了要把那个菜单级联到该菜单项上,
# 当然,还必不可少的就是 label 属性,用于指定该菜单项的名称
menubar.add_cascade(label="文件", menu=fmenu1)
menubar.add_cascade(label="工具", menu=fmenu2)
menubar.add_cascade(label="注册", menu=fmenu3)
menubar.add_cascade(label="关于", menu=fmenu4)
# 添加功能
fmenu1.add_command(label='退出系统', command=self.root.destroy)
fmenu2.add_command(label='闭锁载频查询',command=self.crateviewpage)
# 最后可以用窗口的 menu 属性指定我们使用哪一个作为它的顶层菜单
self.root.config(menu=menubar)
if __name__ == '__main__':
root = Tk()
Mainpage(root)
root.mainloop()
不知道你想干什么,只能猜测了 from tkinter import *
from tkinter import filedialog
class Mainpage:
def __init__(self, master):
self.root = master
# 设置窗口标题
self.root.title("诺基亚GSM网优工具V_1")
# 设置窗口宽和高是否可变
self.root.resizable(width=False, height=False)
# 获取屏幕尺寸以计算布局参数,使窗口居屏幕中央
self.root.width = 800
self.root.height = 600
width = self.root.width
height = self.root.height
screenwidth = self.root.winfo_screenwidth()
screenheight = self.root.winfo_screenheight()
alignstr = "%dx%d+%d+%d" % (
width,
height,
(screenwidth - width) / 2,
(screenheight - height) / 2,
)
self.root.geometry(alignstr)
self.creatpage()
self.fra = Frame(self.root, width=800, height=600)
self.fra.pack()
def crateviewpage(self):
Label(self.fra, text="打开需要读取的Log文件:").place(x=20, y=15)
self.file_text1 = Text(self.fra)
self.file_text1.place(x=20, y=40, width=380, height=25)
Button(self.fra, text="打开转换Log文件...", command=self.read_log).place(x=420, y=40)
# 插入文本框
self.file_text2 = Text(self.fra)
self.file_text2.place(x=20, y=90, width=740, height=480)
# self.file_text2.insert("insert", self.read_log())
def read_log(self):
self.file_path = filedialog.askopenfilename()
if self.file_path is None: # fixed BUG 1
return
self.file_text1.insert("insert", self.file_path)
file_opened = open(self.file_path, "r") # 不要用关键字file做变量名
a_list = [] # 不要用关键字list做变量名
counts = 0
str1 = ""
# file_name = os.path.basename(self.file_path)
for each_line in file_opened:
a_list.append(each_line)
if "FlexiBSC" in each_line:
bsc = each_line[9:19].replace("\n", "")
if "BCF-" in each_line:
bcf = each_line[4:8]
bcf_state = each_line[15:21].replace("\n", "")
if len(bcf_state) < 6:
bcf_state += (6 - len(bcf_state)) * (" ")
if " BTS-" in each_line:
bts = each_line[5:9]
bts_state = each_line[15:21].replace("\n", "")
bts_name = a_list[counts - 1].replace("\n", "")
if len(bts_state) < 6:
bts_state += (6 - len(bts_state)) * " "
if len(bts_name) < 20:
bts_name += (20 - len(bts_name)) * " "
if " TRX-" in each_line:
trx = each_line[6:9]
trx_state = each_line[15:21].replace("\n", "")
if len(trx_state) < 6:
trx_state += (6 - len(trx_state)) * " "
str1 += (
"退服载频:"
+ bsc
+ " BCF-"
+ bcf
+ " "
+ bcf_state
+ " BTS-"
+ bts
+ " "
+ bts_state
+ " "
+ bts_name
+ "TRX-"
+ trx
+ " "
+ trx_state
+ "\n"
)
counts += 1
self.file_text2.insert("insert", str1) # 显示文本放到这里来
# return str1
def creatpage(self):
# 创建一级子目录
menubar = Menu(self.root)
fmenu1 = Menu(self.root, tearoff=0)
fmenu2 = Menu(self.root, tearoff=0)
fmenu3 = Menu(self.root, tearoff=0)
fmenu4 = Menu(self.root, tearoff=0)
# 创建‘工具’下二级子目录
file_menu = ["打开", "保存", "另存为", "设置"]
for item in file_menu:
fmenu1.add_command(label=item)
for item in ["注册用户"]:
fmenu3.add_command(label=item)
for item in ["版权信息", "软件说明"]:
fmenu4.add_command(label=item)
# add_cascade 的一个很重要的属性就是 menu 属性,它指明了要把那个菜单级联到该菜单项上,
# 当然,还必不可少的就是 label 属性,用于指定该菜单项的名称
menubar.add_cascade(label="文件", menu=fmenu1)
menubar.add_cascade(label="工具", menu=fmenu2)
menubar.add_cascade(label="注册", menu=fmenu3)
menubar.add_cascade(label="关于", menu=fmenu4)
# 添加功能
fmenu1.add_command(label="退出系统", command=self.root.destroy)
fmenu2.add_command(label="闭锁载频查询", command=self.crateviewpage)
# 最后可以用窗口的 menu 属性指定我们使用哪一个作为它的顶层菜单
self.root.config(menu=menubar)
if __name__ == "__main__":
root = Tk()
Mainpage(root)
root.mainloop()
|
|