I am still pretty new to tkinter so I decided to try to make a simple rip off discord app, I am having trouble when a new message is sent. So, when the messages start going off the page the most recent message can’t be seen until you send another one and I don’t know why it’s acting like this. Does anyone have any ideas? Code:
from tkinter import *
import tkinter.simpledialog
from tkinter import ttk
import tkinter as tk
import os
root = Tk()
root.title("discord rip off")
root.geometry("720x480+0+0")
def add_Server():
return tk.simpledialog.askinteger("Add Server", "Server Code")
# main message area
frame = Frame(root, width=600, height=300, bd=5, relief=RIDGE, bg="red") # the bd and the relief makes a nice effect outlineing the frame
frame.grid(row=0, column=1)
messageEnter = Entry(frame)
messageEnter.pack(side=BOTTOM, fill=BOTH)
# canvas
myCanvas = Canvas(frame)
myCanvas.pack(fill=BOTH, side=LEFT)
# message scrollbar
messageScrollbar = ttk.Scrollbar(frame, orient=VERTICAL, command=myCanvas.yview)
messageScrollbar.pack(side=RIGHT, fill=Y)
# canvas
myCanvas.configure(yscrollcommand=messageScrollbar.set)
myCanvas.bind("<Configure>", lambda e: myCanvas.configure(scrollregion=(myCanvas.bbox("all"))))
# creating a second frame that is the message frame
messageFrame = Frame(myCanvas)
# canvas
myCanvas.create_window((0,0), window=messageFrame, anchor="nw")
def send_Message(event):
if messageEnter.get() == "":
print(myCanvas.bbox("all"))
pass
else:
Label(messageFrame, text=messageEnter.get(), anchor="w").pack(fill=X, expand=1)
myCanvas.configure(scrollregion=(myCanvas.bbox("all")))
messageEnter.delete(0, "end")
myCanvas.yview_moveto("1.0") # moves the scroll bar to the bottom when called
# send message
messageEnter.bind("<Return>", send_Message)
# server list
# photo = PhotoImage(file=os.path.dirname(__file__)+"/images/add.png")
# photo = photo.subsample(1, 1) # resizies image
serverListFrame = Frame(root, width=38, height=200, bg="blue")
serverListFrame.grid(row=0, column=0, sticky=N)
addServer = Button(root) # , image=photo
addServer.grid(row=0, column=0, sticky=N)
root.update()
root.mainloop()
Thanks.
"You have to reset the Canvas's scrollregion to make the newly-added Label visible - which you're doing, but slightly too early: the Label has not had any size or position actually assigned to it yet (that requires an iteration of the mainloop), so it's not being taken into account. Putting root.update_idletasks(), between creating the Label and setting the scrollregion, should fix it." @jasonharper