[python] Python에서 간단한 메시지 상자를 어떻게 만들 수 있습니까?

alert()JavaScript에서 와 동일한 효과를 찾고 있습니다.

오늘 오후에 Twisted.web을 사용하여 간단한 웹 기반 인터프리터를 작성했습니다. 기본적으로 양식을 통해 Python 코드 블록을 제출하면 클라이언트가 가져 와서 실행합니다. 매번 상용구 wxPython 또는 TkInter 코드 전체를 다시 작성할 필요없이 간단한 팝업 메시지를 만들 수 있기를 원합니다 (코드가 양식을 통해 제출 된 다음 사라집니다).

tkMessageBox를 시도했습니다.

import tkMessageBox
tkMessageBox.showinfo(title="Greetings", message="Hello World!")

그러나 이것은 tk 아이콘이있는 백그라운드에서 다른 창을 엽니 다. 나는 이것을 원하지 않는다. 간단한 wxPython 코드를 찾고 있었지만 항상 클래스를 설정하고 앱 루프 등을 입력해야했습니다. Python에서 메시지 상자를 만드는 간단하고 캐치없는 방법이 없습니까?



답변

다음과 같이 가져 오기 및 한 줄 코드를 사용할 수 있습니다.

import ctypes  # An included library with Python install.   
ctypes.windll.user32.MessageBoxW(0, "Your text", "Your title", 1)

또는 다음과 같이 함수 (Mbox)를 정의하십시오.

import ctypes  # An included library with Python install.
def Mbox(title, text, style):
    return ctypes.windll.user32.MessageBoxW(0, text, title, style)
Mbox('Your title', 'Your text', 1)

스타일은 다음과 같습니다.

##  Styles:
##  0 : OK
##  1 : OK | Cancel
##  2 : Abort | Retry | Ignore
##  3 : Yes | No | Cancel
##  4 : Yes | No
##  5 : Retry | No 
##  6 : Cancel | Try Again | Continue

즐기세요!

참고 : MessageBoxW대신 사용하도록 편집MessageBoxA


답변

당신이 봤어 에는 EasyGUI ?

import easygui

easygui.msgbox("This is a message!", title="simple gui")


답변

또한 취소하기 전에 다른 창을 배치하여 메시지를 배치 할 수 있습니다.

#!/usr/bin/env python

from Tkinter import *
import tkMessageBox

window = Tk()
window.wm_withdraw()

#message at x:200,y:200
window.geometry("1x1+200+200")#remember its .geometry("WidthxHeight(+or-)X(+or-)Y")
tkMessageBox.showerror(title="error",message="Error Message",parent=window)

#centre screen message
window.geometry("1x1+"+str(window.winfo_screenwidth()/2)+"+"+str(window.winfo_screenheight()/2))
tkMessageBox.showinfo(title="Greetings", message="Hello World!")


답변

제시 한 코드는 괜찮습니다! 다음 코드를 사용하여 “백그라운드의 다른 창”을 명시 적으로 만들고 숨기면됩니다.

import Tkinter
window = Tkinter.Tk()
window.wm_withdraw()

메시지 함 바로 앞.


답변

PyMsgBox 모듈은 정확히 이것을합니다. JavaScript의 이름 지정 규칙을 따르는 메시지 상자 함수 : alert (), confirm (), prompt () 및 password () (prompt ()이지만 입력 할 때 *를 사용함). 이러한 함수 호출은 사용자가 확인 / 취소 버튼을 클릭 할 때까지 차단됩니다. 종속성이없는 크로스 플랫폼, 순수 Python 모듈입니다.

다음으로 설치 : pip install PyMsgBox

샘플 사용법 :

import pymsgbox
pymsgbox.alert('This is an alert!', 'Title')
response = pymsgbox.prompt('What is your name?')

http://pymsgbox.readthedocs.org/en/latest/의 전체 문서


답변

Windows에서는 user32 라이브러리와 함께 ctypes를 사용할 수 있습니다 .

from ctypes import c_int, WINFUNCTYPE, windll
from ctypes.wintypes import HWND, LPCSTR, UINT
prototype = WINFUNCTYPE(c_int, HWND, LPCSTR, LPCSTR, UINT)
paramflags = (1, "hwnd", 0), (1, "text", "Hi"), (1, "caption", None), (1, "flags", 0)
MessageBox = prototype(("MessageBoxA", windll.user32), paramflags)

MessageBox()
MessageBox(text="Spam, spam, spam")
MessageBox(flags=2, text="foo bar")


답변

Mac에서 python 표준 라이브러리에는 EasyDialogs. http://www.averdevelopment.com/python/EasyDialogs.html에 (ctypes 기반) Windows 버전도 있습니다 .

중요한 경우 : 기본 대화 상자를 사용하고 이미 언급 한 것과 같이 Tkinter에 의존하지 않지만 easygui기능이 많지 않을 수 있습니다.