Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Python / Tkinter : making dynamic interfaces
PostPosted: Thu Jan 02, 2014 12:40 pm 
User avatar
Jedi Master

Joined: Wed Apr 02, 2008 11:16 pm

Posts: 1007

Location: Kapiti Coast, Wellington
I'm playing around with the idea of making dynamic GUI through python and tkinter. I'm using tkinter because it comes packaged with python3 so there are less portability issues of end users having to install extra modules just to get my program running.

Some of my programs require GUI content to be dynamically changed. At the moment I'm doing this with the sledgehammer approach of dettroying entire frames and rebuilding on the fly. I think it's a pretty messy solution because replacing and repacking frames could hash the interface up badly, but then I don't know of any method to simply "empty" a given frame ready to refill.

Example of my bad method here (specifically, the first half of the "newgrid" function) - In this one I've made a simple window where the user inputs an integer number (followed by ENTER) and the main GUI is redrawn with the number of buttons the user has requested.

# Change buttons program - Python 3 / Tkinter
# Test for dynamically resetting the contents of a frame

import tkinter

TITLE = "Button Changer"

def buttonfeedback(identity):
   # Display a message when button pushed
   print("You pushed", identity)

def errorcheck(checkstring):
   # Check that user input is a valid integer
   state = True
   if len(checkstring) < 1:
      state = False
   for character in checkstring:
      if character not in "0123456789":
         state = False
   return state

def generate(somenumber):
   # Call to greate new set of buttons of given amount
   userinput = somenumber.get()
   if errorcheck(userinput) == False:
      print("Buttons to make:", userinput)
      uservalue.set("")         # reset the entry box

def newgrid(number):
   # Reset the frame ans redraw with requested number of buttons
   # Clear the old frame
   global maingrid
   maingrid = tkinter.Frame()
   maingrid.pack(side = "top")
   # draw all new buttons
   button = []
   for item in range(number):
      button.append(tkinter.Button(maingrid, text = str(item + 1), command = lambda which = item + 1 : buttonfeedback(which)))
      button[item].pack(side = "left")

# Make original layout

mainwin = tkinter.Tk()

topbar = tkinter.Frame()
maingrid = tkinter.Frame()

topbar.pack(side = "top")
maingrid.pack(side = "top")

instructions = tkinter.Label(topbar, text="How many buttons?")
instructions.pack(side = "left")

uservalue = tkinter.StringVar()

inputbox = tkinter.Entry(topbar, width = 8, textvariable = uservalue)
inputbox.pack(side = "left")
inputbox.bind('<Return>', lambda event, content = inputbox : generate(content))   # Detect when user "enters" a number

newgrid(STARTNUMBER)      # Start with default number of buttons.


Anyone know of better ways of doing this?

P.S. I'm at the newbie "I hate lambda functions" stage.

 Post subject: Re: Python / Tkinter : making dynamic interfaces
PostPosted: Mon Apr 14, 2014 2:42 pm 
User avatar

Joined: Sun Feb 17, 2008 12:29 pm

Posts: 1337

Location: Wellington
Did you get any further with this Lancer? Sounds like an interesting idea.

Beware the lollipop of mediocrity: lick it once and you suck forever

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

About Us

Contact Us

Powered by phpBB® Forum Software © phpBB Group
Logo Design by - Oliver Hilbert Revilo Designs