[英]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.