python - 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.
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. 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
解决方案
这段代码有很多问题,所以我已经解决了,你完成的代码在这里:
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()
所以看看你的问题
entrypassw = Entry(parent, textvariable = gen, state = DISABLED)
您需要使用 genpassw 的条目小部件中的“父”是什么,因为那是您的父小部件。
type = full_char_table
你曾经使用'type'作为变量,'type'是python的内置函数,所以你不能使用它。
x += 1
无需在 for 循环中使用增量>> x += 1
entrypassw = Entry(parent, textvariable = gen, state = DISABLED)
entrypassw.pack()
return gen
您正在返回 gen.Before 您将它们描述为小部件的文本变量,现在您正在返回它。它已被弃用。
您必须使用 mainloop 使您的 GUI 运行。
genpassw.mainloop()
推荐阅读
- visual-studio-code - 是否可以在没有 shell 的情况下在 Visual Studio Code 中运行代码?
- laravel - laravel 模型中的 $this->Products 和 $this->Products() 有什么区别?
- flutter - 如何在setter中添加item并在Flutter中与provider一一通知?
- c# - “IPermissionStateManager”的命名空间
- azure-cosmosdb - 如何解释 CosmosDB Mongo API 中 ChangeStream 的恢复令牌?我们可以仅使用恢复令牌(或 changeStreamDocuments)来订购操作吗?
- php - 文件视图到另一个选项卡(PHP)
- kubernetes - Oauth2-proxy 和 Traefik:重定向太多
- scala - spark cogroup/join KeyValueGroupedDataset 与数据集
- python - 无法使用 conda “CondaHTTPError: HTTP 000 CONNECTION FAILED for url”安装软件包
- java - Spring Pageable Sort 与 mongoOperations 一起工作不正确。排序顺序不正确