简体   繁体   English

如何使用Tkinter从一个函数的值返回另一个函数?

[英]How to return a value from a function to another function with Tkinter?

This is my problem. 这是我的问题。 I can not return the value of the randomtext() function to the main function. 我无法将randomtext()函数的值返回给主函数。

I want the text to be selected to be copied and then pasted somewhere. 我希望选择要复制的文本,然后将其粘贴到某个地方。 Using a Label works, but when I use an Entry it does not work. 使用Label有效,但是当我使用Entry时无效。 What am I doing wrong? 我究竟做错了什么?

def psw_generator():
    global gen
    genpassw = Tk()
    genpassw.title("password generator")

    entrypassw = Entry(parent, textvariable = gen, state = DISABLED)
    entrypassw.pack()


def randomtext():

    x = 0
    psw = ""
    lenght = 16
    full_char_table = "abcdef.."
    type = full_char_table

    gen = StringVar(value = psw)

    for x in range(int(lenght)):
        psw += type[int(random.randrange(len(type)))]
        x += 1

    return gen

There are many problems in this code so I had solved them and your finished code is here: 这段代码中有很多问题,因此我已经解决了,您完成的代码在这里:

import random
from tkinter import *

global gen
genpassw = Tk()
gen = StringVar()
genpassw.title("password generator")

entrypassw = Entry(genpassw, textvariable = gen, state = DISABLED)
entrypassw.pack()
def randomtext():
    global gen
    x = 0
    psw = ""
    length = 16
    full_char_table = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"


    for x in range(int(length)):
        psw += full_char_table[random.randint(0,len(full_char_table)-1)]

    gen.set(psw)

randomtext()
genpassw.mainloop()

So have a look at your problems 所以看看你的问题

entrypassw = Entry(parent, textvariable = gen, state = DISABLED)

What is 'parent' in the entry widget you need to use genpassw because that is your parent widget. 您需要使用genpassw的条目小部件中的“父级”是什么,因为那是您的父级小部件。

type = full_char_table

You had used 'type' as a variable, 'type' is builtin function of python so you cannot use it. 您已经使用'type'作为变量,'type'是python的内置函数,因此您无法使用它。

x += 1

No need to use increment in for loop>> x += 1 无需在for循环中使用增量>> x + = 1

entrypassw = Entry(parent, textvariable = gen, state = DISABLED)
entrypassw.pack()

return gen

You are returning gen.Before you described them as text variable for widget, and now you are returning it. 您将返回gen。在将它们描述为小部件的文本变量之前,现在将其返回。 It is deprecated. 不推荐使用。

You must use mainloop to make your GUI run. 您必须使用mainloop来运行GUI。

genpassw.mainloop()

暂无
暂无

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

相关问题 如何从窗口小部件的函数返回值,并将其传递给Tkinter,Python中的另一个窗口小部件的函数 - How to return value from a widget`s function, and pass it to another widget's function in Tkinter, Python 如何通过 tkinter 输入框将多个值从一个函数返回到另一个函数? (ValueError:无法将字符串转换为浮点数:) - How to return multiple value from a function to another by tkinter entry box? (ValueError: could not convert string to float: ) 如何使用 tkinter 中的按钮将值从一个 function 返回到另一个 - How to return a value from one function to other using a button in tkinter 如何返回在 tkinter 中作为命令给出的函数的值 - How to return a value of a function given as a command in tkinter 如何从 tkinter 中的事件 function 返回列表? - How to return a list from an event function in tkinter? Tkinter:从一个 function 获取值并在另一个 function 中使用它 - Tkinter: Getting value from one function and using it in another function Tkinter:从函数返回数组 - Tkinter: Return array from a function 如何通过按下按钮从 tkinter 输入字段返回变量以在另一个 function 中使用? - How do I return a variable from a tkinter Entry field to use in another function by pressing a button? 如何从python中另一个脚本中的函数收集返回值? - How to gather return value from function in another script in python? 如何通过多重处理将布尔值从一个函数返回到另一个函数? - How to return a Boolean value from one function to another via multiprocessing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM