Tkinter 无法在右侧对齐文本

Tkinter Unable to justify text on right

我有基于 tkinter 的聊天 window,我想在 this question

的帮助下在聊天框的右侧显示消息

我写了下面的代码,但是这段代码的问题是第一条消息显示在左侧,然后所有消息都显示在右侧:

import sys
from tkinter import *
import os
import cgitb
cgitb.enable ( )
msg = [ '0v' ]
class ChatInterface ( Frame ) :

  def __init__ ( self , master = None ) :
    Frame.__init__ ( self , master )
    self.imglist = [ ]
    self.dic = dict()
    self.master = master
    self.text_frame = Frame ( self.master , bd = 6 , bg = "black" )
    self.text_frame.pack ( expand = True , fill = BOTH )
    self.text_box_scrollbar = Scrollbar ( self.text_frame , bd = 0 , bg = "#426CB4" )
    self.text_box_scrollbar.pack ( fill = Y , side = RIGHT )
  self.text_box=Text(self.text_frame,yscrollcommand=self.text_box_scrollbar.set,state=DISABLED ,
                           bd = 1 , padx = 6 , pady = 6 , spacing3 = 8 , wrap = WORD , bg = "black" ,
                           font = "Times 10" , relief = GROOVE ,
                           width = 10 , height = 1 , fg = "white" )

    self.text_box.pack ( expand = True , fill = BOTH )
    self.text_box_scrollbar.config ( command = self.text_box.yview )

    self.entry_frame = Frame ( self.master , bd = 1 , bg = "black" )
    self.entry_frame.pack ( side = LEFT , fill = BOTH , expand = True )

    self.entry_field = Entry ( self.entry_frame , bd = 1 , justify = LEFT )

    self.entry_field.pack ( fill = X , padx = 6 , pady = 6 , ipady = 3 )



    self.upload_button_frame = Frame ( self.master , bd = 0 , bg = 'black' )
    self.upload_button_frame.pack ( fill = BOTH )

    self.pic_button = Button ( self.upload_button_frame , text = 'send' ,
                               relief = GROOVE ,
                               borderwidth = 0 , bg = 'slateblue2' , bd = 0 , fg = "Black" ,
                               command = lambda : self.sent() , activebackground = "black" ,
                               activeforeground = "#000000" )
    self.pic_button.pack ( in_ = self.upload_button_frame , side = RIGHT )
    self.master.bind ( "<Return>" , self.sent )
    self.pictccp = t.CreateToolTip ( self.pic_button , "Click here to share pictures" )


    

    
      def sent ( self ) :

      message = self.entry_field.get ( ).strip ( )
      req = requests.post ( url1 , data = { 'msg' : message } ).text
      print ( req )
      if message != '' :
          pr1 = message
          self.text_box.configure ( state = NORMAL )
          # self.text_box.insert ( END , time  , ("small" , "right" , "greycolour") )
          self.text_box.window_create ( END , window = Label ( self.text_box , fg = "floral white" , text = pr1 ,wraplength = 200 , font = ("Arial" , 12) ,bg = "#426CB4" , bd = 8 , justify = "left" ) )
          self.text_box.insert(END,'\n ', "right")

          self.text_box.tag_config ( "right" , justify = "right" )

          self.text_box.configure ( state = DISABLED )

          self.text_box.see ( END )
          self.text_box.yview ( END )
          self.entry_field.delete ( 0 , END )

  


def chating ( ) :
  root = Tk()

  a = ChatInterface ( root )
  root.geometry ( "340x500" )
  root.title ( "Chat" )
  root.mainloop ( )

chating()

这是输出:

从输出中看到 window 第一条消息显示在左侧,而我在右侧进行了调整,请帮助我解决问题,谢谢

是因为在第一个标签前没有插入一个space字符和标签'right',所以没有右对齐。由于 "\n " 插入在后续标签之前,因此这些标签右对齐。

建议在每个插入的标签之前插入一个space而不是在“\n”之后:

self.text_box.insert(END, " ", "right")
self.text_box.window_create(END, window=Label( self.text_box, fg="floral white", text=pr1, wraplength=200, font=("Arial", 12), bg="#426CB4", bd=8, justify="left"))
self.text_box.insert(END, "\n")