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?

I think you can do something like this.

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

from Tkinter import *
tk = Tk()
tk.message()
tk.mainloop()

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

Try:

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

from Tkinter import *
tk = Tk()
message()
tk.mainloop()

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.

Then you have to change your code to:

def message():
    label['text'] = 'Good morning'

# for Python3 replace Tkinter with tkinter
import Tkinter as tk

win = tk.Tk()

label = tk.Label(win)
label.pack()
message()

win.mainloop()

Yes it works @sneekula, thank you so much for your help :)

One more question.
I have changed the Good morning sentence with print (favorite_movies), it doesn't work i know its not correct, so how can i call this function and print the list on pop up winow? I also used the Tkinter code you gave me and repladed the message() part with view().

def view():
    favorite_movies = pickle.load( open("films.db", "rb"))
    print (favorite_movies)
    label['text'] = print (favorite_movies)

favorite_movies = pickle.load( open("films.db", "rb"))
will give you a Python object like a list or dictionary, whatever you saved. You would have to process it to give you a meaningfull string you can display with label['text'] = favorite_movies_string

SO, what should i do now?! I didn't understand what should i do exactly.

Let's assume your favorite_movies is a list object with movie titles ...

# test list
favorite_movies = [
"Gone with the Wind",
"Batman IV",
"Spiderman VII",
"Sniper III"]

favorite_movies_string = "\n".join(favorite_movies)
print(favorite_movies_string)  # test

Well, with the code above, the names will be displayed on terminal window.
But i want when i type python favemovies.py view the view function run and the names of the favorite_movies display on a Tkinter pop up window.
I dont know how to do that.

from ScrolledText import *
from Tkinter import *

main = Tk()
mywin = ScrolledText(main)
mywin.pack(fill=BOTH, expand=YES)

main.title('Favorite Movies')
favorite_movies = [
"Gone with the Wind",
"Batman IV",
"Spiderman VII",
"Sniper III"]
mywin.insert(END,'\n'.join(favorite_movies))
mywin.focus_set()
mywin.mainloop()

Try it this way:

''' favemovies.py

with iPython on Windows use:
run C:\Python27\Atest27\Bull\favemovies.py view
'''

import sys
import pickle
# for Python3 replace Tkinter with tkinter
import tkinter as tk

def view():
    favorite_movies = pickle.load( open("films.db", "rb"))
    favorite_movies_string = "\n".join(favorite_movies)
    label['text'] = favorite_movies_string


win = tk.Tk()

label = tk.Label(win, bg='yellow')
label.pack()
if len(sys.argv) > 1:
    if sys.argv[1] == "view":
        view()
else:
    label['text'] = " usage: python favemovies.py view "

win.mainloop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.