简体   繁体   English

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

[英]python tkinter interface how to create a new to show txt file

I buid a code which takes python user input and insert it into a text file when pressing apply as shown in the picture bellow 我按如下所示显示一个代码,该代码需要python用户输入并将其插入文本文件,然后按应用

图形用户界面

and the text file will always be updated when the user inserts a new text, how to create an new button next to apply to show up to date text file to the user 并且当用户插入新文本时,文本文件将始终更新,如何在旁边创建新按钮以向用户显示最新的文本文件

在此处输入图片说明 and want prevent to enter the same text example if the text file has a (go) the program do not enter (go) again this is my code 并希望防止输入相同的文本示例(如果文本文件中有(执行)程序则不再输入(执行)),这是我的代码

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()

To prevent a user from inserting the same text just empty the two entries, and you can check if entries are not empty before saving to a file. 为了防止用户插入相同的文本,只需将两个条目清空即可,您可以在保存到文件之前检查条目是否不为空。

You can use a top-level window to show the file content. 您可以使用顶级窗口显示文件内容。

Check the following example: 检查以下示例:

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()

Edited to answer @IbrahimOzaeri question in comments. 编辑为在评论中回答@IbrahimOzaeri问题。
You can use tkFileDialog.askopenfilename to ask user to select a file: 您可以使用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)

I was trying something same but with one output 我正在尝试相同的东西但有一个输出

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()

it's same as your code but for one entry, I'm thinking to edit it in a such way that the user chooses the help.txt file like a file requester. 它与您的代码相同,但是对于一个条目,我正在考虑以一种用户可以像文件请求者一样选择help.txt文件的方式对其进行编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM