我正在为我们的 Perforce 服务器使用 P4Python 接口在 Python 3.8.7 中编写脚本。在某些情况下,我不想中止提交更改,但我需要通知用户一些信息。为此,我想使用 tkinter 库中的消息框。如果脚本由 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