TKinter problem
ezd
eugene_druker at yahoo.com
Tue Mar 21 10:27:16 EST 2006
C D Wood wrote:
> To whom this may concern,
> Below is the source code, which
>
> demonstrates a
> problem I am having making a GUI for my python project work.
> 'table.txt'
> is a file that is read from the same folder.
>
> My code writes to a text file 'table.txt', and 'table.txt' is displayed
> in
> the GUI. The user can generate new data at the click of a button
> which re-writes 'table.txt', but I can only add the new table to the
> GUI
> window rather than 'update' the existing one.
>
> Any assistance would be much appreciated,
>
> Regards,
> Christian Wood.
> Part III Aerospace Engineering
> University of Southampton, UK.
>
> ##################################################################
> from Tkinter import *
>
> #Tkinter User Interface
> class MoC:
> def __init__(self, master):
> frame = Frame(master, width=600, height=800, bd=1)
> frame.pack()
>
> #Button frame
> iframe4 = Frame(frame, bd=2, relief=SUNKEN)
> #Using this button below, I want to update the text box in iframe5.
> Button(iframe4, text='Display table.txt',
> command=self.DisplayUpdate).pack(side=LEFT, padx=5)
> Button(iframe4, text='Quit', command=self.quit).pack(side=LEFT,
> padx=5)
> iframe4.pack(expand=1, fill=X, pady=10, padx=5)
>
> #Text box frame
> iframe5 = Frame(frame, bd=2, relief=SUNKEN)
> text=Text(iframe5, height=10, width =70)
> fd = open('table.txt') #table.txt must be in the same folder
> lines = fd.read()
> fd.close()
> text.insert(END, lines)
> text.pack(side=LEFT, fill=X, padx=5)
> sb = Scrollbar(iframe5, orient=VERTICAL, command=text.yview)
> sb.pack(side=RIGHT, fill=Y)
> text.configure(yscrollcommand=sb.set)
> iframe5.pack(expand=1, fill=X, pady=10, padx=5)
>
> #Command definitions
> def quit(self):
> root.destroy()
>
> def DisplayUpdate(self): #The command definition used to update the
> display.
> #Could I insert a line here to remove the existing frame/text
> box first? <<<<<=====
> iframe5 = Frame(root, bd=2, relief=SUNKEN)
> text = Text(iframe5, height=10, width =70)
> fd = open('table.txt')
> lines = fd.read()
> fd.close()
> text.insert(END, lines)
> text.pack(side=LEFT, fill=X, padx=5)
> sb = Scrollbar(iframe5, orient=VERTICAL, command=text.yview)
> sb.pack(side=RIGHT, fill=Y)
> text.configure(yscrollcommand=sb.set)
> iframe5.pack(expand=1, fill=X, pady=10, padx=5)
>
> root = Tk()
> root.option_add('*font', ('arial', 10))
> all = MoC(root)
> root.title('2D Method of Characteristics')
> root.update
> root.mainloop()
What you want probably looks like this:
from Tkinter import *
class MoC:
def __init__(self, master):
frame = Frame(master, width=600, height=800, bd=1)
frame.pack()
iframe4 = Frame(frame, bd=2, relief=SUNKEN)
Button(iframe4, text='Display table.txt',
command=self.DisplayUpdate).pack(side=LEFT, padx=5)
Button(iframe4, text='Quit',
command=self._quit).pack(side=LEFT, padx=5)
iframe4.pack(expand=1, fill=X, pady=10, padx=5)
iframe5 = Frame(frame, bd=2, relief=SUNKEN)
self.text=Text(iframe5, height=10, width =70)
# read the file in the update function; create Text & Scrollbar
only once
self.text.pack(side=LEFT, fill=X, padx=5)
sb = Scrollbar(iframe5, orient=VERTICAL,
command=self.text.yview)
sb.pack(side=RIGHT, fill=Y)
self.text.configure(yscrollcommand=sb.set)
iframe5.pack(expand=1, fill=X, pady=10, padx=5)
self.DisplayUpdate()
def _quit(self): # quit is a keyword in python 2.4 IDE
root.destroy()
def DisplayUpdate(self):
fd = open('table.txt')
lines = fd.read()
fd.close()
self.text.config(state=NORMAL)
self.text.delete(1.0, END)
self.text.insert(END, lines)
self.text.config(state=DISABLED)
# previous 4 lines are to make the text READONLY, see more in:
#
http://www.pythonware.com/library/tkinter/introduction/x8309-patterns.htm
root = Tk()
root.option_add('*font', ('arial', 10))
all = MoC(root)
root.title('2D Method of Characteristics')
root.update
root.mainloop()
ezd
More information about the Python-list
mailing list