如何从 tkinter 中的文本输入中获取变量?
How do I get a variable from a text input in tkinter?
我试图通过这样做从文本输入中获取变量:
username_var = StringVar()
user_name_input_area = Entry(top, width = 30, textvariable=username_var).place(x = 110, y = 100)
username = username_var.get()
然而,当我尝试访问函数中的变量时,它什么也没做。
我的两个函数是:
def ifSubmit():
writeToFile(service)
writeToFile(username)
def writeToFile(input_variable):
with open("password&usernames.txt", "a") as input:
input.writelines(input_variable)
但是当我打开文本文件时,什么也没有出现。我还测试了:
writeToFile('hello')
在这种情况下,我确实在我的文本文件中收到了 hello,所以我怀疑问题是变量而不是 writeToFile() 函数。
那么我是否正确检索了输入的文本?
完整代码:
import string
import random
from tkinter import *
mypasslist = []
with open("password&usernames.txt", "r") as input:
for line in input:
items = line.split()
mypasslist.append([item for item in items[0:]])
def generatePassword():
characters = string.ascii_letters + string.punctuation + string.digits
password = "".join(random.choice(characters) for x in range(random.randint(8, 16)))
return password
def writeToFile(input_variable):
with open("password&usernames.txt", "a") as input:
input.writelines(input_variable)
top = Tk()
top.geometry("1920x1080")
def ifSubmit():
global a
a = generatePassword()
label1 = Label(top, text='Your password is: %s' % a, font=("Arial", 11)).place(x = 40, y = 170)
label1 = Label(top, justify='left', text='Your password and username has been saved, you can access them below.').place(x = 40, y = 227)
copy_button = Button(top, text = 'Copy', command=copyText).place(x=40, y=195)
writeToFile(service)
writeToFile(username)
writeToFile(str(a))
def copyText():
top.clipboard_clear()
top.clipboard_append(a)
top.update()
# the label for user_name
Service = Label(top, text = "Service").place(x = 40, y = 60)
user_name = Label(top, text = "Username").place(x = 40, y = 100)
submit_button = Button(top, text = "Submit", command=ifSubmit).place(x = 40, y = 130)
service_var = StringVar()
Service_input_area = Entry(top, width = 30, textvariable=service_var)
Service_input_area.place(x = 110, y = 60)
service = service_var.get()
username_var = StringVar()
user_name_input_area = Entry(top, width = 30, textvariable=username_var)
username = username_var.get()
user_name_input_area.place(x = 110, y = 100)
top.mainloop()
有几件事很突出:
假设您正在创建标签小部件:
label = Label(top, text="This is a label").place(x=40, y=40)
这是不正确的。 label
将绑定到 place
方法返回的任何内容,因为您链接了 Label
实例化和 place
方法。 place
方法总是 returns None
,因此 label
将是 None
。您需要将其拆分为两个单独的语句 - 一个用于实例化和绑定小部件,另一个用于放置它:
label = Label(top, text="This is a label")
label.place(x=40, y=40)
您需要为脚本中的每个小部件执行此操作,无论它是标签、按钮等。
另外,在一些地方,你会做这样的事情:
var = StringVar()
entry = Entry(..., textvariable=var)
user_input = var.get()
这也是不正确的。您创建一个条目小部件,然后立即使用 var.get
从中读取。由于您刚刚创建了小部件,其内容将为空,因此在本例中 user_input
将为空。您也永远不会更改 user_input
,或稍后再次从条目小部件中读取,因此您最终写入文件的内容将只是一个空字符串。一旦用户真正有机会写东西,您就需要调用 var.get
。按钮回调将是执行此操作的好地方。
回答已经指出了大部分错误
我还看到你在 isSubmit()
功能下做错了一些事情。在这里,您创建了 2 个标签和一个按钮,只要按下提交按钮就会创建该按钮。您要么需要销毁以前的标签和按钮,要么只创建一次标签和按钮,然后使用 place()
和 place_forget()
来显示和隐藏它们。此外,尽量避免使用 python 内置 class 和函数名称来命名变量。
这是您更正后的代码:
import string
import random
from tkinter import *
mypasslist = []
with open(r"file.txt", "r") as inp:
for line in inp:
items = line.split()
mypasslist.append([item for item in items[0:]])
def generatePassword():
characters = string.ascii_letters + string.punctuation + string.digits
password = "".join(random.choice(characters) for x in range(random.randint(8, 16)))
return password
def writeToFile(input_variable):
with open(r"file.txt", "a") as inp:
inp.writelines(input_variable)
top = Tk()
top.geometry("1920x1080")
def ifSubmit():
passwd = generatePassword()
label1.config(text='Your password is: %s' % passwd)
label1.place(x = 40, y = 170)
label2.config(text='Your password and username has been saved, you can access them below.')
label2.place(x = 40, y = 227)
copy_button = Button(top, text = 'Copy', command=copyText).place(x=40, y=195)
writeToFile(service_var.get()+', ')
writeToFile(username_var.get()+', ')
writeToFile(passwd+'\n')
def copyText():
top.clipboard_clear()
top.clipboard_append(passwd)
top.update()
# the label for user_name
Label(top, text = "Service").place(x = 40, y = 60)
Label(top, text = "Username").place(x = 40, y = 100)
submit_button = Button(top, text = "Submit", command=ifSubmit).place(x = 40, y = 130)
service_var = StringVar()
Service_input_area = Entry(top, width = 30, textvariable=service_var)
Service_input_area.place(x = 110, y = 60)
username_var = StringVar()
user_name_input_area = Entry(top, width = 30, textvariable=username_var)
user_name_input_area.place(x = 110, y = 100)
# label 1
label1 = Label(top, font=("Arial", 11))
label2 = Label(top, justify='left')
copy_button = Button(top, text = 'Copy', command=copyText)
top.mainloop()
我试图通过这样做从文本输入中获取变量:
username_var = StringVar()
user_name_input_area = Entry(top, width = 30, textvariable=username_var).place(x = 110, y = 100)
username = username_var.get()
然而,当我尝试访问函数中的变量时,它什么也没做。
我的两个函数是:
def ifSubmit():
writeToFile(service)
writeToFile(username)
def writeToFile(input_variable):
with open("password&usernames.txt", "a") as input:
input.writelines(input_variable)
但是当我打开文本文件时,什么也没有出现。我还测试了:
writeToFile('hello')
在这种情况下,我确实在我的文本文件中收到了 hello,所以我怀疑问题是变量而不是 writeToFile() 函数。
那么我是否正确检索了输入的文本?
完整代码:
import string
import random
from tkinter import *
mypasslist = []
with open("password&usernames.txt", "r") as input:
for line in input:
items = line.split()
mypasslist.append([item for item in items[0:]])
def generatePassword():
characters = string.ascii_letters + string.punctuation + string.digits
password = "".join(random.choice(characters) for x in range(random.randint(8, 16)))
return password
def writeToFile(input_variable):
with open("password&usernames.txt", "a") as input:
input.writelines(input_variable)
top = Tk()
top.geometry("1920x1080")
def ifSubmit():
global a
a = generatePassword()
label1 = Label(top, text='Your password is: %s' % a, font=("Arial", 11)).place(x = 40, y = 170)
label1 = Label(top, justify='left', text='Your password and username has been saved, you can access them below.').place(x = 40, y = 227)
copy_button = Button(top, text = 'Copy', command=copyText).place(x=40, y=195)
writeToFile(service)
writeToFile(username)
writeToFile(str(a))
def copyText():
top.clipboard_clear()
top.clipboard_append(a)
top.update()
# the label for user_name
Service = Label(top, text = "Service").place(x = 40, y = 60)
user_name = Label(top, text = "Username").place(x = 40, y = 100)
submit_button = Button(top, text = "Submit", command=ifSubmit).place(x = 40, y = 130)
service_var = StringVar()
Service_input_area = Entry(top, width = 30, textvariable=service_var)
Service_input_area.place(x = 110, y = 60)
service = service_var.get()
username_var = StringVar()
user_name_input_area = Entry(top, width = 30, textvariable=username_var)
username = username_var.get()
user_name_input_area.place(x = 110, y = 100)
top.mainloop()
有几件事很突出:
假设您正在创建标签小部件:
label = Label(top, text="This is a label").place(x=40, y=40)
这是不正确的。 label
将绑定到 place
方法返回的任何内容,因为您链接了 Label
实例化和 place
方法。 place
方法总是 returns None
,因此 label
将是 None
。您需要将其拆分为两个单独的语句 - 一个用于实例化和绑定小部件,另一个用于放置它:
label = Label(top, text="This is a label")
label.place(x=40, y=40)
您需要为脚本中的每个小部件执行此操作,无论它是标签、按钮等。
另外,在一些地方,你会做这样的事情:
var = StringVar()
entry = Entry(..., textvariable=var)
user_input = var.get()
这也是不正确的。您创建一个条目小部件,然后立即使用 var.get
从中读取。由于您刚刚创建了小部件,其内容将为空,因此在本例中 user_input
将为空。您也永远不会更改 user_input
,或稍后再次从条目小部件中读取,因此您最终写入文件的内容将只是一个空字符串。一旦用户真正有机会写东西,您就需要调用 var.get
。按钮回调将是执行此操作的好地方。
我还看到你在 isSubmit()
功能下做错了一些事情。在这里,您创建了 2 个标签和一个按钮,只要按下提交按钮就会创建该按钮。您要么需要销毁以前的标签和按钮,要么只创建一次标签和按钮,然后使用 place()
和 place_forget()
来显示和隐藏它们。此外,尽量避免使用 python 内置 class 和函数名称来命名变量。
这是您更正后的代码:
import string
import random
from tkinter import *
mypasslist = []
with open(r"file.txt", "r") as inp:
for line in inp:
items = line.split()
mypasslist.append([item for item in items[0:]])
def generatePassword():
characters = string.ascii_letters + string.punctuation + string.digits
password = "".join(random.choice(characters) for x in range(random.randint(8, 16)))
return password
def writeToFile(input_variable):
with open(r"file.txt", "a") as inp:
inp.writelines(input_variable)
top = Tk()
top.geometry("1920x1080")
def ifSubmit():
passwd = generatePassword()
label1.config(text='Your password is: %s' % passwd)
label1.place(x = 40, y = 170)
label2.config(text='Your password and username has been saved, you can access them below.')
label2.place(x = 40, y = 227)
copy_button = Button(top, text = 'Copy', command=copyText).place(x=40, y=195)
writeToFile(service_var.get()+', ')
writeToFile(username_var.get()+', ')
writeToFile(passwd+'\n')
def copyText():
top.clipboard_clear()
top.clipboard_append(passwd)
top.update()
# the label for user_name
Label(top, text = "Service").place(x = 40, y = 60)
Label(top, text = "Username").place(x = 40, y = 100)
submit_button = Button(top, text = "Submit", command=ifSubmit).place(x = 40, y = 130)
service_var = StringVar()
Service_input_area = Entry(top, width = 30, textvariable=service_var)
Service_input_area.place(x = 110, y = 60)
username_var = StringVar()
user_name_input_area = Entry(top, width = 30, textvariable=username_var)
user_name_input_area.place(x = 110, y = 100)
# label 1
label1 = Label(top, font=("Arial", 11))
label2 = Label(top, justify='left')
copy_button = Button(top, text = 'Copy', command=copyText)
top.mainloop()