python - How do I center the random output from my list? -
from tkinter import * import random pil import image movies = (open('c:\users\me\desktop\projects\movies.txt').readlines()) mymovie = (movies) print(mymovie) def pickmovie(): movielabel.configure(text=random.choice(movies)) #gui window root = tk() root.title('movie randomizer') root.geometry('900x800') #picture photo = photoimage(file='c:\users\sivang\desktop\projects\popcorn.gif') label = label(image=photo) label.image = photo # reference bitch label.pack() #movie label movielabel = label(root, text="", font=('times new roman', 28)) movielabel.pack() #pick movie pickbutton = button(text="pick!", fg="red", bg="white", command=pickmovie) pickbutton.pack(side='bottom', padx = 5, pady = 25) #start gui root.mainloop()
my question refers following code:
movies = (open('c:\users\me\desktop\projects\movies.txt').readlines()) mymovie = (movies) print(mymovie)
whenever run program, text isn't in center. question how keep in center.
using str.format
can specify right, left or centered alignment, see format specification mini-language.
example:
movies = "star wars\nsome awesome movie\nanother great movie\nmeh movie" >>> print movies star wars awesome movie great movie meh movie
now using .format
, specifying center aligned specific width get:
>>> print '\n'.join('{:^50}'.format(s) s in movies.split('\n')) star wars awesome movie great movie meh movie
you can add flair:
>>> print '\n'.join('{:-^50}'.format(s) s in movies.split('\n')) --------------------star wars--------------------- ----------------some awesome movie---------------- ---------------another great movie---------------- --------------------meh movie---------------------
you'll have play around width ensure strings centered.
Comments
Post a Comment