简体   繁体   中英

Tkinter Unable to justify text on right

I have tkinter based chat window where i want to display messages on right side of chat box with help of this question

i wrote below code but here issue with this code is first message is shown on left side and after all are shown on right side:

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()

this is the output: 输出

as seen from output window first message is displayed on left side while i justify it on right kindly help me to sort it out thank you

It is because a space character is not inserted with tag 'right' before the first label, so it is not aligned to the right. As "\n " is inserted before subsequent labels, those labels are aligned to the right.

Suggest to insert a space before every label inserted instead of after the "\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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM