実現したいこと
プログラミング勉強中の者です。
pythonで文字数をInputTextに入力するとランダムの文字でパスワードを作成しOutpuTextに出力するプログラムを作りたいのですが、以下のようなエラーが出て消えず困っています。
整数以外を入力するとエラーのポップアップが出るようにしたいです。
ご教授の程よろしくお願い致します。
発生している問題・エラーメッセージ
実行すると以下のポップアップが表示されます。
Error Unable to complete operation on element with key 0
You cannot perform operations (such as calling update) on an Element until:
window.read() is called on finalize=True when Window created.Addng a ‘’finalize = True” parameter to your Window creation will likely fix this.
The error originated from:
File C:\Users\xxxxxxx\anaconda3\lib\warning.py
Line 30
In_showwarnimsg_impl_
File.write(text)
該当のソースコード
import PySimpleGUI as sg
import secrets
import string
sg.theme('DarkAmber')
layout = [
[sg.Text('桁数設定', size=(5, 1)), sg.InputText('', size=(8, 1), key='page1'), sg.Text('※設定するパスワードの桁数を入力してください', size=(20, 3))],
[sg.Output(size=(70,3))]
]
window = sg.Window('パスワードを作成します', layout)
chars = string.ascii_letters + string.digits
input_num = input('')
if not input_num.isdigit():
sg.PopupGetText('エラーです', title='Error')
password = "".join([secrets.choice(chars) for i in range(int(input_num))])
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
window.close()
他に試したコード
①Inputtextに入力しそのままポップアップで入力したくて書いたコード
import PySimpleGUI as sg
import secrets
import string
sg.theme('DarkAmber')
layout = [[sg.Text('桁数設定', size=(5,1)), sg.InputText('', size=(8, 1), key='page1'),
sg.Text('※設定するパスワードの桁数を入力してください', size=(20, 3))],[sg.Button('入力',key='bt1')]]
window = sg.Window('パスワードを作成します', layout)
chars = string.ascii_letters + string.digits
input_num = input('')
password = "".join([secrets.choice(chars) for i in range(int(input_num))])
while True:
event , values = window.read()
if event == None:
break
elif event == 'bt1':
r = sg.PopupError('数字を入力してください',title='AAA')
if not r:
input_num.isdigit()
else:sg.popup(print(password))
window.close()
→この場合は終了コードが出ない。
補足情報
windows10,開発環境はPyCharm Community Edition 2022.3.1
その他追記
can110様
該当コード内の
if not input_num.isdigit():
sg.PopupGetText('エラーです', title='Error')
をイベントループ内に移動し、
while True:内のbreakより下を以下のように追記しました。
elif event =='page1':
if input_num.isdigit():
print(password)
else:sg.PopupGetText('エラーです', title='Error')
→同じエラーでうまくいきませんでした。
回答1件
あなたの回答
tips
プレビュー