为什么我在单击“复制到剪贴板”按钮时从stringvar得到类似元组的值?我需要文本值

jvidinwx  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(284)

为什么我从 StringVar() ? 当我点击“复制”按钮时,我从我的项目中得到了这样的结果 ('n', 'o', 'i', 'Q', 'X', '2', 'd') . 我需要一个文本值从复制按钮。一根没有 '' .

from tkinter import *
from tkinter import ttk
import random

window = Tk()
window.resizable(False , False)
window.title('Password Generator')
window.geometry('400x200')

length = IntVar()
lbl = StringVar()
var1 = StringVar()

Alphabet = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'

showpass = ttk.Label(window , font = ('arial' ,15) , textvariable = lbl).pack()

def randalp():
        string = list(Alphabet)
        random.shuffle(string)
        return string[:length.get()]

ttk.Label(window , text = 'Password Lentgh:').pack()
numchoosen = ttk.Combobox(window, width = 12 , textvariable = length)
numchoosen['values'] = (5,6,7,8,9,10)
numchoosen.pack()
numchoosen.current(2)
numchoosen.config(state = 'readonly')

rad1 = ttk.Checkbutton(window , text = 'Alphabet' , variable = var1).pack()

def btnclick():
        get1 = var1.get()
        if get1 == '1':
                lbl.set(randalp())
def copybtn():
        window.clipboard_clear()
        window.clipboard_append(lbl.get())

ttk.Button(window , text = 'Generate' , command = btnclick).pack()
ttk.Button(window , text = 'Copy' , command = copybtn).pack()

window.mainloop()
cidc1ykv

cidc1ykv1#

你的职能 randalp() 返回列表而不是字符串。
按如下所示更改函数以获取字符串而不是列表:

def randalp():
    string = list(Alphabet) # string is a list
    random.shuffle(string)
    return ''.join(string[:length.get()]) # return a string

相关问题