我对 tkinter 还是很陌生,所以我决定尝试制作一个简单的不和谐应用程序,当发送新消息时我遇到了麻烦。因此,当消息开始离开页面时,直到您发送另一条消息才能看到最新消息,我不知道为什么它会这样。有没有人有任何想法?代码:
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()
谢谢。