鱼C论坛

 找回密码
 立即注册
查看: 1143|回复: 2

[已解决]小白求救各位大佬,如何解决文件选择框自动弹出问题!!!

[复制链接]
发表于 2021-12-7 17:36:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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()
最佳答案
2021-12-7 18:11:44
不知道你想干什么,只能猜测了
  1. from tkinter import *
  2. from tkinter import filedialog


  3. class Mainpage:
  4.     def __init__(self, master):
  5.         self.root = master
  6.         # 设置窗口标题
  7.         self.root.title("诺基亚GSM网优工具V_1")
  8.         # 设置窗口宽和高是否可变
  9.         self.root.resizable(width=False, height=False)
  10.         # 获取屏幕尺寸以计算布局参数,使窗口居屏幕中央
  11.         self.root.width = 800
  12.         self.root.height = 600
  13.         width = self.root.width
  14.         height = self.root.height
  15.         screenwidth = self.root.winfo_screenwidth()
  16.         screenheight = self.root.winfo_screenheight()
  17.         alignstr = "%dx%d+%d+%d" % (
  18.             width,
  19.             height,
  20.             (screenwidth - width) / 2,
  21.             (screenheight - height) / 2,
  22.         )
  23.         self.root.geometry(alignstr)
  24.         self.creatpage()
  25.         self.fra = Frame(self.root, width=800, height=600)
  26.         self.fra.pack()

  27.     def crateviewpage(self):
  28.         Label(self.fra, text="打开需要读取的Log文件:").place(x=20, y=15)
  29.         self.file_text1 = Text(self.fra)
  30.         self.file_text1.place(x=20, y=40, width=380, height=25)
  31.         Button(self.fra, text="打开转换Log文件...", command=self.read_log).place(x=420, y=40)
  32.         # 插入文本框
  33.         self.file_text2 = Text(self.fra)
  34.         self.file_text2.place(x=20, y=90, width=740, height=480)
  35.         # self.file_text2.insert("insert", self.read_log())

  36.     def read_log(self):
  37.         self.file_path = filedialog.askopenfilename()
  38.         if self.file_path is None:  # fixed BUG 1
  39.             return
  40.         self.file_text1.insert("insert", self.file_path)
  41.         file_opened = open(self.file_path, "r")  # 不要用关键字file做变量名
  42.         a_list = []  # 不要用关键字list做变量名
  43.         counts = 0
  44.         str1 = ""
  45.         # file_name = os.path.basename(self.file_path)
  46.         for each_line in file_opened:
  47.             a_list.append(each_line)
  48.             if "FlexiBSC" in each_line:
  49.                 bsc = each_line[9:19].replace("\n", "")

  50.             if "BCF-" in each_line:
  51.                 bcf = each_line[4:8]
  52.                 bcf_state = each_line[15:21].replace("\n", "")
  53.                 if len(bcf_state) < 6:
  54.                     bcf_state += (6 - len(bcf_state)) * (" ")

  55.             if " BTS-" in each_line:
  56.                 bts = each_line[5:9]
  57.                 bts_state = each_line[15:21].replace("\n", "")
  58.                 bts_name = a_list[counts - 1].replace("\n", "")
  59.                 if len(bts_state) < 6:
  60.                     bts_state += (6 - len(bts_state)) * " "
  61.                 if len(bts_name) < 20:
  62.                     bts_name += (20 - len(bts_name)) * " "

  63.             if "  TRX-" in each_line:
  64.                 trx = each_line[6:9]
  65.                 trx_state = each_line[15:21].replace("\n", "")
  66.                 if len(trx_state) < 6:
  67.                     trx_state += (6 - len(trx_state)) * " "
  68.                 str1 += (
  69.                     "退服载频:"
  70.                     + bsc
  71.                     + "  BCF-"
  72.                     + bcf
  73.                     + "   "
  74.                     + bcf_state
  75.                     + "  BTS-"
  76.                     + bts
  77.                     + "   "
  78.                     + bts_state
  79.                     + "  "
  80.                     + bts_name
  81.                     + "TRX-"
  82.                     + trx
  83.                     + "  "
  84.                     + trx_state
  85.                     + "\n"
  86.                 )
  87.             counts += 1
  88.         self.file_text2.insert("insert", str1)  # 显示文本放到这里来
  89.         # return str1

  90.     def creatpage(self):
  91.         # 创建一级子目录
  92.         menubar = Menu(self.root)
  93.         fmenu1 = Menu(self.root, tearoff=0)
  94.         fmenu2 = Menu(self.root, tearoff=0)
  95.         fmenu3 = Menu(self.root, tearoff=0)
  96.         fmenu4 = Menu(self.root, tearoff=0)
  97.         # 创建‘工具’下二级子目录
  98.         file_menu = ["打开", "保存", "另存为", "设置"]

  99.         for item in file_menu:
  100.             fmenu1.add_command(label=item)

  101.         for item in ["注册用户"]:
  102.             fmenu3.add_command(label=item)

  103.         for item in ["版权信息", "软件说明"]:
  104.             fmenu4.add_command(label=item)
  105.         # add_cascade 的一个很重要的属性就是 menu 属性,它指明了要把那个菜单级联到该菜单项上,
  106.         # 当然,还必不可少的就是 label 属性,用于指定该菜单项的名称
  107.         menubar.add_cascade(label="文件", menu=fmenu1)
  108.         menubar.add_cascade(label="工具", menu=fmenu2)
  109.         menubar.add_cascade(label="注册", menu=fmenu3)
  110.         menubar.add_cascade(label="关于", menu=fmenu4)
  111.         # 添加功能
  112.         fmenu1.add_command(label="退出系统", command=self.root.destroy)
  113.         fmenu2.add_command(label="闭锁载频查询", command=self.crateviewpage)
  114.         # 最后可以用窗口的 menu 属性指定我们使用哪一个作为它的顶层菜单
  115.         self.root.config(menu=menubar)


  116. if __name__ == "__main__":
  117.     root = Tk()
  118.     Mainpage(root)

  119.     root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-12-7 18:11:44 | 显示全部楼层    本楼为最佳答案   
