5

I use 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 good on Linux(Kali Linux) but when I use this on Windows (after creating .exe file by py2exe) message box pop up and then immediately disappear without waiting for clicking 'OK'. How can I force it to wait on clicked button?

Michu93
  • 5,058
  • 7
  • 47
  • 80
  • Just a guess, but I think the script exits and so do the message box, child of the main process. – Pedro Lobito Mar 19 '17 at 21:00
  • @PedroLobito ok, so I can make a fake waiting(for example 10 sec) after pop up message but it's not what I am looking for. There must be a solution to close program after clicking 'OK' on Windows – Michu93 Mar 19 '17 at 21:04
  • You may want to use tkinter and add a watcher to the box buttons. – Pedro Lobito Mar 19 '17 at 23:19

2 Answers2

5

Using tkMessageBox is almost identical to using os.system and zenity to show warning message box.

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 address tk window not showing up after compiling using py2exe, you will need to include "dll_excludes": ["tcl85.dll", "tk85.dll"] inside your options when setting up, which exclude the two dlls that causes 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
Taku
  • 31,927
  • 11
  • 74
  • 85
  • My 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, ) After adding dll_excludes it says that: AttributeError: 'list' object has no attribute 'items' – Michu93 Mar 22 '17 at 18:31
  • opps, I miss typed it, try it now – Taku Mar 22 '17 at 18:37
  • 1
    options = {'py2exe': {'bundle_files': 1, 'compressed': True, 'dll_excludes': ['tcl85.dll', 'tk85.dll']}} – Taku Mar 22 '17 at 18:37
  • Well, that's really close but there is an error after launching .exe file: ImportError: Traceback (most recent call last): ImportError: MemoryLoadLibrary failed loading _tkinter.pyd – Michu93 Mar 22 '17 at 18:41
  • then you should try `import _tkinter` and type `_tkinter.__file__` in the console. and change the options to: options = {'py2exe': {'bundle_files': 1, 'compressed': True, 'dll_excludes': ['tcl85.dll', 'tk85.dll'], 'includes':[*PATH*]}} replace *PATH* with the filepath that `_tkinter.__file__` returned – Taku Mar 22 '17 at 18:56
  • Sorry, I don't get how can I get path to _tkinter.__file__ PS. got it, gonna try to change setup.py now – Michu93 Mar 29 '17 at 08:08
  • ImportError: No module named C:\Python27\DLLs\_tkinter It seems like even if it does work then it's only on my PC with my path to this module. Am I wrong? I need something which will work on every machine with Windows – Michu93 Mar 29 '17 at 08:16
  • No, if you do it correctly, it will work on any windows pc – Taku Mar 30 '17 at 00:02
1

Following the comments I think you need to generate the dialog box via tkinter. Here's an example:

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

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

Change os.system... for the code above


You may want to check more tkinter dialog examples

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268