【问题标题】:How do I fix the entry.get() function in Tkinter? [duplicate]如何修复 Tkinter 中的 entry.get() 函数? [复制]
【发布时间】:2020-04-28 18:06:30
【问题描述】:

这是一个非常愚蠢的项目,用于测试我在 python 3.8 中的绝对初学者技能。但我收到以下错误:

AttributeError: 'NoneType' object has no attribute 'get'

我不知道怎么了。

import tkinter as tk

window = tk.Tk()
window.title('File Writer')

def getfile():
    text1 = entry1.get()
    text2 = entry2.get()
    text3 = entry3.get()
    text4 = entry4.get()
    fl = fl.get
    save = open('C:\\Users\\Anas\\Desktop\\' + fl + '.txt', 'w')
    save.write('Your name: ' +  text1 + '\n')
    save.write('Your Birthdate: ' + text2 + '\n')
    save.write('Your City: ' + text3 + '\n')
    save.write('Your Work: ' + text4 + '\n')
    save.close()


tk.Label(window, text='Welcome to file writer', font="Times 22 bold").pack()
tk.Label(window, text="What is your full name?").pack()
entry1 = tk.Entry(window).pack()
tk.Label(window, text="When is your birthdate (DD/MM/YYYY").pack()
entry2 = tk.Entry(window).pack()
tk.Label(window, text="Where do you live? (City, State, Country").pack()
entry3 = tk.Entry(window).pack()
tk.Label(window, text="What is your job? (position/company)").pack()
entry4 = tk.Entry(window).pack()
tk.Label(window, text='What do you want to name your file').pack()
entry5 = tk.Entry(window).pack()
tk.Button(window, text="Click to save your file", font='Arial 20', command=getfile).pack()


window.mainloop()

【问题讨论】:

  • entry1 等不是条目 - 它们是 None,是在您的条目上调用 .pack() 的结果。
  • 另外,f1 未定义。
  • 作为注释,您需要添加.get()。你忘记了括号。

标签: python python-3.x tkinter python-requests


【解决方案1】:

当你定义条目时,你放了 .pack() 方法,但是 pack、grid 或 place 不返回元素 (tk.Entry),而是 None,所以你当你最后用 .pack() 定义了元素,

entry1 = tk.Entry(window).pack() # .pack() -> returns None

变量entry1的值被设置为None

解决这个问题可以先定义变量,再添加包

entry1 = tk.Entry(window) # now the element is stored in the variable
entry1.pack()

我建议你更改路径C:\\Users\\Anas\\Desktop\\

来自用户的输入,以便其他计算机可以使用 或者做这样的事情

import getpass
path = f'C:\\Users\\{getpass.getuser()}\\Desktop\\'

【讨论】:

    猜你喜欢
    • 2017-01-10
    • 1970-01-01
    • 2020-12-01
    • 2023-02-09
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    相关资源
    最近更新 更多