python tkinter接口(interface)如何创建一个新的显示txt文件

标签 python python-2.7 file tkinter ttk

我编写了一个代码,它接受 python 用户输入,并在按下应用时将其插入到文本文件中,如下图所示

GUI

当用户插入新文本时,文本文件将始终更新,如何在应用旁边创建一个新按钮以向用户显示最新的文本文件

enter image description here 并希望防止输入相同的文本 例如,如果文本文件有一个 (go) 程序不要再次输入 (go) 这是我的代码

root = Tk()

ivn = StringVar()
inputVarName = Entry(root, textvariable=str(ivn))
ivn.set(str("text1"))
inputVarName.grid(row=0, column=0)

ivn2 = StringVar()
inputVarName2 = Entry(root, textvariable=str(ivn2))
ivn2.set(str("text2"))
inputVarName2.grid(row=1, column=0)




def writetofile():
    content_list = [ivn.get(), ivn2.get()]

    print("\n".join(content_list))    
    with open("help.txt", "a") as f:
        for item in content_list:
            f.write("%s\n" % item)

applyButton = Button(root, text="Apply", command=writetofile)
applyButton.grid(row=2, column=1)



root.mainloop()

最佳答案

为了防止用户插入相同的文本,只需清空这两个条目,您可以在保存到文件之前检查条目是否不为空。

您可以使用顶层窗口来显示文件内容。

检查以下示例:

from tkinter import Tk, Toplevel, Button, Entry, StringVar, Text, DISABLED, END, W, E 
import tkinter.ttk as ttk
import tkMessageBox

root = Tk()


ivn = StringVar()
inputVarName = Entry(root, textvariable=str(ivn))
ivn.set(str("text1"))
inputVarName.grid(row=0, column=0,columnspan=2)

ivn2 = StringVar()
inputVarName2 = Entry(root, textvariable=str(ivn2))
ivn2.set(str("text2"))
inputVarName2.grid(row=1, column=0,columnspan=2)


def writetofile():
    content_list = [ivn.get(), ivn2.get()]
    if any(content_list):
        print("\n".join(content_list))

        with open("help.txt", 'r+') as inFile:
            for item in content_list:
                if ("%s\n" % item).encode('UTF-8') in inFile:
                    tkMessageBox.showwarning("Warning", "'%s': Already exists!" % item)
                    return

        with open("help.txt", "a") as f:
            for item in content_list:
                f.write( ("%s\n" % item).encode('UTF-8'))

    ivn.set('')
    ivn2.set('')


def showfile():
    top = Toplevel()
    top.title("help.txt")
    textArea = Text(top)

    scrollbar = ttk.Scrollbar(top, command=textArea.yview)
    scrollbar.grid(row=0, column=1, sticky='nsew')

    textArea['yscrollcommand'] = scrollbar.set

    with open("help.txt", "r") as infile:
        textArea.insert(END, infile.read())

    textArea.grid(row=0, column=0)
    textArea.config(state=DISABLED)

applyButton = Button(root, text="Apply", command=writetofile)
applyButton.grid(row=2, column=0, sticky=W+E)

showButton = Button(root, text="Show", command=showfile)
showButton.grid(row=2, column=1, sticky=W+E)



root.mainloop()

编辑以回答评论中@IbrahimOzaeri 的问题。
您可以使用 tkFileDialog.askopenfilename 要求用户选择一个文件:

from Tkinter import Tk
import Tkinter, Tkconstants, tkFileDialog

root = Tk()
root.filename = tkFileDialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("text files","*.txt"),("all files","*.*")))
print (root.filename)

关于python tkinter接口(interface)如何创建一个新的显示txt文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59065815/

相关文章:

python用正斜杠和反斜杠存储路径名

mysql - django多个数据库用于单个项目中的多个组织

file - Go中如何防止程序B归档/删除程序A当前打开的文件?

python - Miniconda3 Linux Python 3.7 64 位,缺少安装程序?

python - Dataframe Slice 不删除索引值

python - Foursquare 身份验证 - 'No handlers could be found for logger "foursquare"'

Python-从字符串中获取括号元组列表

Java创建文件如果不存在

java - android以编程方式删除旧的缓存数据

python - Django - <class> 没有外键