I made a simple Chat Application using WPF (as GUI toolkit) and IronPython. Here is the source.
The XAML
Code: Select all
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" SizeToContent="Height"
Width="505" Height="200" MinHeight="200" MinWidth="505" ResizeMode="NoResize">
<StackPanel Orientation="Vertical" MinHeight="180" MinWidth="505" MaxHeight="180" MaxWidth="505">
<StackPanel Orientation="Horizontal" MinHeight="40" MinWidth="505" MaxHeight="40" MaxWidth="505">
<TextBox x:Name="ip_address" FontSize="16" MinHeight="40" MinWidth="500" MaxHeight="40" MaxWidth="500">127.0.0.1</TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal" MinHeight="100" MinWidth="500" MaxHeight="100" MaxWidth="500">
<TextBox TextWrapping="Wrap" x:Name="textbox" FontSize="12" MinHeight="100" MinWidth="500"
MaxHeight="100" MaxWidth="500"
ScrollViewer.VerticalScrollBarVisibility="Visible"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal" MinHeight="40" MinWidth="400" MaxHeight="40" MaxWidth="505">
<TextBox TextWrapping="Wrap" x:Name="entry" FontSize="12" ScrollViewer.VerticalScrollBarVisibility="Visible"
Height="40" MinHeight="40" MinWidth="400"
MaxHeight="40" MaxWidth="400"
AcceptsReturn="True"></TextBox>
<Button x:Name="send" Width="100" MinHeight="40" MinWidth="100" FontSize="12">Send</Button>
</StackPanel>
</StackPanel>
</Window>
Code: Select all
import clr
clr.AddReference("PresentationFramework")
clr.AddReference("PresentationCore")
from System.IO import File
from System.Windows.Markup import XamlReader
from System.Windows import *
import socket
from threading import *
#import cPickle
#CMD_MSG = range(1)
class Chat(object):
def __init__(self):
mythread = Thread(target=self.server)
mythread.start()
stream = File.OpenRead("p2pChat.xaml")
self.root = XamlReader.Load(stream)
self.ip_address_input = self.root.FindName('ip_address')
self.sendB = self.root.FindName('send')
self.output = self.root.FindName('textbox')
self.entry_input = self.root.FindName('entry')
self.sendB.Click += self.sendit
def server(self):
self.port = 9000
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.bind((socket.gethostbyname(socket.gethostname()), int(self.port)))
#print socket.gethostbyname(socket.gethostname())
while True:
msg, addr = self.s.recvfrom(2024)
## cmd, msg = ord(msg[0]),msg[1:]
## if cmd == CMD_MSG:
self.output.Text += (msg + "\n")
def client(self, msg):
self.port = 9000
self.host = self.ip_address_input.Text
self.se = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.se.sendto(str(self.host) + " : " + msg, (self.host, int(self.port)))
def sendit(self, s, e):
self.client(self.entry_input.Text)
self.output.Text += (self.entry_input.Text + "\n")
self.entry_input.Text = ""
myChat = Chat()
app = Application()
app.Run(myChat.root)
Maboroshi