module 9

Cards (36)

  • Graphical User Interface (GUI) programming

    Event-driven programming
  • tkinter
    Python module for creating GUI programs
  • Widget settings and placement
    1. Creating widget object
    2. Setting options
    3. Placing widget in GUI
  • Using frames for layout
    1. Placing widgets into frames
    2. Packing frames into main window
  • Buttons and message boxes
    • Buttons need event handler attached
    • tkinter.messagebox module for showing message boxes
  • Input using Entry widgets
    1. Creating Entry widget
    2. Getting/setting content
    3. Manipulating text in Entry
  • Graphical User Interfaces (GUIs)

    • What most people use to interact with computers
    • Involve windows, buttons, checkboxes, etc.
    • Interaction via mouse/touchscreen rather than just keyboard
  • Prior to GUIs, interaction was primarily via Command Line Interfaces (CLIs)
  • CLI programs are easier to learn and more cross-platform, but most programs have GUIs
  • Event-driven programming

    • User can interact with GUI at any time
    • Code runs in response to user events
  • Event-driven programming
    1. Main loop listens for events
    2. Event handlers triggered by events
  • Many events will not have an event handler associated with them
  • It's important to run ongoing/resource-heavy events in a separate thread
  • Entry Widget
    • widgets create a single-line text field
    • The width is set in characters, not pixels
    • The show setting makes it work like a password field
    • There are methods that allow you to interact with the content of the field
  • .get()
    returns the current content as a string – this is what you will use most often
  • .delete()

    and .insert() allow you to delete from or insert into the field (using index numbers, or using indices such as tkinter.INSERT or tkinter.END)
  • .icursor()
    lets you set the insertion cursor
  • .select_*()

    methods allow you to select text
  • Entry Widget in main window
    • self.demo_entry = tkinter.Entry(self.main, width=15)
    • self.demo_entry = tkinter.Entry(self.main, width=15, show='•')
  • delete()
    delete all characters
  • insert()

    insert "!" at insert cursor position
  • select()

    select all from insert cursor
  • StringVar
    A StringVar object is like a string variable. It can be linked to widgets like Label and Entry, and will automatically show what it contains at all times
  • .set()
    and .get() methods can set and get the value of a StringVar
  • IntVar (and others) also exist, for different data types!
  • GUI programs can recreate almost all programs from the unit
  • GUI consists of
    • frames, labels, entries and buttons
  • Implementing the GUI in the constructor

    1. create Frame widgets
    2. create and pack height Label and Entry widgets
    3. create and pack weight Label and Entry widgets
    4. create and pack Button widgets
    5. pack Frame widgets
    6. set initial focus on height Entry widget
  • show_result()
    calculate and show BMI, or error message
  • reset_form()

    clear both Entry widgets and set focus on height
  • GUI-based programs
    event-driven; Code handles events that are triggered by users interacting with the GUI
  • GUI elements ("widgets")

    • include buttons, text entry fields, labels
  • Settings
    can control text, font, size, colours, padding, alignment and more
  • Packing
    Widgets must be packed into the GUI, and frames can help achieve desired layout
  • Messageboxes
    can be used to show pop-up information / warning / error boxes
  • Confirmation/decision boxes
    Can also show boxes with OK/Cancel, Yes/No and Retry/Cancel buttons