d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Using Tkinter With Saving To A Text File
Add Reply New Topic New Poll
Member
Posts: 39,853
Joined: Apr 9 2009
Gold: 4,560.09
Dec 6 2014 09:26pm
What i want what i to do is


For Example


Click that image
Save the File
Text file should say the word "Pirate"


Here's what i used to make my pirate image pop-up to the window.


This post was edited by MelsonX on Dec 6 2014 09:27pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Member
Posts: 39,853
Joined: Apr 9 2009
Gold: 4,560.09
Dec 6 2014 09:35pm
Quote (carteblanche @ Dec 6 2014 08:32pm)


I get that part but not with the image
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 6 2014 09:57pm
if i understand you correctly, you want someone to click imagelabel and the open file dialog should appear, right? if i read the documentation correctly, your 'pirate' function should execute when it's clicked, and you should be able to open the file dialog from there. is that not happening? explaining what IS happening
Member
Posts: 39,853
Joined: Apr 9 2009
Gold: 4,560.09
Dec 6 2014 10:03pm
Quote (carteblanche @ Dec 6 2014 08:57pm)
if i understand you correctly, you want someone to click imagelabel and the open file dialog should appear, right? if i read the documentation correctly, your 'pirate' function should execute when it's clicked, and you should be able to open the file dialog from there. is that not happening? explaining what IS happening


It is working properly but now i want add this


From the image that i posted, when i click "Save".
It should input a word "Pirate" to a txt file

Like this



variable = open("champion.txt", "a")
???????? idk whats next
????????idk whats next


This post was edited by MelsonX on Dec 6 2014 10:05pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 6 2014 10:06pm
you tried the first link i posted and it didn't work? post your code and explain what errors you're getting.

Quote
variable = open("champion.txt", "a")
???????? idk whats next
????????idk whats next


did you read the answer from the first link? specifically, where it says "text2save"

This post was edited by carteblanche on Dec 6 2014 10:07pm
Member
Posts: 39,853
Joined: Apr 9 2009
Gold: 4,560.09
Dec 6 2014 10:09pm
Quote (carteblanche @ Dec 6 2014 09:06pm)
you tried the first link i posted and it didn't work? post your code and explain what errors you're getting.



did you read the answer from the first link? specifically, where it says "text2save"


nvm


Thanks for quick reply btw

This post was edited by MelsonX on Dec 6 2014 10:12pm
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Dec 7 2014 03:42am
How Python normally handles file objects should be something you read into, to get a general idea of it.

with acts as context manager and handles the open and close automatically, further reading: https://docs.python.org/3/library/stdtypes.html#typecontextmanager

Code
data = 'heh'

with open('file.txt', 'w') as f:
f.write(data)


or, another normal Python method for opening and closing, which is normally not used because with is better at handling opening and closing streams

Code
data = 'heh'
f = open('file.txt', 'w')
f.write(data)
f.close()


Tkinter has its own methods setup for it though, should work similarly. Here is something explaining in more detail the file handling aspect and methods: http://tkinter.unpythonic.net/wiki/tkFileDialog

asksaveasfile(mode='w', **options)

Code
import Tkinter, Tkconstants, tkFileDialog

class TkFileDialogExample(Tkinter.Frame):

def __init__(self, root):
Tkinter.Frame.__init__(self, root)

self.file_opt = options = {}

def asksaveasfile(self):
"""Returns an opened file in write mode."""

return tkFileDialog.asksaveasfile(mode='w', **self.file_opt)


If you want to browse the source of this, I found https://github.com/python/cpython/blob/master/Lib/tkinter/filedialog.py which seems to show all the details of how it is organized, you will notice it is a fancy rewrite of Python's open() with some functional fluff. :)
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll