Showing posts with label Windows 8. Show all posts
Showing posts with label Windows 8. Show all posts

Friday, July 13, 2012

Python: Global Variable Access

Current post is on how to access the global variables in Python 3.2. 


Global variables can be easily accessed by using the keyword global.

The global statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals. It would be impossible to assign to a global variable without global, although free variables may refer to globals without being declared global.

Names listed in a global statement must not be used in the same code block textually preceding that global statement.

Names listed in a global statement must not be defined as formal parameters or in a for loop control target, class definition, function definition, or import statement.

Example 1: Change the global variable
globalVariable = False

def ChangeGlobalFunction():
      """Function that changes global variable"""
      global  globalVariable  #Global statement
      globalVariable = True

def PrintGlobalFunction():
      """Function that prints global variable"""
      print(globalVariable)

PrintGlobalFunction()
ChangeGlobalFunction()
PrintGlobalFunction()
Output:
False
True

Python: Check for file existence

Current post is on how to check whether file exists on system running Python 3.2.


First import the 'os.path' module.  os.path module implements some useful functions on pathname.

import os.path

Add the following line to check file.
  
os.path.exists(filename)

It returns True if filename exists else returns False


To make sure it is file and it exists, one can use:

os.path.isfile(filename)

It returns True only if the type is of file and it exists else returns False


Example 1: File Check using 'exists' function

      if os.path.exists("HelloWorld.py"):
           print("Hello World")
      else:
           print("No World !!!")

Example 2: File Check using 'isfile' function

      if os.path.isfile("HelloWorld.py"):
           print("Hello World")
      else:
           print("No World !!!")


Python: Message Box in Windows

Current post is on how to show message box in Python 3.2 running on Microsoft Windows OS.


First import the 'ctypes' module. ctypes module provides C compatible data types and allows calling functions in DLLs or shared libraries.

import ctypes

Next we need is the messagebox function pointer

messageBox = ctypes.windll.user32.MessageBoxW  *

After having the function pointer, we just need to call the function with required arguments.

returnValue = messageBox(ParentHandle,Error Text,Title Text,ButtonEnumValue)

where,
           ParentHandle = Handle to parent window
           Error Text = Text one wants to show to end user
           Title Text = Text that will appear on Title bar of message box
           ButtonEnumValue = Which button one needs to show

Detailed information can be found on Microsoft MessageBox Documentation

Based on return value, one can take the required action.

Example 1: Message Box with OK button.

      import ctypes

      messageBox = ctypes.windll.user32.MessageBoxW

      returnValue = messageBox(None,"Message Box!!!","Hello",0x40 | 0x0)


 
Example 2: Message Box with OK-Cancel button & ReturnValue Check


      import ctypes

      messageBox = ctypes.windll.user32.MessageBoxW

      returnValue = messageBox(None,"Message Box!!!","Hello",0x40 | 0x1)

      if returnValue = = 1:
            print("Pressed OK")
     elif returnValue = = 2:
           print("Pressed Cancel")


* Python 3.x takes unicode only.


Disclaimer:

The above post and all the posts in the blog are derived from facts, information, logical interpretation and logical conclusion of printed and internet materials available to me, perceived and produced by 99 gm brain of mine, which by no means always be accurate, consistent and complete.

All the posts are for personal quick reference only.

If any suggestion, correction, misinterpretation, misconception commented, which will be moderated and deleted if required, to avoid unforeseen issues.

If any trademark / copywrite issue is present, do send in a mail and appropriate tag (logo, name, website link) will be attached to the same.

Additional disclaimer will be attached wherever required in the post.