Page 1 of 1

Python EasyGUI basics

#1 Vblaster  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 16-January 11

Posted 18 August 2011 - 11:06 PM

EasyGUI, in Python


EasyGUI is a simple GUI Module, Using Function Calls.

To demonstrate EasyGUI's power and ease of use, I'll show you how to make a program that I made for my purposes a few days ago: Diary.


Diary is a basic log writer, writing user input to a user chosen file, with extra functions to dump the whole file to the screen, clear the file, or exit (can't make the close button work. Derp.). Diary for terminal (or command prompt), will be included at the end, for servers/and people who like terminal.

First, Download easygui from http://easygui.sourc...e.net/download/

Next, the file containing all the functions, named Vars.py

from easygui import * # from the module easygui import everything(*)



def Ask():
    fname = enterbox(msg='Filename: ', title='Filename', default='log.txt') #define the variable fname as the result of an enterbox, which takes user input. msg is displayed above the input area, title is the window title, and default is the default text in the box.

def Clear(): # create a function called Clear
    
    f = open(fname, "w"); # open the file defined in Ask, in write mode.
    f.write("") # write nothing to the file, this works, because write mode replaces everything (append mode writes to the end.)

def Dump(): # create a function called Dump
    f = open(fname, "r"); # open the file defined in Ask()
    msgbox(f.read()) # open a window, with the text of the file.

def Post(): # create a function called Post
    f = open(fname, "a"); # open the file defined in Ask.
    message = enterbox(msg='Enter Message: ', title='Post', default=''); # Define message as the result of another enterbox.
    f.write("\n"); # write a newline to the file.
    f.write(message) # write your message to the file.
    
def Die(): # Define the function Die.
    sys.exit() # Quit The Program.
    
Ask() Execute Ask, because when imported into the Main File, it runs automatically.




And finally, The Main File, Named Main.py :


from easygui import *
from Vars import * # Try this one yourself.

msgbox("Hi") # Make a windows displaying Hi.
msgbox("Welcome To Diary") # Make a window Displaying Welcome To Diary.

choices = ["Post", "Dump", "Clear", "Close"] # create an array containing the button names.
def Main(): # Define a function called Main, which is the main window of the program.

    choice = indexbox("", "Main Menu", choices); # choose the function, Returns 0 for Post, 1 For Dump Etc.

    if  choice == 0: # If choice equals 0, then:

        Post() # Run Post.
        Main() # Return to the main window.
    
    elif choice == 1: # If the first if returns false, then check if choice equals 1, if so then:

        Dump() # Run dump.
        Main() # Return to the main window.
    
    elif choice == 2: # If the second if returns false, then check if choice equals 2, if so then:

        Clear()# Run clear
        Main()# Return to the main window.
        
    elif choice == 3:# If the third if returns false, then check if choice equals 3, if so then:
    
        Die() # Run Die, which closes the program.
    
Main() # Run the Main window. ^ This was all one function remember.




Now go into terminal (or command prompt), and type "python Main.py", without the quotes.

Attached File(s)



Is This A Good Question/Topic? 1
  • +

Page 1 of 1