无法使用tkinter tk.TEXT显示消息

时间:2019-06-20 14:30:28

标签: python-3.x tkinter

我创建了类似于Pirch98的IRC。我已经用Python 3.7.3完成了。我已经在VB6和VS12中做到了这一点。现在,我有4个类模块:

  1. 创建主表单(tkinter)。
  2. 创建登录信息(tkinter)。
  3. 创建套接字。
  4. 创建服务器窗口(tkinter)。

这与Pirch98完全相同。

chanserv.py:

#!/usr/bin/python3

import tkinter as tk

class ChanServ:    
    def __init__(self, data):
       self.Server_data(data)

    def Server_data(self, data):   
        self.root = tk.Tk()
        self.root.title(data) 
        self.v = tk.StringVar()
        self.text1 = tk.Text(self.root, height=25, width=50, bg='black', fg='yellow')
        self.scroll = tk.Scrollbar(self.root, command=self.text1.yview, orient=tk.VERTICAL)
        self.scroll.config(command=self.text1.yview)
        self.text1.configure(yscrollcommand=self.scroll.set)
        self.entry = tk.Entry(self.root, textvariable=self.v, bg='black', fg='yellow')
        self.entry.pack(side=tk.BOTTOM, fill=tk.BOTH)
        self.entry.focus()
        self.text1.pack(side=tk.LEFT, fill=tk.BOTH, expand='YES')
        self.scroll.pack(side=tk.RIGHT, fill=tk.BOTH)
        #self.text1.insert(tk.END, data)
        #self.text1.insert(tk.END, '\n')

        def get(event):
            p =(self.entry.get())
            data = (str(p).split('/')[1])
            self.entry.delete(0, tk.END)
            self.Display_text(data)        
        self.entry.bind('<Return>', get)

    def Display_text(self, data):
        self.text1.insert(tk.END, data)
        self.text1.insert(tk.END, '\n')

        return data

def run():
    #ChanServ()
    tk.mainloop()              

ircconnection.py:

#!/usr/bin/env python3.
# This code was written for Python 3.7.3
#Date: 19th June, 2019

import socket
import sys
import threading
import time
import chanserv
import configparser
parser = configparser.ConfigParser()
parser.read('pychat.ini')

# Defining a class to run the server. One per connection. This class will do most of our work.
class IRC_Server:
    def __init__(self, host, port, nick, channel=None, password=None):
        self.irc_host = host
        self.irc_port = port
        self.irc_nick = nick
        self.irc_channel = channel
        self.irc_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.is_connected = False
        self.should_reconnect = False
        self.command = None
        self.ChanServ = chanserv.ChanServ('%s on %s' % (self.irc_nick, self.irc_host))

    def connect(self):
        self.should_reconnect = True
        self.user_info()

        try:
            self.irc_sock.connect((self.irc_host, self.irc_port))
        except:
            _error = ('Error: Could not connect to IRC; Host: ' + str(self.irc_host) + 'Port: ' + str(self.irc_port))
            self.ChanServ.Display_text(_error)
            exit(1) # We should make it recconect if it gets an error here            

        _nick = ('NICK %s \r\n') % (self.irc_nick)
        self.irc_sock.send(bytes(_nick, 'UTF-8'))

        _user = ('USER %s %s 0, *, :%s\r\n' % (self.irc_host, self.irc_nick,self.irc_nick))
        self.irc_sock.send(bytes(_user, 'UTF-8'))

            mw = parser.get('User', 'mw')
            if (int(mw)) == 1:
                self.irc_sock.send(bytes('MODE ' + self.irc_nick + ' +w', 'UTF-8'))

            ms = parser.get('User', 'ms')
            self.irc_sock.send(bytes('MODE ' + self.irc_nick + ' +r',  'UTF-8'))

            mi = parser.get('User', 'mi')
            self.irc_sock.send(bytes('MODE ' + self.irc_nick + ' +i', 'UTF-8'))                

        self.is_connected = True         
        self.listen()     

    def listen(self):
        while self.is_connected:
            recv = self.irc_sock.recv(4096)                  
            lines = (recv).split(bytes('\r\n', 'UTF-8'))

            for data in lines:
                data = str(data).strip()
                data = data[1:][:-1]
                print(data)

            if str(recv).find('PING') != -1:
                self.irc_sock.send(bytes('PONG %s\r\n' % recv.split()[1], 'UTF-8')) 

        if self.is_should_reconnect:
            self.connect()

    def user_info(self):
        self.ChanServ = chanserv.ChanServ('%s on %s' % (self.irc_nick, self.irc_host))
        _connect = "Attempting to connect to " + str(self.irc_host) + ":" + str(self.irc_port)
        self.ChanServ.Display_text(_connect)
        _host = 'Host IP is %s' % socket.gethostbyname(self.irc_host)
        self.ChanServ.Display_text(_host)
        _Your_IP = 'Your IP is %s' % socket.gethostbyname(socket.gethostname())
        self.ChanServ.Display_text(_Your_IP)
        _local_host = 'Local Host is %s' % socket.gethostname()
        self.ChanServ.Display_text(_local_host)

    def notice_auth(self, data):
        data = data[data.find('NOTICE AUTH'):len(data)]
        data = str(data)
        self.ChanSer.Display_text(data)

        return data

    def QUOTE_PONG(self, data):
        data = data[data.find('/QUOTE PONG'):len(data)]
        data = str(data)

        return data

    def chanserv_message(self, data):
        data = data[data.find(self.irc_nick)+1:len(data)]
        data = data[data.find(' ')+1:len(data)]
        data = str(data[0:len(data)])
        print(data)
        self.ChanServ.Display_text(data)

        return data

    def server_mode(self, data):
        data = data[data.find(':')+1:len(data)]
        data = data[data.find(' ')+4:len(data)]
        data = str(data[1:len(data)])
        _mode = 'Your client mode is modified'

        return data

nick = parser.get('User', 'nick1').strip()
irc = parser.get('User', 'serverpref')
server = irc.split(':')[0] 
port = irc[len(server)+1:][0:4] 
_channel = ''

#Start connecting
test = IRC_Server(server, int(port), nick, _channel)

def run():
    run_test = threading.Thread(None, test.connect)
    run_test.start()
    while(test.should_reconnect):
        chanserv.run()
        time.sleep(5)       

如果我从#3和#4进行了2节课,则一切正常,并且消息可以显示在tkinter tk.TEXT上。现在,如果我执行4个类模块,则会收到一条错误消息,指出TypeError:Display_text()缺少1个必需的位置参数:“ data”。

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Users\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Documents\pyIRC20\irc_connection4.py", line 56, in connect
    self.listen()
  File "C:\Users\Documents\pyIRC20\irc_connection4.py", line 70, in listen
    self.chanserv_message(str(data))
  File "C:\Users\Documents\pyIRC20\irc_connection4.py", line 112, in chanserv_message
    _ChanServ.ChanServ.Display_text(data)
TypeError: Display_text() missing 1 required positional argument: 'data'

我仍然得到空白的Tkinter TEXT。

屏幕截图:

Here is screenshot

0 个答案:

没有答案