Using Tkinter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J-Burns

    Using Tkinter

    Hello. Im a bit new to using Tkinter and im not a real pro in
    programming itself... :P. Need some help here.

    Problem 1:

    How do I make something appear on 2 separate windows using Tkinter? By
    this I mean that the format would be something like this:

    You have Page1 : This has 2-3 buttons on it.
    Clicking on each button opens up a new window respectively having
    something entirely different... possibly more buttons,text fields etc.

    Problem 2:

    If I have a drop down box in Pythons tkinter, is it possible that the
    entities that the drop down has are items that have been picked up
    from a notepad file via file handling?

    Problem 3:

    I have 2 radio buttons Radio button A and radio button B. Below them I
    have I have 2 separate panels Panel A and Panel B possibly each having
    separate text fields, buttons etc in it.

    Is it possible that only one of them is shown depending on the radio
    button that I have selected? Meaning that if I have selected on Radio
    button A only Panel A is shown and likewise if I have selected Radio
    Button B only panel B is shown.

    I guess that's it for now. Even if any1 here knws any of these
    problems do post back asap.
    Wud b waiting for replies.
    Thanks.
  • adam2new@gmail.com

    #2
    Re: Using Tkinter

    For references, you may use these PDF files (One URL changed since my
    last time there, but it should be correct for now):


    (The first one may be useful for starting out)

    I'll let you know ahead of time, my knowledge is in Python 2.5 (newer
    versions, as I've heard, are becoming incompatible with older versions
    -- at least, version 3.0, a.k.a. Python 3000).
    I'll also let you know that this may be my first time helping someone.

    On Aug 22, 7:20 am, J-Burns <arslanbur...@g mail.comwrote:
    Problem 1:
    >
    How do I make something appear on 2 separate windows using Tkinter? By
    this I mean that the format would be something like this:
    >
    You have Page1 : This has 2-3 buttons on it.
    Clicking on each button opens up a new window respectively having
    something entirely different... possibly more buttons,text fields etc.
    You can make a class for each that inherits from Tkinter.Topleve l or
    Tkinter.Frame so you can re-create the window on the fly.
    I'll code for using "import Tkinter" and not "from Tkinter import *"
    and also code for using "pack()" for putting widgets on a window.
    Remember that this is just a sample (it should work, but I didn't test
    it).
    * NEVER try to pack() one widget and grid() another in the same
    "parent"


    class Page1(Tkinter.F rame):
    def __init__(self, parent=None):
    Tkinter.Frame._ _init__(self, parent)
    self.make_widge ts()
    self.spawned_wi ndows = {} # for holding the windows
    # assumption: only one of each type of window open
    # at a time
    def make_widgets(se lf): # for putting in the widgets
    self.btn1 = Tkinter.Button( self)
    self.btn1["text"] = "Spawn Window 1" # or some other text
    self.btn1["command"] = self.spawn_wind ow_1
    self.btn1.pack( ) # this alone will pack the button on the next
    # available spot horizontally
    # continue with the other buttons

    def spawn_window_1( self): # I put a generic name here for sample
    # still assuming one of each type of window open
    if self.spawned_wi ndows.has_key(" window1"):
    self.spawned_wi ndows["window1"].lift() # show the window
    return # already has one open
    self.spawned_wi ndows["window1"] = Page1A(self, spawn_closed,
    "window1") # create
    one
    # if you already executed a "mainloop() " call, it should show

    def spawn_closed(se lf, name): # a callback to say it closed
    if self.spawned_wi ndows.has_key(n ame):
    del self.spawned_wi ndows[name]
    return True # meaning: it was found and closed
    return False # meaning: it was not found

    # other "def"s (functions/subroutines) here


    class Page1A(Tkinter. Toplevel):
    def __init__(self, master, closecmd, closearg):
    Tkinter.Topleve l.__init__(self , master)
    # for telling Page1 that this closed
    self.closecmd = closecmd
    self.closearg = closearg
    self.make_widge ts()

    def make_widgets(se lf):
    # *create widgets here
    # now, a close button (I assume you dont need it anymore)
    self.closebtn = Tkinter.Button( self)
    self.closebtn["text"] = "Close window"
    self.closebtn["command"] = self.destroy
    self.closebtn.p ack()

    def destroy(self): # tell Page1 that this closed
    self.closecmd(s elf.closearg)
    Tkinter.Topleve l.destroy(self)


    Problem 2:
    >
    If I have a drop down box in Pythons tkinter, is it possible that the
    entities that the drop down has are items that have been picked up
    from a notepad file via file handling?
    You can just open the file once (on the script's start), when you
    press a button, or every so often, that will update the drop-down box.
    I don't know how you have that file set, so you may need to program
    the syntax in.
    Examples of how that file may be in:

    * One item per line -- use "f = open(filename)" and
    * "for line in f:
    * # add line here"
    * NOTE: requires to clear the drop-down before
    * adding lines, and restoring the selected item
    File
    Edit
    Help

    * Comma separated with first one for drop-down label
    * -- use a Python module available
    * (I think "import csv" and use the Python Docs for help)
    File,New,Open,S ave,Quit
    Edit,Cut,Copy,P aste

    * Microsoft's .ini file style
    * -- Should be found somewhere in the Python Documentation
    [File]
    name1a=value1a
    name1b=value1b
    [Edit]
    name2a=value2a
    namd2b=value2b

    etc.


    Problem 3:
    >
    I have 2 radio buttons Radio button A and radio button B. Below them I
    have I have 2 separate panels Panel A and Panel B possibly each having
    separate text fields, buttons etc in it.
    >
    Is it possible that only one of them is shown depending on the radio
    button that I have selected? Meaning that if I have selected on Radio
    button A only Panel A is shown and likewise if I have selected Radio
    Button B only panel B is shown.
    Python 2.5 and an up-to-date OS (WinXP had a problem in the past)

    * Hide
    widget.pack_for get() # if you used pack()
    widget.grid_for get() # if you used grid()
    widget.place_fo rget() # if you used the Unknown-To-Me place()
    * Show
    * just re-pack (etc.) it.
    I guess that's it for now. Even if any1 here knws any of these
    problems do post back asap.
    Wud b waiting for replies.
    I don't know if it would all work, but I tried to help out.
    Thanks.
    You're welcome.

    Comment

    • Scott David Daniels

      #3
      Re: Using Tkinter

      J-Burns wrote:
      Hello. Im a bit new to using Tkinter and im not a real pro in
      programming itself... :P. Need some help here.
      OK, looks like you are getting direct answers, but I thought I'd
      mention an easy way to experiment with Tkinter programming.

      If you start Idle with the "-n" switch (*), then anything you do shares
      the same "mainloop" as Idle, and your window manipulation is "live".
      This means, that immediately after typing in:
      >>import Tkinter
      >>f = Tkinter.Frame()
      You will see the frame f show up. You can experiment directly with
      watching the effects of calls that you make in the interactive
      interpretter.

      (*) Easy way to do this:
      On some systems, associate a button with "pythonw -m idlelib.idle -n".
      On a Windows system with an Idle shortcut/button/icon already:
      Copy the shortcut/button/icon
      Right-click the shortcut and select the "properties " menu.
      On the "General" tab of the Properties window:
      Give the shortcut a nicer name (I use Idle25-n for mine).
      On the "Shortcut" tab of the properties window, add a space and a -n
      to the target line.
      Click OK, and try out your new button.

      --Scott David Daniels
      Scott.Daniels@A cm.Org

      Comment

      • Cameron Laird

        #4
        Re: Using Tkinter

        In article <2b09111a-e949-45b9-97ce-6c6b2d8d1637@b2 g2000prf.google groups.com>,
        <adam2new@gmail .comwrote:
        >On Aug 22, 7:20 am, J-Burns <arslanbur...@g mail.comwrote:

        Comment

        Working...