简体   繁体   English

如何将 Python Tkinter 的输入保存到 txt 文件中?

[英]How can I save inputs from Python Tkinter into a txt file?

from tkinter import *
from tkinter import messagebox
from tkinter import END
from typing import  *
from tkinter import Tk
import tweepy
import time



window = Tk()
window.title("Tweetio")
window.minsize(1000,800)
top_frame = Frame(window).pack()
bottom_frame = Frame(window).pack()
btns_frame = Frame(window, pady = 10)
btns_frame.pack()


def openKey():
    
    newwin = Toplevel(window)
    newwin.title('Insert API Key')
    newwin.geometry('500x250')
    newwin.resizable(0, 0)
    newwin.columnconfigure(0, pad=3)
    newwin.columnconfigure(1, pad=3)
    newwin.rowconfigure(0, pad=6)
    newwin.rowconfigure(1, pad=6)
    newwin.rowconfigure(2, pad=6)
    newwin.rowconfigure(3, pad=6)
    newwin.rowconfigure(4, pad=6)
    Label(newwin, text = "API KEY:").grid(row = 0)
    Label(newwin, text = "API KEY SECRET:").grid(row = 1)
    Label(newwin, text = "BEARER TOKEN:").grid(row = 2)
    Label(newwin, text = "ACCESS TOKEN:").grid(row = 3)
    Label(newwin, text = "ACCESS TOKEN SECRET:").grid(row = 4)
    api_key = Entry(newwin).grid(row = 0, column = 1)
    api_secret_key = Entry(newwin).grid(row = 1, column = 1)
    bearer_token = Entry(newwin).grid(row = 2, column = 1)
    access_token = Entry(newwin).grid(row = 3, column = 1)
    access_token_secret = Entry(newwin).grid(row = 4, column = 1)
     
     
    def saveEntry(): 
        with open ('api_keys.text', 'w', encoding='utf-8') as f:
            f.write(input(api_key) + '\n')
            f.write(input(api_secret_key) + '\n')
            f.write(input(bearer_token) + '\n')
            f.write(input(access_token) + '\n')
            f.write(input(access_token_secret) + '\n')

    Button(newwin, text = "Enter", width = 30, height = 5, pady = 10, fg = "Black", bg = "Gold", command = lambda:saveEntry).grid(row = 8, column = 1)

I'm trying to save the user input into a txt file to be used later in the program.我正在尝试将用户输入保存到一个 txt 文件中,以便稍后在程序中使用。 I want to save all 5 inputs into the same txt folder.我想将所有 5 个输入保存到同一个 txt 文件夹中。 Then, call back on the text file with 'r'.然后,使用“r”回调文本文件。 It's tkinter for Python. Any ideas? Python 是 tkinter。有什么想法吗? Any input would be much appreciated.任何输入将不胜感激。

I tried to use 'w' and f.write but it wasn't working.我尝试使用“w”和 f.write,但没有用。

First, the Entry widgets are not being assigned to variables properly, so you will not be able to retrieve their values.首先, Entry小部件未正确分配给变量,因此您将无法检索它们的值。 Use code below instead:请改用以下代码:

api_key = Entry(newwin)
api_key.grid(row = 0, column = 1)

In addition, you should use the get method on the Entry widgets instead of input to retrieve the user input through the Entry widgets, like this:此外,您应该在Entry小部件上使用get方法而不是input来通过Entry小部件检索用户输入,如下所示:

api_key_value = api_key.get()

along with this:连同这个:

with open ('api_keys.text', 'w', encoding='utf-8') as f:
    f.write(api_key_value + '\n')
    ...

Hope this could help.希望这能有所帮助。

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

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