Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 阅读,操作,;在tkinter中显示文本文件_Python_Python 3.x_Python 2.7_Tkinter - Fatal编程技术网

Python 阅读,操作,;在tkinter中显示文本文件

Python 阅读,操作,;在tkinter中显示文本文件,python,python-3.x,python-2.7,tkinter,Python,Python 3.x,Python 2.7,Tkinter,我对python还很陌生。我试图用tkinter读取一个文本文件,然后进行操作,最后显示结果 以下是我的示例文件,其格式将固定: 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:

我对python还很陌生。我试图用tkinter读取一个文本文件,然后进行操作,最后显示结果

以下是我的示例文件,其格式将固定:

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
文件包含有关启动和关闭状态的信息

步骤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步和第3步,我在互联网上浏览了很多文章,但没有找到任何真正有助于解决这个问题的方法。
任何帮助都会很好。

非常感谢您的回复,当我运行相同的程序时,我会发现以下错误。附上回溯。你能告诉我吗?阿尔多只是想知道,如果我在python 2.7.x中运行,“filename=Tk.filedialog.askopenfilename()#仅在3.x中工作”这一步骤是否有其他替代方法?以前的错误在我导入“从tkinter导入filedialog作为fd”后得到修复,但现在出现了一个新问题。我添加了带有错误的新回溯。我曾尝试将“start”变量声明为全局变量,但这也无助于解决它。感谢您的修复,但显示的输出仅在“打开”按钮下方。是否可以以方框或美化的方式显示o/p?另外,如果我想提供我想要的总停机时间的自定义日期,可以这样做吗?如果发生这种情况,它将真正解决问题!!提前多谢
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()
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()