Home > Net >  when generating text the text displays <function s at 0x000001B240AB3E2> how can I display the
when generating text the text displays <function s at 0x000001B240AB3E2> how can I display the

Time:08-31

from ctypes.wintypes import SIZE
from tkinter import * # import tkinter
import secrets

window = Tk() # creates window

window.title('PassGen') # changes title
window.geometry('600x400') # sets size
window.resizable(False,False) # prevents user from changing size

title = Label(window,text = 'PassGen', font=('Times',20)) # creates title
title.place(x=260,y=75) # displays title at desird location

v = DoubleVar()
scale = Scale(window, variable = v, from_ = 8, to = 16, orient = HORIZONTAL)
scale.pack(anchor=CENTER)
scale.place(x=235,y=170)

def s():
    if scale == 8:
        (secrets.token_hex(8))
    elif scale == 9:
        (secrets.token_hex(9))
    elif scale == 10:
        (secrets.token_hex(10))
    elif scale == 11:
        (secrets.token_hex(11))
    elif scale == 12:
        (secrets.token_hex(12))
    elif scale == 13:
        (secrets.token_hex(13))
    elif scale == 14:
        (secrets.token_hex(14))
    elif scale == 15:
        (secrets.token_hex(15))
    elif scale == 16:
        (secrets.token_hex(16))


def password():
    display = Text(window,height=1,width=32) # displays text
    display.insert('1.0', s) # inserts the pasword generator into the text
    display.place(x=80,y=153) # displays the ext in a specific place
    display.config(state='disabled') # prevents user from editing password


button = Button(window, text = 'Generate Password', command = password).place(x=350,y=150) # button for user to click and generate password.

window.mainloop()# displays window infinitely

when generating text the text displays <function s at 0x000001B240AB3E2> how can I display the generated thing properly in tkinter python?

I want the scale to control the length of the generated text but it displays <function s at 0x000001B240AB3E20> instead of something like dljvdlk2okd0d

CodePudding user response:

There are few issues in your code:

  • it is better to use IntVar instead of DoubleVar for v
  • need to return result from s()
  • use s() instead of s in display.insert('1.0', s)
...
v = IntVar()  # use IntVar instead of DoubleVar
scale = Scale(window, variable = v, from_ = 8, to = 16, orient = HORIZONTAL)
...
def s():
    return secrets.token_hex(v.get())

def password():
    display = Text(window,height=1,width=32) # displays text
    ### use s() instead of s in below line
    display.insert('1.0', s()) # inserts the pasword generator into the text
    display.place(x=80,y=153) # displays the ext in a specific place
    display.config(state='disabled') # prevents user from editing password
...

Actually you don't need s() at all:

def password():
    display = Text(window,height=1,width=32) # displays text
    # call secrets.token_hex(v.get()) directly instead of s()
    display.insert('1.0', secrets.token_hex(v.get()))
    display.place(x=80,y=153) # displays the ext in a specific place
    display.config(state='disabled') # prevents user from editing password

CodePudding user response:

You should do display.insert('1.0', s()) instead of just s (because originally you just pass a reference to s instead of whatever string it returns.

You should also return your results in s() like return secrets.token_hex() instead of just putting them in parathesis. Python doesn't have implicit return.

  • Related