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
生成文本時,文本顯示 <function s at 0x000001B240AB3E2> 如何在 tkinter python 中正確顯示生成的內容?
我希望比例控制生成文本的長度,但它顯示 <function s at 0x000001B240AB3E20> 而不是 dljvdlk2okd0d
uj5u.com熱心網友回復:
您的代碼中有幾個問題:
- 最好使用
IntVar
而不是DoubleVar
forv
- 需要從回傳結果
s()
- 使用
s()
而不是s
在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
...
其實你根本不需要s()
:
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
uj5u.com熱心網友回復:
你應該這樣做display.insert('1.0', s())
而不是僅僅s
(因為最初你只是傳遞一個對 s 的參考而不是它回傳的任何字串。
您還應該以s()
like的形式回傳結果,return secrets.token_hex()
而不是僅僅將它們放在 parathesis 中。Python 沒有隱式回傳。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/505398.html
標籤:Python python-3.x tkinter tkinter 按钮 tkinter-text
上一篇:從訊息框Tkinter呼叫函式