[Tutor] tkinter scrolling

Rusty Newton rustynewton@comcast.net
Thu Dec 19 17:46:35 2002


This is a multi-part message in MIME format.

--Boundary_(ID_R9SiFLua7Z3dnSc95DyOig)
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 7BIT

hail!

creating a text game, i have a textbox widget and entry widget, when you type somthing in the
entry widget it takes the text from it and prints it in the textbox widget,  when the text gets to the
bottom of the textbox widget i want it to automatically scroll down with the text so the text doesnt
go off the screen hehe =P anyone know how to do this? ive been looking through the scrolling methods
and such for the text field and scroll bar but i still cant figure it out! heres my code so far =] all the
comments are for my own sake hehe

from Tkinter import *
global loc_x,loc_y#define the variables for the x,y coordinates used in virtual mapping
loc_x = 0
loc_y = 0

class App:

##  Create GUI 

    def __init__(self, master):#creates the master widget self

        frame = Frame(master)#defines the master frame 
        frame.pack()#makes it visible
        
        #this creates the text box display
        self.text_box = Text(frame, state=DISABLED, bg="black", fg="green", wrap=WORD)
        self.text_box.grid(row=0, column=0, columnspan=4)
               
        #this creates the text entry box 
        self.text_entry = Entry(frame, bg="black", fg="green", insertbackground="white")
        self.text_entry.grid(row=1, column=0, sticky=E+W)
        self.text_entry.bind("<Return>", self.enter_command)#binds the enter key to the entrybox

        #creates the enterbutton
        self.text_enterbutton = Button(frame, text="ENTER", fg="blue", command=self.button_enter)
        self.text_enterbutton.grid(row=1, column=1, sticky=E+W)

        #creates the quitbutton
        self.text_quitbutton = Button(frame, text="QUIT", fg="blue", command=frame.quit)
        self.text_quitbutton.grid(row=1, column=2, sticky=E+W)
 
    def enter_command(self, event):
        #this is what happens when you press the enter button
        #in the entry text box
                self.text_box.config(state=NORMAL)
                sending_text = self.text_entry.get() + "\n"
                self.text_box.insert(END, sending_text)
                self.text_entry.selection_range(0, END)
                self.text_box.config(state=DISABLED)
    def button_enter(self):
        #this is what happens when you click the enter button
        #after entering text in the text entry box
                self.text_box.config(state=NORMAL)
                sending_text = self.text_entry.get() + "\n"
                self.text_box.insert(END, sending_text)
                self.text_entry.selection_range(0, END)
                self.text_box.config(state=DISABLED)
        

root = Tk()#creates the root widget to run everything

app = App(root)#creates a instance of the class app as a child to the parent root widget

root.mainloop()#starts the main tk loop to start checking all its child widgets



--Boundary_(ID_R9SiFLua7Z3dnSc95DyOig)
Content-type: text/html; charset=iso-8859-1
Content-transfer-encoding: 7BIT

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2800.1126" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>hail!</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>creating a text game, i have a textbox widget and 
entry widget, when you type somthing in the</FONT></DIV>
<DIV><FONT face=Arial size=2>entry widget it takes the text from it and prints 
it in the textbox widget,&nbsp; when the text gets to the</FONT></DIV>
<DIV><FONT face=Arial size=2>bottom of the textbox widget i want it to 
automatically scroll down with the text so the text doesnt</FONT></DIV>
<DIV><FONT face=Arial size=2>go off the screen hehe =P anyone know how to do 
this? ive been looking through the scrolling methods</FONT></DIV>
<DIV><FONT face=Arial size=2>and such for the text field and scroll bar but i 
still cant figure it out! heres my code so far =] all the</FONT></DIV>
<DIV><FONT face=Arial size=2>comments are for my own sake hehe</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>from Tkinter import *<BR>global loc_x,loc_y#define 
the variables for the x,y coordinates used in virtual mapping<BR>loc_x = 
0<BR>loc_y = 0</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>class App:</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>##&nbsp; Create GUI </FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; def __init__(self, 
master):#creates the master widget self</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; frame = 
Frame(master)#defines the master frame 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; frame.pack()#makes it 
visible<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #this creates the text box 
display<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.text_box = 
Text(frame, state=DISABLED, bg="black", fg="green", 
wrap=WORD)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
self.text_box.grid(row=0, column=0, 
columnspan=4)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
#this creates the text entry box <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
self.text_entry = Entry(frame, bg="black", fg="green", 
insertbackground="white")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
self.text_entry.grid(row=1, column=0, 
sticky=E+W)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
self.text_entry.bind("&lt;Return&gt;", self.enter_command)#binds the enter key 
to the entrybox</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #creates 
the enterbutton<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
self.text_enterbutton = Button(frame, text="ENTER", fg="blue", 
command=self.button_enter)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
self.text_enterbutton.grid(row=1, column=1, sticky=E+W)</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #creates 
the quitbutton<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
self.text_quitbutton = Button(frame, text="QUIT", fg="blue", 
command=frame.quit)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
self.text_quitbutton.grid(row=1, column=2, 
sticky=E+W)<BR>&nbsp;<BR>&nbsp;&nbsp;&nbsp; def enter_command(self, 
event):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #this is what happens when 
you press the enter button<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #in the 
entry text 
box<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
self.text_box.config(state=NORMAL)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
sending_text = self.text_entry.get() + 
"\n"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
self.text_box.insert(END, 
sending_text)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
self.text_entry.selection_range(0, 
END)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
self.text_box.config(state=DISABLED)<BR>&nbsp;&nbsp;&nbsp; def 
button_enter(self):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #this is what 
happens when you click the enter 
button<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #after entering text in the 
text entry 
box<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
self.text_box.config(state=NORMAL)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
sending_text = self.text_entry.get() + 
"\n"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
self.text_box.insert(END, 
sending_text)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
self.text_entry.selection_range(0, 
END)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
self.text_box.config(state=DISABLED)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>root = Tk()#creates the root widget to run 
everything</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>app = App(root)#creates a instance of the class app 
as a child to the parent root widget</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>root.mainloop()#starts the main tk loop to start 
checking all its child widgets</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><BR></FONT>&nbsp;</DIV></BODY></HTML>

--Boundary_(ID_R9SiFLua7Z3dnSc95DyOig)--