Message box close immediately

I am using a script:

#!/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()

It works well on Linux (Kali Linux), but when I use it on Windows (after creating the .exe file via py2exe), a message box appears and then disappears right away without waiting for OK. How to make her wait for the pressed button?

+4
source share
2 answers

Use is tkMessageBoxalmost identical to use os.systemand zenityto display a warning window.

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

To indicate that the tk window does not appear after compilation using py2exe, you will need to include it "dll_excludes": ["tcl85.dll", "tk85.dll"]inside your optionssetup, which excludes two DLLs that cause errors.

# 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
+4

, , tkinter. :

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

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

os.system...


tkinter

+1

Source: https://habr.com/ru/post/1672689/


All Articles