Home > other >  Tkinter How to create new instance of chat window whenever the button is clicked
Tkinter How to create new instance of chat window whenever the button is clicked

Time:06-07

I am working on a simple chat app. In my project there are two windows first one is main window in which i have a textbox and a button in text i enter name of person whom i want to talk and when i clicked on button a chat window appeared with name of a person. Issue come when i tried to send message to multiple person on same time all messages go to the last one i searched for. for example i searched two person two different chat window appears with receiver names and i send different messages to different people but in database all messages are stored with name of latest person.

Here is my code:

from tkinter import *
import os
import pathlib
import cgitb
from tkinter import filedialog
from tkinter.filedialog import askopenfile
import threading
import ctypes
from urllib.request import urlopen , Request
import requests
from apscheduler.scheduler import Scheduler
cgitb.enable ( )

url1 = 'http://localhost:9090/project/php/test.php'

def screen():

    global search
    search = Tk()
    search.geometry ( "400x400" )
    search.title ( "dsf" )
    search.config(bg = "black")
    global s
    global search_entry
    s = StringVar()
    searchlbl = Label ( search , text = '' , bg = 'black' , fg = 'white' , font = ('Times' , 12) )
    searchlbl.pack ( )
    searchlbl = Label ( search , text = '' , bg = 'black' , fg = 'white' , font = ('Times' , 12) )
    searchlbl.pack ( )
    searchlbl = Label ( search , text = '' , bg = 'black' , fg = 'white' , font = ('Times' , 12) )
    searchlbl.pack ( )
    searchlbl = Label ( search , text = '' , bg = 'black' , fg = 'white' , font = ('Times' , 12) )
    searchlbl.pack ( )
    searchlbl = Label ( search , text = '' , bg = 'black' , fg = 'white' , font = ('Times' , 12) )
    searchlbl.pack ( )
    search_entry = Entry ( search , text = 'search' , width = 40, textvariable = s )
    search_entry.pack ( )
    searchlbl = Label ( search , text = '' , bg = 'black' , fg = 'white' , font = ('Times' , 12) )
    searchlbl.pack ( )
    searchbtn = Button ( search , text = "Search" , width = 10 , height = 1 , bg = "#426CB4" , fg = "white" ,
                         command = chating , activebackground = "medium turquoise" , activeforeground = "white" )
    searchbtn.pack ( )

    #sender.append(chating())
    #var1 = chating()
    search.mainloop ( )



def chating():

    window = Toplevel(search)
    window.title(s.get())
    messages = Text(window)
    messages.pack()

    input_user = StringVar()
    input_field = Entry(window, text=input_user)
    input_field.pack(side=BOTTOM, fill=X)

    def Enter_pressed(event):
        input_get = input_field.get()
        print(input_get)
        messages.insert(INSERT, '%s\n' % input_get)

        # label = Label(window, text=input_get)
        sender = s.get ( )
        req = requests.post ( url1 , data = { 'msg' :input_get, 'sender' : sender } ).text
        input_user.set('')
        # label.pack()
        return "break"

    frame = Frame(window)  # , width=300, height=300)
    input_field.bind("<Return>", Enter_pressed)
    frame.pack()

    window.mainloop()

screen()

looking for your kind help thanks.

CodePudding user response:

It is because you get the user name from the same entry s every time a message is sent to server.

You just need to save the user name to a local variable when the chat window is open:

def chating():
    # save the user name
    sender = s.get()

    window = Toplevel(search)
    window.title(sender)
    messages = Text(window)
    messages.pack()

    input_user = StringVar()
    input_field = Entry(window, text=input_user)
    input_field.pack(side=BOTTOM, fill=X)

    def Enter_pressed(event):
        input_get = input_field.get()
        print(input_get)
        messages.insert(INSERT, '%s\n' % input_get)

        req = requests.post ( url1 , data = { 'msg' :input_get, 'sender' : sender } ).text
        input_user.set('')
        # label.pack()
        return "break"

    frame = Frame(window)  # , width=300, height=300)
    input_field.bind("<Return>", Enter_pressed)
    frame.pack()

    # not necessary to call mainloop() more than once
    #window.mainloop()

Note also that you don't need to call window.mainloop() inside chating() function.

  • Related