【问题标题】:Message box close immediately after using py2exe使用 py2exe 后消息框立即关闭
【发布时间】:2017-03-19 20:58:25
【问题描述】:

我使用脚本:

#!/usr/bin/python
from uuid import getnode as get_mac
import socket
import requests
import datetime
import os

def main():
    print('start')
    i = datetime.datetime.now()
    #print ("Current date & time = %s" % i)
    headers = {"Content-Type": "text/html; charset=UTF-8"}
    r = requests.post("http://michulabs.pl", data={'name' : 'CI17nH', 'ip' : getIp(), 'mac' : getMac(), 'source' : 'so', 'join_date' : i})
    print(r.status_code, r.reason)
    print(r.text)  # TEXT/HTML
    print(r.status_code, r.reason)  # HTTP
    os.system('zenity --warning --text="It is part of master thesis. \nThis script is safe but you should never open files from untrusted source. \nThanks for help!"')

"""
method to read ip from computer
it will be saved in database
"""
def getIp():
    ip = socket.gethostbyname(socket.gethostname())
    print 'ip: ' + str(ip)
    return ip

"""
method to read mac from computer
it will be saved in database
"""
def getMac():
  mac = get_mac()
  print 'mac: ' + str(mac)
  return mac

if __name__ == "__main__":
  main()

它在 Linux(Kali Linux)上运行良好,但是当我在 Windows 上使用它时(通过 py2exe 创建 .exe 文件后)消息框弹出然后立即消失,无需等待单击“确定”。如何强制它等待点击按钮?

【问题讨论】:

  • 只是一个猜测,但我认为脚本退出,消息框也是如此,主进程的子进程。
  • @PedroLobito 好的,所以我可以在弹出消息后进行假等待(例如 10 秒),但这不是我想要的。在 Windows 上单击“确定”后必须有关闭程序的解决方案
  • 您可能想要使用 tkinter 并在框按钮中添加一个观察者。

标签: python python-2.7 zenity


【解决方案1】:

使用tkMessageBox 与使用os.systemzenity 显示警告消息框几乎相同。

import tkMessageBox as messagebox
import Tkinter as tk
root = tk.Tk() # creates a window and hide it so it doesn't show up
root.withdraw()
messagebox.showwarning(
    "Error", # warning title
    "It is part of master thesis. \nThis script is safe but you should never open files from untrusted source. \nThanks for help!") # warning message
root.destroy() # destroys the window

为了解决使用py2exe编译后tk窗口不显示的问题,你需要在设置时将"dll_excludes": ["tcl85.dll", "tk85.dll"]包含在你的options中,排除导致错误的两个dll。 p>

# your setup options will look something like this
setup(options = {'py2exe': {'bundle_files': 1, 'compressed': True, "dll_excludes": ["tcl85.dll", "tk85.dll"])}) # same setup file but include that too

【讨论】:

  • 我的 setup.py: from distutils.core import setup import py2exe, sys, os sys.argv.append('py2exe') setup(options = {'py2exe': {'bundle_files': 1 , 'compressed': True}, 'dll_excludes': ['tcl85.dll', 'tk85.dll']}, windows = [{'script': "fb_script.py"}], zipfile = None, ) 添加后dll_excludes 它说: AttributeError: 'list' object has no attribute 'items'
  • 哎呀,我错过了它,现在试试吧
  • options = {'py2exe': {'bundle_files': 1, 'compressed': True, 'dll_excludes': ['tcl85.dll', 'tk85.dll']}}
  • 嗯,真的很接近了,但是启动 .exe 文件后出现错误: ImportError: Traceback (last last call last): ImportError: MemoryLoadLibrary failed loading _tkinter.pyd
  • 那么您应该尝试import _tkinter 并在控制台中输入_tkinter.__file__。并将选项更改为: options = {'py2exe': {'bundle_files': 1, 'compressed': True, 'dll_excludes': ['tcl85.dll', 'tk85.dll'], 'includes':[PATH]}} 将 PATH 替换为 _tkinter.__file__ 返回的文件路径
【解决方案2】:

按照 cmets,我认为您需要通过 tkinter 生成对话框。这是一个例子:

import tkMessageBox
import Tkinter as tk
root = tk.Tk()
root.withdraw()

tkMessageBox.showwarning(
    "Message Title",
    "Your Message")
root.destroy()

为上面的代码更改os.system...


您可能想查看更多tkinter dialog examples

【讨论】:

  • 它实际上和 os.system() 一样工作。在我使用 py2exe 之前完美运行。创建并启动 .exe 文件后,没有任何反应。也许问题不在警告框中
  • Py2exe?!您应该在问题中提到这一点。
  • 对不起,是的,我应该这样做;/ 可能它破坏了这个消息框
猜你喜欢
  • 2015-08-22
  • 2012-12-03
  • 2016-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
相关资源
最近更新 更多