CIS2750 Seminar 6 Created by: Anthony Koso - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

CIS2750 Seminar 6 Created by: Anthony Koso

Description:

Must be created before any other widget. w = Label(root, text='Hello world' ... However, if you have no reference to widget, you cannot edit it ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 14
Provided by: ako1
Category:

less

Transcript and Presenter's Notes

Title: CIS2750 Seminar 6 Created by: Anthony Koso


1
CIS2750 Seminar 6 Created by Anthony Koso
  • Exporting X Displays
  • Python/Tk

2
Exporting Your X Display
  • Run a graphical program on one machine and have
    it display on another
  • Can run applications from different platforms
  • Will become more important for A3/A4
  • Can be done with SSH tunneling
  • ssh -X user_at_host
  • putty - connections-gtSSH-gttunnels-gtenable X11
  • ltlocal addressgt0
  • X server software for windows
  • Cygwin http//x.cygwin.com
  • X-Win 32 http//www.starnet.com

3
How To Export Your X Display
  • On local machine
  • Start X server
  • Run xhost ltremote_computergt
  • On remote machine (via telnet/ssh/etc)
  • Set DISPLAY to ltlocal_computergt0
  • Run X apps normally
  • Alternative to setting DISPLAY (in bash)
  • DISPLAYltlocal_computergt0 xapp
  • Must be done each time
  • If SSH tunneling used, skip xhost and setting
    DISPLAY
  • It's already done for you!

4
What is Tkinter
  • Standard Python interface to the Tk GUI toolkit
    (from Scriptics, formerly of Sun Labs)
  • Graphical toolkit for Python
  • Exists on Windows, Mac, Unix, Linux
  • Tk interface is located in _tkinter (a binary
    module)
  • Contains low-level interface to Tk

5
Tkinter
  • Every element is a "widget
  • Three layout styles
  • Pack, Grid, Place
  • Packages for Redhat Linux
  • tkinter, tix, tk,

6
Hello World
  • File hello1.py
  • from Tkinter import
  • root Tk()
  • w Label(root, textHello world)
  • w.pack()
  • root.mainloop()

7
Analyze Hello World
  • root Tk()
  • Create a Tk root widget
  • Only create one root widget for each program
  • Must be created before any other widget
  • w Label(root, textHello world)
  • Create a Label widget
  • w.pack()
  • Call pack method on this widget
  • Tells it to size itself to fit the given text,
    and make itself visible
  • root.mainloop()
  • Starts event loop
  • Handles events from user, windowing system and
    operations queued by Tkinter

8
Using Classes Hello World2
  • class App
  • def __init__ (self, master)
  • frame Frame(master)
  • frame.pack()
  • self.button Button(frame, textQUIT,
  • fgred, commandframe.quit)
  • self.button.pack(sideLEFT)
  • self.hi Button(frame, textHello,
  • commandself.say_hi)
  • def say_hi(self)
  • print Hello world!
  • root Tk()
  • app App(root)
  • root.mainloop()

9
Analyze Class Example
  • def __init__ (self, master)
  • Constructor is called with a parent widget, to
    which it adds children widgets
  • frame Frame(master)
  • frame.pack()
  • Create a Frame widget (simple container)
  • Stored in a local variable called frame
  • Call pack to make frame visible
  • self.button Button(frame, textQUIT,
  • fgred, commandframe.quit)
  • self.button.pack(sideLEFT)
  • Create two Button widgets as children to frame
  • Widgets are packed relative to their parent (ex.
    master)

10
Widget Reference
  • In second example, frame widget is stored in
    frame (local variable)
  • Tkinter automatically maintains a widget tree
  • Must use destroy() to remove a widget
  • However, if you have no reference to widget, you
    cannot edit it
  • Beware of other examples that create widgets
    without reference to them

11
Widget Classes
12
Tkinter Example
  • See simplegui.py

13
Final Notes
  • Questions?
  • Comments?
  • Reminder A2 due on Feb 15
Write a Comment
User Comments (0)
About PowerShow.com