不知道你想干什么,只能猜测了
  1. from tkinter import *
  2. from tkinter import filedialog


  3. class Mainpage:
  4.     def __init__(self, master):
  5.         self.root = master
  6.         # 设置窗口标题
  7.         self.root.title("诺基亚GSM网优工具V_1")
  8.         # 设置窗口宽和高是否可变
  9.         self.root.resizable(width=False, height=False)
  10.         # 获取屏幕尺寸以计算布局参数,使窗口居屏幕中央
  11.         self.root.width = 800
  12.         self.root.height = 600
  13.         width = self.root.width
  14.         height = self.root.height
  15.         screenwidth = self.root.winfo_screenwidth()
  16.         screenheight = self.root.winfo_screenheight()
  17.         alignstr = "%dx%d+%d+%d" % (
  18.             width,
  19.             height,
  20.             (screenwidth - width) / 2,
  21.             (screenheight - height) / 2,
  22.         )
  23.         self.root.geometry(alignstr)
  24.         self.creatpage()
  25.         self.fra = Frame(self.root, width=800, height=600)
  26.         self.fra.pack()

  27.     def crateviewpage(self):
  28.         Label(self.fra, text="打开需要读取的Log文件:").place(x=20, y=15)
  29.         self.file_text1 = Text(self.fra)
  30.         self.file_text1.place(x=20, y=40, width=380, height=25)
  31.         Button(self.fra, text="打开转换Log文件...", command=self.read_log).place(x=420, y=40)
  32.         # 插入文本框
  33.         self.file_text2 = Text(self.fra)
  34.         self.file_text2.place(x=20, y=90, width=740, height=480)
  35.         # self.file_text2.insert("insert", self.read_log())

  36.     def read_log(self):
  37.         self.file_path = filedialog.askopenfilename()
  38.         if self.file_path is None:  # fixed BUG 1
  39.             return
  40.         self.file_text1.insert("insert", self.file_path)
  41.         file_opened = open(self.file_path, "r")  # 不要用关键字file做变量名
  42.         a_list = []  # 不要用关键字list做变量名
  43.         counts = 0
  44.         str1 = ""
  45.         # file_name = os.path.basename(self.file_path)
  46.         for each_line in file_opened:
  47.             a_list.append(each_line)
  48.             if "FlexiBSC" in each_line:
  49.                 bsc = each_line[9:19].replace("\n", "")

  50.             if "BCF-" in each_line:
  51.                 bcf = each_line[4:8]
  52.                 bcf_state = each_line[15:21].replace("\n", "")
  53.                 if len(bcf_state) < 6:
  54.                     bcf_state += (6 - len(bcf_state)) * (" ")

  55.             if " BTS-" in each_line:
  56.                 bts = each_line[5:9]
  57.                 bts_state = each_line[15:21].replace("\n", "")
  58.                 bts_name = a_list[counts - 1].replace("\n", "")
  59.                 if len(bts_state) < 6:
  60.                     bts_state += (6 - len(bts_state)) * " "
  61.                 if len(bts_name) < 20:
  62.                     bts_name += (20 - len(bts_name)) * " "

  63.             if "  TRX-" in each_line:
  64.                 trx = each_line[6:9]
  65.                 trx_state = each_line[15:21].replace("\n", "")
  66.                 if len(trx_state) < 6:
  67.                     trx_state += (6 - len(trx_state)) * " "
  68.                 str1 += (
  69.                     "退服载频:"
  70.                     + bsc
  71.                     + "  BCF-"
  72.                     + bcf
  73.                     + "   "
  74.                     + bcf_state
  75.                     + "  BTS-"
  76.                     + bts
  77.                     + "   "
  78.                     + bts_state
  79.                     + "  "
  80.                     + bts_name
  81.                     + "TRX-"
  82.                     + trx
  83.                     + "  "
  84.                     + trx_state
  85.                     + "\n"
  86.                 )
  87.             counts += 1
  88.         self.file_text2.insert("insert", str1)  # 显示文本放到这里来
  89.         # return str1

  90.     def creatpage(self):
  91.         # 创建一级子目录
  92.         menubar = Menu(self.root)
  93.         fmenu1 = Menu(self.root, tearoff=0)
  94.         fmenu2 = Menu(self.root, tearoff=0)
  95.         fmenu3 = Menu(self.root, tearoff=0)
  96.         fmenu4 = Menu(self.root, tearoff=0)
  97.         # 创建‘工具’下二级子目录
  98.         file_menu = ["打开", "保存", "另存为", "设置"]

  99.         for item in file_menu:
  100.             fmenu1.add_command(label=item)

  101.         for item in ["注册用户"]:
  102.             fmenu3.add_command(label=item)

  103.         for item in ["版权信息", "软件说明"]:
  104.             fmenu4.add_command(label=item)
  105.         # add_cascade 的一个很重要的属性就是 menu 属性,它指明了要把那个菜单级联到该菜单项上,
  106.         # 当然,还必不可少的就是 label 属性,用于指定该菜单项的名称
  107.         menubar.add_cascade(label="文件", menu=fmenu1)
  108.         menubar.add_cascade(label="工具", menu=fmenu2)
  109.         menubar.add_cascade(label="注册", menu=fmenu3)
  110.         menubar.add_cascade(label="关于", menu=fmenu4)
  111.         # 添加功能
  112.         fmenu1.add_command(label="退出系统", command=self.root.destroy)
  113.         fmenu2.add_command(label="闭锁载频查询", command=self.crateviewpage)
  114.         # 最后可以用窗口的 menu 属性指定我们使用哪一个作为它的顶层菜单
  115.         self.root.config(menu=menubar)


  116. if __name__ == "__main__":
  117.     root = Tk()
  118.     Mainpage(root)

  119.     root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-7 20:40:53 | 显示全部楼层
谢谢大佬!给我说说嘛?我问题点主要错在什么地方?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 19:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表