Chat App in IronPython WPF

Questions about programming languages and debugging
Post Reply
User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Chat App in IronPython WPF

Post by maboroshi »

I have been getting into WPF as my GUI Toolkit, I do like it there are some annoyances but over all its cool.

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> 
And the Python

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)
Enjoy

Maboroshi

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Re: Chat App in IronPython WPF

Post by bad_brain »

sweet... :D
I remember we did lots of tests with different versions, I'm glad it worked out with IronPYthon.... :D
Image

Post Reply