【问题标题】:python tkinter interface how to create a new to show txt filepython tkinter接口如何创建一个新的显示txt文件
【发布时间】:2019-11-27 08:16:08
【问题描述】:

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

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

并希望阻止输入相同的文本 例如,如果文本文件有 (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()

【问题讨论】:

    标签: python python-2.7 file tkinter ttk


    【解决方案1】:

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

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

    检查以下示例:

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

    编辑以回答 cmets 中的@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)
    

    【讨论】:

    【解决方案2】:

    我正在尝试相同的方法,但只有一个输出

    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 文件的方式编辑它,就像文件请求者一样。

    【讨论】:

      猜你喜欢
      • 2021-06-24
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 2021-07-04
      相关资源
      最近更新 更多