如何禁用在python web浏览器的消息? [英] How can I disable the webbrowser message in python?

查看:197
本文介绍了如何禁用在python web浏览器的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Python程序,当我发送的用户通过使用WebBrowser模块创建Gmail帐户,蟒蛇显示:

In my python program, when I send the user to create a gmail account by use of the webbrowser module, python displays:

请输入您的Gmail用户名:现有的浏览器会话创建新窗口

"Please enter your Gmail username: Created new window in existing browser session."

有什么方法来摆脱创造了现有的浏览器会话新窗口,因为它占用的空间,用户键入自己的Gmail帐户。

Is there any way to get rid of "created new window in existing browser session", as it takes up the space where the user types in their Gmail account.

在code这个是:

webbrowser.open('https://www.google.com/accounts/NewAccount?service=mail')  
gmail_user = raw_input('Please enter your Gmail username: ')

编辑:的尝试既亚历马尔泰利的建议之后,code是:的http://引擎收录。 COM / 3uu9QS4A

After trying out both of Alex Martelli's suggestions, the code is: http://pastebin.com/3uu9QS4A

编辑2:我刚刚决定告诉用户转到Gmail的注册页面,而不是实际发送到那里,因为这是更简单的做,结果中没有(当前unsolvable-通过-ME)的错误。

EDIT 2: I have decided just to tell users to go to the gmail registration page instead of actually sending them there, as that is much simpler to do and results in no (currently-unsolvable-by-me) errors.

推荐答案

由于美国洛特在评论暗示,你应该做的的raw_input 第一;然而,这,本身不SUP preSS消息从 web浏览器,因为你问 - 它只是推迟它

As S.Lott hints in a comment, you should probably do the raw_input first; however, that, per se, doesn't suppress the message from webbrowser, as you ask -- it just postpones it.

要真正的燮preSS 的消息,可以暂时重定向标准输出或标准错误 - 无论这两个你所选择的浏览器的使用发出该消息。这也可能是没有用的,它们在Python的水平重定向(通过为sys.stderr sys.stderr来),因为你的浏览器是将要直接做它的输出;相反,你可以在操作系统级别做到这一点,例如,标准输出:

To actually suppress the message, you can temporarily redirect standard-output or standard-error -- whichever of the two your chosen browser uses to emit that message. It's probably no use to redirect them at Python level (via sys.stdout or sys.stderr), since your browser is going to be doing its output directly; rather, you can do it at the operating-system level, e.g., for standard output:

import os
gmail_user = raw_input('Please enter your Gmail username: ')
savout = os.dup(1)
os.close(1)
os.open(os.devnull, os.O_RDWR)
try:
   webbrowser.open(whatever)
finally:
   os.dup2(savout, 1)

(标准错误而不是标准输出,使用2个而不是1)。这是pretty低级编程,但由于web浏览器模块不给你挂钩,以控制其浏览器获得开辟了道路,这是pretty多只能选择(或多或少)保证该消息的SUP pression。

(for standard error instead of standard output, use 2 instead of 1). This is pretty low-level programming, but since the webbrowser module does not give you "hooks" to control the way in which the browser gets opened, it's pretty much the only choice to (more or less) ensure suppression of that message.

这篇关于如何禁用在python web浏览器的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