Perforce サーバーの P4Python インターフェイスを使用して、Python 3.8.7 でスクリプトを作成しています。変更の送信を中止したくない状況がいくつかありますが、いくつかの情報についてユーザーに通知する必要があります。そのために、tkinter ライブラリの messagebox を使用したいと考えました。スクリプトが perforce で実行された場合、メッセージ ボックスは表示されませんが、ターミナルで実行すると期待どおりに動作します。
この問題を解決する方法の手がかり。
メッセージボックスを表示したいスクリプトのコードスニペットを次に示します。
def setupConnection(cfg):
connectionError = False
exit = False
errorMsg = ""
while not exit:
try:
client = http.client.HTTPConnection(cfg.serverAddress)
client.connect()
except:
connectionError = True
errorMsg = "Address " + cfg.serverAddress + " is not available! Check connection or configuration."
#sys.exit("Address " + cfg.serverAddress + " is not available! Check connection or configuration.")
if not connectionError:
client.request(method=GET, url=cfg.apiAddress, headers=cfg.headers)
response = client.getresponse()
if response.status == pageNotFound404:
connectionError = True
errorMsg = "Address " + cfg.apiAddress + " is not available! Check connection or configuration."
#sys.exit("Address " + cfg.apiAddress + " is not available! Check connection or configuration.")
else:
cfg.apiGetResponse = (response.read()).decode()
exit = True
return client
if connectionError:
window = Tk()
window.eval('tk::PlaceWindow %s center' % window.winfo_toplevel())
window.withdraw()
if not messagebox.askretrycancel('Connection error', errorMsg, icon='error'):
exit = True
errorMsg = ('Please add changelist ' + cfg.changelist_ID + ' in ' + cfg.customFieldName + ' textbox of workpackage ' + cfg.workpackage_ID)
messagebox.showinfo('User action required', errorMsg)
window.destroy()
window.quit()
return None