微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何从 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')

在这种情况下,我确实在我的文本文件中打招呼,所以我怀疑是问题的变量而不是 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,textvariable=service_var)
Service_input_area.place(x = 110,y = 60)
service = service_var.get()

username_var = StringVar()
user_name_input_area = Entry(top,textvariable=username_var)
username = username_var.get()
user_name_input_area.place(x = 110,y = 100)

top.mainloop() 

解决方法

有几点很突出:

假设您正在创建一个标签小部件:

async-storage

这是不正确的。 async-storage 将绑定到 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_input。按钮回调将是执行此操作的好地方。

,

here 的回答已经指出了大部分错误。

我还发现您在 isSubmit() 函数下还做错了一些事情。在这里,您将创建 2 个标签和一个按钮,每当按下提交按钮时都会创建该按钮。您要么需要销毁之前的标签和按钮,要么只创建一次标签和按钮,然后使用 place()place_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,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,command=copyText)


top.mainloop() 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。