Niloofar24 15 Posting Whiz

Well, i tried that. Yes, the function will be run and the Good morning message will be displayed on the shell, but i want the message to be displayed on the TK window not on the terminal. I mean to be displayed on the pop up little window which Tkinter has created.

Niloofar24 15 Posting Whiz

That is the main code without adding your code.

Niloofar24 15 Posting Whiz
#!/usr/bin/env python3
# -*-coding: utf8-*-

filename = 'films.db'
target = open (filename, 'a')

import pickle

favorite_movies=[]

favorite_movies = pickle.load( open("films.db", "rb"))

def append():
    import sys
    movie_name = str(sys.stdin.readline())
    favorite_movies = pickle.load( open("films.db", "rb"))
    favorite_movies.append(movie_name)
    pickle.dump(favorite_movies, open("films.db", "wb"))
    print (favorite_movies)
def sort():
    favorite_movies.sort()
    print (favorite_movies)
def view():
    favorite_movies = pickle.load( open("films.db", "rb"))
    print (favorite_movies)

if __name__ == "__main__":

    import argparse
    parser = argparse.ArgumentParser(description='Execute a function')
    parser.add_argument('funcname',
    help='name of function to execute',
    metavar='FUNCNAME',
    choices=['view' , 'sort' , 'append'])
args = parser.parse_args()
function = globals()[args.funcname]
function()
Niloofar24 15 Posting Whiz

Let mee add sth!!
Here you see tesst.py on the error message, it's the same as favemovies.py i'm working on it. I just use test.py with the same body for changing and testing codes.

Niloofar24 15 Posting Whiz

Well, i added your code to the end of my code, but this is the error i got:

Traceback (most recent call last):
  File "test.py", line 12, in <module>
    favorite_movies = pickle.load( open("films.db", "rb"))
IOError: [Errno 2] No such file or directory: 'films.db'
Niloofar24 15 Posting Whiz

Well, here is a question for me....
Where should i put your codes?
All of them above the first functio?
I mean the first function must start from the line number 22?!

Niloofar24 15 Posting Whiz

It didn't work unfortunately.
Here is the error i got:

Traceback (most recent call last):
  File "niloofar.py", line 5, in <module>
    tk.message()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1848, in __getattr__
    return getattr(self.tk, attr)
AttributeError: message
Niloofar24 15 Posting Whiz

Hello everybody! Me again with an other question :)
I have a file with a list and 3 functions in it; view, sort, append.
I used pickle to save data changes in to an other file (films.db).
Imagine you want to use this programe for the first time on your system so there is no db file correct?
You should type python favemoviese.py view or favemovies.py append.... At this time the script must creat the db file immidiately beacuse there was no primary db file, right?

And then you should be able to append your word to the list because the db file has been created jaut a few seconds ago.
But when i do type python favemovies.py append or view, an error appears that say there is no db file to load data from, and after that an empty db file will be created immidiately.
Why? How can i create the db file first.

Really orry for long discribtion!! I just didnt know how to explaine the problem, just wanted to make it clear.

I've type this above all functions:

    #!/usr/bin/env python3
    # -*-coding: utf8-*-

    filename = 'films.db'
    target = open (filename, 'a')


    import pickle

    favorite_movies=[]

    favorite_movies = pickle.load( open("films.db", "rb"))

    def append():

I want filename and target work first to create the db file but the programe reads the pickle.load line first.

Niloofar24 15 Posting Whiz

Hi everybody!
How can i call a function from Tkinter without using Buttons?
I have a test.py file and there is a function def message in it:

def message():
    print('Good morning')

from Tkinter import *

tk = Tk()

tk.mainloop()

I want to type test.py message from linux terminal and the good morning message display on a window.
What should i do?
I dont want to use button.
What should i type between line 6 and 8?

Niloofar24 15 Posting Whiz

Well, i solved the problem myself finally :)

Niloofar24 15 Posting Whiz

Hello!
I have a file with a list and 3 functions in it; view, append and sort functions.
I want to use pickle to save data changes in to another file inorder to retrive it next time.
I have a list, when i call function VIEW, i see the primary list i created. When i call APPEND function, i can append a new word and see it with print in that function. But if i call VIEW function again, i see the primary list again without any data changes (the append word).

