Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从tkinter中的文本输入中获取变量?_Python_Variables_Tkinter_Text_File Writing - Fatal编程技术网

Python 如何从tkinter中的文本输入中获取变量?

Python 如何从tkinter中的文本输入中获取变量?,python,variables,tkinter,text,file-writing,Python,Variables,Tkinter,Text,File Writing,我尝试通过以下操作从文本输入中获取变量: 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

我尝试通过以下操作从文本输入中获取变量:

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
方法总是返回
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\u input
将为空。您也不会更改
用户输入
,也不会在以后再次读取条目小部件,因此您最终写入文件的内容将只是一个空字符串。一旦用户确实有机会写东西,您就需要调用
var.get
。按钮回调是一个很好的方法。

答案已经指出了大部分错误

isSubmit()
函数下,我还看到了一些您正在做的错误。在这里,您将创建2个标签和一个按钮,每当按下submit按钮时都会创建该按钮。您需要销毁以前的标签和按钮,或者只创建一次标签和按钮,然后使用
place()
place\u-forget()
来显示和隐藏它们。另外,尽量避免使用python内置的类和函数名来命名变量

这是您更正的代码:

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


不确定这是否是您描述的问题的直接原因,但
user\u name\u input\u area
将与您想象的不同。您正在将它绑定到某个匿名
tk.Entry
对象的
place
方法返回的结果。您需要将
user\u name\u input\u区域
绑定到一条语句中的
tk.Entry
实例,然后在另一条语句中,将小部件.As@PaulM。所说的
username\u var
将始终是
None
。这个人也有同样的问题,我建议你发布所有的代码。如果看不到更多的代码,很难判断发生了什么。比如:
username\u var=StringVar()user\u name\u input\u area=Entry(top,width=30,textvariable=username\u var)username=username\u var.get()user\u name\u input\u area.place(x=110,y=100)
这能解决我的问题吗?如果你调用
username\u var.get(),你也知道吗
在创建
用户名\u输入\u区域后,它将立即返回
,因为用户没有足够的时间在那里实际写入任何内容。按下按钮时,我调用了var.get,它工作了,谢谢!