消息框立即关闭

时间:2017-03-19 20:58:26

标签: python python-2.7 zenity

我使用脚本:

#!/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文件之后)弹出消息框然后立即消失而不等待单击“确定”。如何强制它等待点击按钮?

2 个答案:

答案 0 :(得分:5)

使用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错误。

# 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

答案 1 :(得分:1)

根据评论,我认为您需要通过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

相关问题