I have two classes, one which handles the GUI = GUI()
, and one that handles the Client connection to my server = Client()
. So, my error report would be:
TypeError: change_state() takes exactly 2 arguments (1 given)
when having a self argument within change_state() in GUI(), and calling GUI.change_state('NORMAL') from Client().
But when not having self as an argument within change_state()
, I can't call other functions within the GUI class by using self. And changing GUI.change_state()
to GUI().change_state
would call the __init__
method on GUI everytime.
I hope you understood a tiny bit of what I meant
Code:
#!/usr/bin/python
import socket
import time
import tkinter
import threading
from tkinter import *root = Tk()
root.geometry("363x200")
root.resizable(0,0)
root.title("Emsg")
b1 = None
b2 = None
class Client:def __init__(self, host='localhost', port=5000):
## global b1
## global b2
## global b3try:self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)self.server_socket.connect((host, port))time.sleep(0.5)GUI.set_info("Connected...")self.boool = Trueself.gotten = ""self.data = ""GUI.change_state('NORMAL') <<<<<<<<< Problemthreading.Timer(1.0, self.listen).start()except socket.error as error:self.boool = FalseGUI.set_info(str(error)[14:])
## except:
## self.boool = False
## GUI.set_info("An unknown error occured")def leave(self):self.boool = Falseself.server_socket.close()GUI.change_state('DISABLED')GUI.set_info("You have left the chat")GUI.set_v("", "")self.data = ""def listen(self): while self.boool:try:time.sleep(0.1)self.data = self.server_socket.recv(512)if self.data == bytes('quit', 'UTF-8'):self.leave()GUI.set_info("Server has left the chat")GUI.set_v("", "")elif self.data != self.gotten:GUI.set_v("Server:", self.data, True)self.gotten = self.dataexcept socket.error:self.leave()def send(self, message=''):self.server_socket.send(bytes(message, 'UTF-8'))GUI.set_v("Client:", message)GUI.reset_cmsg()class GUI:def __init__(self):self.client = Noneself.setup_buttons()def setup_buttons(self):global b1global b2global b3b1 = Button(root, text="Send", width=8, state=DISABLED, command=self.send)b1.grid(row=0, column=2)b2 = Button(root, text="Leave", width=8, state=DISABLED, command=self.leave)b2.grid(row=0, column=3)b3 = Button(root, text = "Connect", width = 8, command = self.connect)b3.grid(row = 0, column = 4)def connect(self):self.client = Client()self.first = Truedef leave(self):if self.client:self.client.leave()def send(self):if self.client:self.client.send(textEntry.get())def set_v(name, value, encoding=False):if encoding:v.set("%s: %s\n%s" % (name, str(value, 'UTF-8'), v.get()))else:v.set("%s: %s\n%s" % (name, value, v.get()))if name == "":v.set("")def change_state(self, _state):global b1global b2global b3if _state == 'DISABLED':b1.config(state=DISABLED)b2.config(state=DISABLED)b3.config(command=self.connect) <<<<<<<< Problem if changedto GUI().connectb3.config(text="Connect")textEntry.config(state=DISABLED)elif _state == 'NORMAL':b1.config(state=NORMAL)b2.config(state=NORMAL)b3.config(command=self.leave)b3.config(text="Disconnect")textEntry.config(state=NORMAL)def set_info(value):info.set(value)def reset_cmsg():c_msg.set("")v = StringVar()
info = StringVar()
c_msg = StringVar()Label(root, text="Enter: ").grid(row=0, column=0)
textEntry = Entry(root, state=DISABLED, textvariable=c_msg)
textEntry.grid(row=0, column=1)
statusField = Label(root, textvariable=info, wraplength=200)
statusField.grid(row=1, column=0, columnspan=5, sticky="w")
msgField = Message(root, textvariable=v, width=330, fg="blue")
msgField.grid(row=2, column=0, columnspan=5, sticky="w")GUI()root.mainloop()