I'm making a Treasure Island type game and I'm trying to make the screen follow the text e.g if the text goes off the screen the screen automatically scrolls down to fit in the text. I've tried various types of this by different people mostly scrollbars but I can't get any to work, I currently have a "scrollbar" set up but it doesn't work.
here's the code:
import tkinter
import tkinter as ttk
from Treasure_Island_V2_art import Treasure_Chest, Pirate_Ship
window = tkinter.Tk()
window.title("Treasure Island")
window.attributes('-fullscreen', True)
window.config(bg = "black")
window.grid_columnconfigure(0, weight=1)
window.grid_rowconfigure(0, weight=1)
text = tkinter.Text(window, height=10)
text.grid(row=0, column=0, sticky=tkinter.EW)
scrollbar = ttk.Scrollbar(window, orient='vertical', command=text.yview)
scrollbar.grid(row=0, column=1, sticky=tkinter.NS)
text['yscrollcommand'] = scrollbar.set
logo = tkinter.Label(window, text = '''
| | | |
_________|________________.=""_;=.______________|_____________________|_______
| | ,-"_,="" `"=.| |
|___________________|__"=._o`"-._ `"=.______________|___________________
| `"=._o`"=._ _`"=._ |
_________|_____________________:=._o "=._."_.-="'"=.__________________|_______
| | __.--" , ; `"=._o." ,-"""-._ ". |
|___________________|_._" ,. .` ` `` , `"-._"-._ ". '__|___________________
| |o`"=._` , "` `; .". , "-._"-._; ; |
_________|___________| ;`-.o`"=._; ." ` '`."\` . "-._ /_______________|_______
| | |o; `"-.o`"=._`` '` " ,__.--o; |
|___________________|_| ; (#) `-.o `"=.`_.--"_o.-; ;___|___________________
____/______/______/___|o;._ " `".o|o_.--" ;o;____/______/______/____
/______/______/______/_"=._o--._ ; | ; ; ;/______/______/______/_
____/______/______/______/__"=._o--._ ;o|o; _._;o;____/______/______/____
/______/______/______/______/____"=._o._; | ;_.--"o.--"_/______/______/______/_
____/______/______/______/______/_____"=.o|o_.--""___/______/______/______/____
/______/______/______/______/______/______/______/______/______/______/_____ /
''')
logo.grid()
logo.config(font=('arial', 15), fg = "green", bg = "black")
line1 = tkinter.Label(window, text=("Welcome to Treasure Island,\n"
"Your goal is to find the buried Treasure!"))
label1 = tkinter.StringVar()
a = tkinter.Label(textvariable=label1, font = ('arial', 20), fg = "blue", bg = "black")
a.grid()
for i in line1.cget("text"):
a=label1.get()
label1.set(a i)
window.after(50)
window.update()
space = tkinter.Label(window, text=" \n \n ")
space.grid()
space.config(bg = "black")
line2 = tkinter.Label(window, text="You wake up to find yourself laying in the middle of a deserted beach,\n"
"you feel hungry but you see something that catches your eye.")
label2 = tkinter.StringVar()
b = tkinter.Label(textvariable=label2, font = ('arial', 20), fg = "blue", bg = "black")
b.grid()
for i in line2.cget("text"):
b=label2.get()
label2.set(b i)
window.after(30)
window.update()
window.mainloop()
CodePudding user response:
You can create a canvas
with a frame
inside to contain the widgets you would like to scroll; Create a scrollbar for the canvas
and create widgets inside frame
.
Use onFrameConfigure(canvas)
listen to Frame
size change, and use canvas.yview_moveto( 1 )
to scroll to the end of window.
Reference: Scrollbar in Tkinter grid
I also suggest to change the font for text art to monospaced fonts, e.g. consolas
import tkinter
from Treasure_Island_V2_art import Treasure_Chest, Pirate_Ship
def onFrameConfigure(canvas):
'''Reset the scroll region to encompass the inner frame'''
canvas.configure(scrollregion=canvas.bbox("all"))
canvas.yview_moveto( 1 )
window = tkinter.Tk()
window.title("Treasure Island")
window.attributes('-fullscreen', True)
window.config(bg = "black")
window.grid_columnconfigure(0, weight=1)
window.grid_rowconfigure(0, weight=1)
# set Canvas and Frame for scrollbar
canvas = tkinter.Canvas(window, border=0, highlightthickness=0, background="black")
frame = tkinter.Frame(canvas, background="black")
scrollbar = tkinter.Scrollbar(window, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=scrollbar.set)
scrollbar.grid(row=0, column=1, sticky=tkinter.NS)
frame.grid()
canvas.grid(row=0, column=0, sticky=tkinter.NSEW)
canvas.create_window((canvas.winfo_screenwidth()/2,0), window=frame, anchor=tkinter.CENTER)
frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))
logo = tkinter.Label(frame, justify='left', text = '''
| | | |
_________|________________.=""_;=.______________|_____________________|_______
| | ,-"_,="" `"=.| |
|___________________|__"=._o`"-._ `"=.______________|___________________
| `"=._o`"=._ _`"=._ |
_________|_____________________:=._o "=._."_.-="'"=.__________________|_______
| | __.--" , ; `"=._o." ,-"""-._ ". |
|___________________|_._" ,. .` ` `` , `"-._"-._ ". '__|___________________
| |o`"=._` , "` `; .". , "-._"-._; ; |
_________|___________| ;`-.o`"=._; ." ` '`."\` . "-._ /_______________|_______
| | |o; `"-.o`"=._`` '` " ,__.--o; |
|___________________|_| ; (#) `-.o `"=.`_.--"_o.-; ;___|___________________
____/______/______/___|o;._ " `".o|o_.--" ;o;____/______/______/____
/______/______/______/_"=._o--._ ; | ; ; ;/______/______/______/_
____/______/______/______/__"=._o--._ ;o|o; _._;o;____/______/______/____
/______/______/______/______/____"=._o._; | ;_.--"o.--"_/______/______/______/_
____/______/______/______/______/_____"=.o|o_.--""___/______/______/______/____
/______/______/______/______/______/______/______/______/______/______/_____ /
''')
logo.grid()
logo.config(font=('consolas', 15), fg = "green", bg = "black")
line1 = tkinter.Label(frame, text=("Welcome to Treasure Island,\n"
"Your goal is to find the buried Treasure!"))
label1 = tkinter.StringVar()
a = tkinter.Label(frame, textvariable=label1, font = ('arial', 20), fg = "blue", bg = "black")
a.grid()
for i in line1.cget("text"):
a=label1.get()
label1.set(a i)
window.after(50)
window.update()
space = tkinter.Label(frame, text=" \n \n ")
space.grid()
space.config(bg = "black")
line2 = tkinter.Label(frame, text="You wake up to find yourself laying in the middle of a deserted beach,\n"
"you feel hungry but you see something that catches your eye.")
label2 = tkinter.StringVar()
b = tkinter.Label(frame, textvariable=label2, font = ('arial', 20), fg = "blue", bg = "black")
b.grid()
for i in line2.cget("text"):
b=label2.get()
label2.set(b i)
window.after(30)
window.update()
window.mainloop()