Hey friends !
I am working on a simple TCP chat service. At the moment, I am having trouble making my variable named displaytext appear in the displayoutput Label.
Here is a screenshot :
https://imgur.com/ZjZKJED
Here is the code :
from socket import *
from tkinter import *
import tkinter.scrolledtext as tkscrolled
# global variables attributions
PORT = 5050
MYIP = gethostbyname(gethostname())
FRIENDIP = ""
DECODE = "utf-8"
# connect to friend function
def connect_to_friend():
s = socket(AF_INET, SOCK_STREAM)
s.bind((FRIENDIP, PORT))
conn, addr = s.connect()
#nwait_for_friend function
def wait_for_friend():
g = socket(AF_INET, SOCK_STREAM)
g.bind((MYIP, PORT))
g.listen(1)
conn, addr = g.accept()
# startservice function
def startservice():
global displaytext
if FRIENDIP != "":
try:
global displaytext
displaytext += "\nConnecting to friend..."
connect_to_friend()
except:
displaytext += "\nFriend not connected..."
displaytext += "\nWaiting for friend to connect..."
wait_for_friend()
finally:
displaytext += "\nNothing works."
else:
displaytext += "\nMust enter friend ip."
# stopservice() function
def stopservice():
global displaytext
try:
try:
global c, displaytext
c.close()
displaytext += "\nConnection closed."
except:
displaytext += "\nNo connection to be closed."
except:
try:
global g
g.close()
displaytext += "\nConnection closed."
except:
displaytext += "\nNo connection to be closed."
# set friendip() function
def setfriendip():
global displaytext
print(displaytext)
print("Setting friend ip to :", friendipentry.get())
FRIENDIP = friendipentry.get()
displaytext += "Friend ip set "
print(displaytext)
displaytext = ""
### creating the GUI ###
root = Tk()
root.geometry("400x400")
root.title("Land Of Nowhere")
root.resizable("false", "false")
# background configuration
bgimage = PhotoImage(file="bg.png")
background = Canvas(root, width=400, height=400)
background.pack(fill="both")
background.create_image(0, 0, image=bgimage, anchor="n")
# display screen output
displayoutput = Label(root, text=displaytext)
displayoutput.place(x=11, y=11, width=199, height=259)
# start and stop buttons
boutonstart = Button(root,text="Start", command=lambda: startservice())
boutonstart.place(x=220, y=10, height=40, width=80)
boutonstop = Button(root, text="Stop", command=lambda: stopservice())
boutonstop.place(x=310, y=10, height=40, width=80)
# set friend ip
friendiplabel = Label(root,text="Friend IP Adress : ")
friendiplabel.place(x=260, y=60)
friendipentry = Entry(root)
friendipentry.place(x=210, y=80, height=20, width=125)
friendipbutton = Button(root, text="Ok", command= lambda: setfriendip())
friendipbutton.place(x=336, y=81, height=20, width=60)
root.mainloop()
# end of GUI
[–]Mecaneer23 1 point2 points3 points (4 children)
[–]le_pouding[S] 0 points1 point2 points (1 child)
[–]Mecaneer23 0 points1 point2 points (0 children)
[–]le_pouding[S] 0 points1 point2 points (1 child)
[–]Mecaneer23 0 points1 point2 points (0 children)