I know i should use:

import pickle

pickle.dump(favorite_movies, open("films.db", "wb"))

favorite_movies = pickle.load( open("films.db", "rb"))

Correct?!

Now my problem is that where should i type pickle.dump and pickle.load in the main file.
In the first line above all codes?
Inside each function?
Or somewhere in this part:

if __name__ == "__main__":

    import argparse
    parser = argparse.ArgumentParser(description='Execute a function')
    parser.add_argument('funcname',
    help='name of function to execute',
    metavar='FUNCNAME',
    choices=['view' , 'sort' , 'append'])
args = parser.parse_args()
function = globals()[args.funcname]
function()

I'm almost new to programming, so this is the problem i have now with this little project.

Niloofar24 15 Posting Whiz

Well, where should i save these codes? In the main file? Or in an other file with any special name.txt ?

Niloofar24 15 Posting Whiz

I have a file with the name message.py and i have created some functions in it. I have created a list and some functions to be able to append, sort or remove names to my list.
Now i want to save data into my hard disk in order to keep changes. For example i want to append a name to the list and close the program and next time i want changes to be saved to access them again.
I just know that i should use a text file as database.
Now what should i do?

Niloofar24 15 Posting Whiz

Hi everybody.
How can i creat e text file to use as a database in python?
I want to save my project's data changes into hard disk. What should i do?

Niloofar24 15 Posting Whiz

Yesssssssssss!! It works Gribouillis thank you so much :)
I've created some other functions and all of them works separately, nice!
______________
Thanks everybody for all answers.

Niloofar24 15 Posting Whiz

Yes! From Linux's terminal in python language.
Sorry maybe i couldn't explain good, well my English is not very good. So....let me make it clear.
I want to type this command python message.py sayhi for example, from the Linux terminal.

Niloofar24 15 Posting Whiz

Yes. I want when i type python message.py sayhi, the function sayhi run and when type python message.py goodbye, the function goodbye run separately.

In fact; i want to have some different functions in message.py file and call them separately everytime by typing python message.py (the name of the function).

Niloofar24 15 Posting Whiz

oopss!! both*

Niloofar24 15 Posting Whiz

Slavi, i copied your code, but when i type python message.py sayhi bout print messages will be displayed! 'hi everybody!' and 'see you soon' bout will be displayed to gather at the same time.

Niloofar24 15 Posting Whiz

Both of them*

Niloofar24 15 Posting Whiz

Thank you Gribouillis, i copied the code you wrote and it worked with python message.py sayhi.

Now for working with more functions what should i do?
Please look at this:

#!/usr/bin/env python3
# -*-coding: utf8-*-

def sayhi():
    print('hi everybody!')

def goodbye():
    print('see you soon!')

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description='Execute a function')
    parser.add_argument('funcname',
    help='name of function to execute',
    metavar='FUNCNAME',
    choices=['sayhi'])
args = parser.parse_args()
function = globals()[args.funcname]
function()


if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description='Execute a function')
    parser.add_argument('funcname',
    help='name of function to execute',
    metavar='FUNCNAME',
    choices=['goodbye'])
args = parser.parse_args()
function = globals()[args.funcname]
function()

Here i've added an other function goodbye.
When i type python message.py sayhi and python message.py goodbye, just one of them will work. How can i call noth of them separately? How should i change the code above?
_______________
Thanks everybody for all answers :)

Niloofar24 15 Posting Whiz

Thank you for your answer.
Here is what i have; a file with name message.py:

def sayhi():
    print('hi everybody!')


if __name__ == "__main__":
    sayhi(int(argv[0]))

Well it's a little hard to explain what i mean....i want to type this for example python message.py sayhi on the shell and run the function.

In fact; i want to create some functions on message.py file and then call each of them separately from the terminal.

Niloofar24 15 Posting Whiz

Hi everybody. I've created a .py file with a simple function in it. Now i want to call that function from linux shell scripting. How should i do that? please help me, thank you.