动态滚动条 tkinter
Dynamic scroll bar tkinter
我对 tkinter 还是很陌生,所以我决定尝试制作一个简单的 rip off discord 应用程序,发送新消息时我遇到了麻烦。因此,当消息开始离开页面时,直到您发送另一条消息才能看到最近的消息,我不知道为什么会这样。有人有什么想法吗?
代码:
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()
谢谢。
“您必须重置 Canvas 的 scrollregion 以使新添加的 Label 可见 - 您正在这样做,但有点太早了:Label 尚未实际分配任何大小或位置到它(这需要主循环的迭代),所以它没有被考虑在内。在创建标签和设置滚动区域之间放置 root.update_idletasks() 应该修复它。”
@杰森哈珀
我对 tkinter 还是很陌生,所以我决定尝试制作一个简单的 rip off discord 应用程序,发送新消息时我遇到了麻烦。因此,当消息开始离开页面时,直到您发送另一条消息才能看到最近的消息,我不知道为什么会这样。有人有什么想法吗? 代码:
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()
谢谢。
“您必须重置 Canvas 的 scrollregion 以使新添加的 Label 可见 - 您正在这样做,但有点太早了:Label 尚未实际分配任何大小或位置到它(这需要主循环的迭代),所以它没有被考虑在内。在创建标签和设置滚动区域之间放置 root.update_idletasks() 应该修复它。” @杰森哈珀