File Transfer Need Help

A

ADE

Hi everyone well from my last post I found what I am looking for

I have some code now that transfers files

I have added a GUI to it and need some help with two things one my
loadtemplate() function if I run it won't open the filetypes I just get the
error message from tkMessageBox but its the right filetype

and the final problem the Variable FILE how can I get it to interact with
GUI once I select an image to send how can I use the selected File to send
using the sendfile function

this is as far as I know the last of my problems

please note that this code has been taken from different parts of the
internet

Cheers


# USAGE: python FileSender.py [file]
from Tkinter import *
import tkFileDialog
import tkMessageBox
import sys, socket

root = Tk()
root.title("My APP")
root.minsize(300, 200)
root.maxsize(500, 400)


def loadtemplate():
filename = tkFileDialog.askopenfilename(filetypes=(("TIFF", ".tiff"),
("JPG", ".jpg"),
("GIF", ".gif")))
if filename:
try:
self.settings["template"].set(filename)
except:
tkMessageBox.showerror("Open Source File", "Failed to read file
\n'%s' " %filename)
return


HOST = 'localhost'
CPORT = 9091
MPORT = 9090
FILE = # I need help here what do I put here


def sendfile():
cs = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
cs.connect((HOST, CPORT))
cs.send("SEND " + FILE)
cs.close()

ms = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ms.connect((HOST, MPORT))

f = open(FILE, "rb")
data = f.read()
f.close()
textbox.insert("Sending File Now...")
ms.send(data)
ms.close()
textbox.insert("File Sent now Closing")

mybrowse = Button(root, text="Browse", command=loadtemplate)
mybrowse.grid(row=0, column=0, sticky=E+W)
mybutton = Button(root, text="Send File", command=sendfile)
mybutton.grid(row=0, column=1, sticky=E+W)
textbox = Text(root)
textbox.grid(row=1, column=0, columnspan=2)
textbox.insert(END, "Depending on size of File this could take a while\n\n")
 
V

vincent wehren

ADE said:
Hi everyone well from my last post I found what I am looking for

I have some code now that transfers files

I have added a GUI to it and need some help with two things one my
loadtemplate() function if I run it won't open the filetypes I just get the
error message from tkMessageBox but its the right filetype

The problem is the line: self.settings["template"].set(filename)
This line is out of context. You're not within a class with a "member
variable" called settings. What are you trying to accomplish?

Aren't you looking for somthing like:

....
filename = tkFileDialog.askopenfilename(filetypes=(
("TIFF", ".tiff"), ("JPG", ".jpg"),
("GIF", ".gif")))
try:
sendfile(filename)
except:
tkMessageBox.showerror("Some error sending %" % filename)
....

Of course to do so, you need to change the sendfile() function to a
function that takes 'filename' as argument. As a result, there's no
need for a global variable FILE anymore -- which may solve your 2nd problem:

def sendfile(filename):
...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top