【问题标题】:reading, manipulating & displaying text file in tkinter在 tkinter 中读取、操作和显示文本文件
【发布时间】:2023-03-02 21:50:01
【问题描述】:

我对 python 很陌生。我正在尝试使用 tkinter 读取文本文件,然后进行操作,最后显示结果。所以基本上有 3 个步骤。

这是我的示例文件,格式固定:

DOWN 07.11.2016 08:21:33 - 07.11.2016 08:22:33
UP   07.11.2016 09:41:07 - 09.11.2016 09:20:33
DOWN 09.11.2016 08:26:33 - 09.11.2016 08:35:33
UP   09.11.2016 08:23:33 - 09.11.2016 08:25:33
DOWN 09.11.2016 08:36:33 - 09.11.2016 08:38:33
DOWN 10.11.2016 08:36:33 - 10.11.2016 08:38:33

文件包含有关 UP 和 DOWN 状态的信息。

第 1 步: 打开和读取文件

from tkinter import *
from tkinter import ttk
from tkinter import filedialog
interface = Tk()
def openfile():
    return filedialog.askopenfilename()
button = ttk.Button(interface, text="Open", command=openfile)  # <------
button.grid(column=1, row=1)

interface.mainloop()

第 2 步:操作

在这里,我尝试检查每一行并检查其是否 DOWN,那么总停机时间是多少,在这种情况下(示例文件)从哪个日期开始,总停机时间为 12 分钟。

第 3 步: 我想在 GUI 屏幕上操作后将这 12 分钟显示为停机时间。 所以最后我在 tinkter 屏幕上的输出应该是

Total Downtime is 12 min from 07.11.2016 08:21:33

如何实现第 2 步和第 3 步,我在互联网上浏览了很多文章,但找不到任何真正有助于解决此问题的内容。 任何帮助都会很棒。

【问题讨论】:

    标签: python python-3.x python-2.7 tkinter


    【解决方案1】:
    try:
        import Tkinter as Tk
        import tkFileDialog as fileDialog
    except ImportError:
        import tkinter as Tk
        fileDialog = Tk.filedialog
    
    import datetime
    
     # Manipulation
    def processText(lines):
        total = 0
        start = None
        for k, line in enumerate(lines):
            direction, date1, time1, _, date2, time2 = line.split()
            if direction != "DOWN": continue
            if start==None: start = date1 + ' ' + time1
            # 1
            D1, M1, Y1 = date1.split('.')
            h1, m1, s1 = time1.split(':')
            # 2
            D2, M2, Y2 = date2.split('.')
            h2, m2, s2 = time2.split(':')
            # Timestamps
            t1 = datetime.datetime(*map(int, [Y1, M1, D1, h1, m1, s1])).timestamp()
            t2 = datetime.datetime(*map(int, [Y2, M2, D2, h2, m2, s2])).timestamp()
            total += (t2-t1)
        return total, start
    
    # Opening and updating
    def openFile():
        filename = fileDialog.askopenfilename()
    
        fileHandle = open(filename, 'r')
        down, start = processText(fileHandle.readlines())
        txt = "Total Downtime is {0} min from {1}".format(down//60, start)
        textVar.set(txt)
    
        fileHandle.close()
    
     # Main
    root = Tk.Tk()
    
    button = Tk.Button(root, text="Open", command=openFile)
    button.grid(column=1, row=1)
    
    textVar = Tk.StringVar(root)
    label = Tk.Label(root, textvariable=textVar)
    label.grid(column=1, row=2)
    
    root.mainloop()
    

    【讨论】:

    • 非常感谢您的回复,当我运行相同时,我得到以下错误。附上回溯。你能建议吗?
    • Aldo 只是想知道如果我在 python 2.7.x 中运行,此步骤是否有任何替代方法 "filename = Tk.filedialog.askopenfilename() # Only works in 3.x"?
    • 以前的错误在我导入“from tkinter import filedialog as fd”后得到修复,但现在有一个新问题。我添加了错误的新回溯。我尝试将“start”变量声明为全局变量,但这也无助于解决它。
    • 感谢修复,但显示的输出就在“打开”按钮的下方。是否可以以框或美化的方式显示 o/p? AlsoIf我想提供我想要总停机时间的自定义日期可以完成吗?如果发生这种情况,它将真正解决问题!提前非常感谢
    猜你喜欢
    • 1970-01-01
    • 2010-12-01
    • 2018-02-26
    • 1970-01-01
    • 2015-03-19
    • 2014-09-22
    • 2013-05-03
    • 2013-01-23
    • 1970-01-01
    相关资源
    最近更新 更多