-1

Why I get tuple value from StringVar()? When I click on Copy button, I get like this from my project ('n', 'o', 'i', 'Q', 'X', '2', 'd'). I need a text value from Copy button . a string without '' .

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()

  • 2
    please, provide [mre]. Where do you get this `(''n', 'o', 'i', 'Q', 'X', '2', 'd')`? What was the input? – buran Apr 27 '21 at 15:26
  • 1
    Your code doesn't do what you say it does. For one thing, `lbl` is an empty string so `lbl.get()` will always return an empty string rather than a non-empty string or a tuple. – Bryan Oakley Apr 27 '21 at 15:27
  • 1
    Show us how you get the result you posted because I get a string instead of a tuple. – acw1668 Apr 27 '21 at 15:27
  • Also you know that the variable `showpass` is always `None`. For more info read [this](https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name) – TheLizzard Apr 27 '21 at 15:34
  • Question Updated –  Apr 27 '21 at 16:07

1 Answers1

1

Your function randalp() returns a list instead of a string.

Change the function as below to get a string instead of list:

def randalp():
    string = list(Alphabet) # string is a list
    random.shuffle(string)
    return ''.join(string[:length.get()]) # return a string
acw1668
  • 40,144
  • 5
  • 22
  • 34