python tkinter界面如何创建新的显示txt文件

问题描述 投票:1回答:2

我构建了一个代码,该代码需要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()
<<

我正在尝试相同的东西,但是输出一个
import tkinter.ttk as ttk import tkMessageBox root = Tk() root.geometry("500x300") root.title("The Gatway company") ivn = StringVar() inputVarName = Entry(root, textvariable=str(ivn)) ivn.set(str("text1")) inputVarName.grid(row=0, column=0,columnspan=2) def writetofile(): content_list = [ivn.get()] if any(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('') 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) applyButton.config( height = 5, width = 10 ) showButton = Button(root, text="Show", command=showfile) showButton.grid(row=2, column=1, sticky=W+E) showButton.config( height = 5, width = 10 ) root.mainloop()

与您的代码相同,但是对于一个条目,我正在考虑以一种用户可以选择help.txt文件的方式(如文件请求者)的方式对其进行编辑。

为了防止用户插入相同的文本,只需清空两个条目,您可以在保存到文件之前检查条目是否不为空。
您可以使用顶级窗口显示文件内容。

检查以下示例:

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 python-2.7 file tkinter ttk
2个回答
1
投票

2
投票
您可以使用顶级窗口显示文件内容。
© www.soinside.com 2019 - 2024. All rights reserved.