Tkinter List of Canvases

2020-11-10 Thread ChalaoAdda
Hello,

I have a list of canvas of images. I would like to display all the images. But 
it displays the last image. Here is my code.

from tkinter import *
from PIL import Image, ImageTk

root = Tk()

canvas_width = 265
canvas_height = 130
canvases = []

r, c = 0, 0

for tile in tiles:
img = tile.image
photo = ImageTk.PhotoImage(image=img)
can = Canvas(root, width=canvas_width, height=canvas_height)
can.create_image(0, 0, image=photo, anchor=NW)
canvases.append(can)
can.grid(row=r, column=c)

   c += 1
if c % 5 == 0:
r += 1
c = 0


root.mainloop()

Please help.
Thanks.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Tkinter List of Canvases

2020-11-10 Thread ChalaoAdda
On Tue, 10 Nov 2020 17:21:04 +, MRAB wrote:

> On 2020-11-10 10:01, ChalaoAdda wrote:
>> Hello,
>> 
>> I have a list of canvas of images. I would like to display all the
>> images. But it displays the last image. Here is my code.
>> 
>> from tkinter import *
>> from PIL import Image, ImageTk
>> 
>> root = Tk()
>> 
>> canvas_width = 265 canvas_height = 130 canvases = []
>> 
>> r, c = 0, 0
>> 
>> for tile in tiles:
>>  img = tile.image photo = ImageTk.PhotoImage(image=img)
>>  can = Canvas(root, width=canvas_width, height=canvas_height)
>>  can.create_image(0, 0, image=photo, anchor=NW)
>>  canvases.append(can)
>>  can.grid(row=r, column=c)
>> 
>> c += 1
>>  if c % 5 == 0:
>>  r += 1 c = 0
>> 
>> 
>> root.mainloop()
>> 
> You need to keep a reference to the images. In your code, 'photo' still
> refers to the last photoimage, so that's why it's displayed; there are
> no references to the others, so they're garbage-collected. (You probably
> expected that 'create_image' would keep a reference to the image, but it
> doesn't. tkinter has its idiosyncrasies!)
> 
> Try adding a list for the photoimages:
> 
> ...
> photos = []
> 
> for tile in tiles:
>  img = tile.image photo = ImageTk.PhotoImage(image=img)
>  photos.append(photo)
>  can = Canvas(root, width=canvas_width, height=canvas_height)
> ...

Thanks a lot.

-- 
https://mail.python.org/mailman/listinfo/python-list


tkinter global variable

2020-11-13 Thread ChalaoAdda
Hello,

I am trying to read a file from askopenfilename outside the function. I 
am getting blank file name. What am I doing wrong? Here is my code.

from tkinter import *
from tkinter import filedialog

def on_openfile():
global pic
pic = filedialog.askopenfilename()


root = Tk()

menubar = Menu(root)
root.config(menu=menubar)
file_menu = Menu(menubar)
file_menu.add_command(label="Open", command=on_openfile)
file_menu.add_command(label="Exit", command=root.destroy)
menubar.add_cascade(label="File", menu=file_menu)


f = open(pic)
print(f.read())

root.mainloop()


Thanks.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: tkinter global variable

2020-11-13 Thread ChalaoAdda
On Fri, 13 Nov 2020 15:41:20 +, Stefan Ram wrote:

> ChalaoAdda  writes:
>>I am trying to read a file from askopenfilename outside the function. I
>>am getting blank file name. What am I doing wrong? Here is my code.
> 
>   It is possible that you try to read from "pic" before "on_openfile"
>   ever was executed.

I didn't get you. I click on the menu item "Open" and then from the 
filedialog select a file. And then outside that function I am trying to 
read and print the file.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: tkinter global variable

2020-11-13 Thread ChalaoAdda
On Fri, 13 Nov 2020 16:04:03 +, Stefan Ram wrote:

> ChalaoAdda  writes:
>>On Fri, 13 Nov 2020 15:41:20 +, Stefan Ram wrote:
>>> ChalaoAdda  writes:
>>>>I am trying to read a file from askopenfilename outside the function.
>>>>I am getting blank file name. What am I doing wrong? Here is my code.
>>>It is possible that you try to read from "pic" before "on_openfile"
>>>ever was executed.
>>I didn't get you. I click on the menu item "Open" and then from the
>>filedialog select a file. And then outside that function I am trying to
>>read and print the file.
> 
>   I have added two additional statements to your source code:
>   print( "A" ) and print( "B" ).
> 
>   If you now execute this new source code, you might observe that "B" is
>   being printed before "A" is being printed.
> 
> from tkinter import *
> from tkinter import filedialog
> 
> def on_openfile():
> print( "A" )
> global pic pic = filedialog.askopenfilename()
> 
> 
> root = Tk()
> 
> menubar = Menu(root)
> root.config(menu=menubar)
> file_menu = Menu(menubar) file_menu.add_command(label="Open",
> command=on_openfile) file_menu.add_command(label="Exit",
> command=root.destroy)
> menubar.add_cascade(label="File", menu=file_menu)
> 
> 
> print( "B" )
> f = open(pic)
> print(f.read())
> 
> root.mainloop()

Ok. I got the point. So what do I need to do access the variable? How do 
I return a value from that function?

Thanks.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: tkinter global variable

2020-11-13 Thread ChalaoAdda
On Fri, 13 Nov 2020 12:04:53 -0500, Richard Damon wrote:

> On 11/13/20 11:42 AM, ChalaoAdda wrote:
>> On Fri, 13 Nov 2020 16:04:03 +, Stefan Ram wrote:
>>
>>> ChalaoAdda  writes:
>>>> On Fri, 13 Nov 2020 15:41:20 +, Stefan Ram wrote:
>>>>> ChalaoAdda  writes:
>>>>>> I am trying to read a file from askopenfilename outside the
>>>>>> function.
>>>>>> I am getting blank file name. What am I doing wrong? Here is my
>>>>>> code.
>>>>> It is possible that you try to read from "pic" before "on_openfile"
>>>>> ever was executed.
>>>> I didn't get you. I click on the menu item "Open" and then from the
>>>> filedialog select a file. And then outside that function I am trying
>>>> to read and print the file.
>>>   I have added two additional statements to your source code:
>>>   print( "A" ) and print( "B" ).
>>>
>>>   If you now execute this new source code, you might observe that "B"
>>>   is being printed before "A" is being printed.
>>>
>>> from tkinter import *
>>> from tkinter import filedialog
>>>
>>> def on_openfile():
>>> print( "A" )
>>> global pic pic = filedialog.askopenfilename()
>>>
>>>
>>> root = Tk()
>>>
>>> menubar = Menu(root)
>>> root.config(menu=menubar)
>>> file_menu = Menu(menubar) file_menu.add_command(label="Open",
>>> command=on_openfile) file_menu.add_command(label="Exit",
>>> command=root.destroy)
>>> menubar.add_cascade(label="File", menu=file_menu)
>>>
>>>
>>> print( "B" )
>>> f = open(pic)
>>> print(f.read())
>>>
>>> root.mainloop()
>> Ok. I got the point. So what do I need to do access the variable? How
>> do I return a value from that function?
>>
>> Thanks.
> 
> The problem is that you are accessing the variable BEFORE the box has
> been put up and the user clicking on it. That doesn't happen until the
> mainloop() call. You need to delay the opening and reading of the file
> till the filedialog has been used and returned.
> 
> Perhaps on_openfile could open and read the file.

Ok. Then how do I access the content of the file from outside the 
function? How can I access return value?
Thanks.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: tkinter global variable

2020-11-13 Thread ChalaoAdda
On Fri, 13 Nov 2020 18:40:53 +0100, David Kolovratník wrote:

> On Fri, Nov 13, 2020 at 05:12:10PM +0000, ChalaoAdda wrote:
>> On Fri, 13 Nov 2020 12:04:53 -0500, Richard Damon wrote:
>> 
>> > On 11/13/20 11:42 AM, ChalaoAdda wrote:
>> >> On Fri, 13 Nov 2020 16:04:03 +0000, Stefan Ram wrote:
>> >>
>> >>> ChalaoAdda  writes:
>> >>>> On Fri, 13 Nov 2020 15:41:20 +, Stefan Ram wrote:
>> >>>>> ChalaoAdda  writes:
>> >>>>>> I am trying to read a file from askopenfilename outside the
>> >>>>>> function.
>> >>>>>> I am getting blank file name. What am I doing wrong? Here is my
>> >>>>>> code.
>> >>>>> It is possible that you try to read from "pic" before
>> >>>>> "on_openfile"
>> >>>>> ever was executed.
>> >>>> I didn't get you. I click on the menu item "Open" and then from
>> >>>> the filedialog select a file. And then outside that function I am
>> >>>> trying to read and print the file.
>> >>>   I have added two additional statements to your source code:
>> >>>   print( "A" ) and print( "B" ).
>> >>>
>> >>>   If you now execute this new source code, you might observe that
>> >>>   "B"
>> >>>   is being printed before "A" is being printed.
>> >>>
>> >>> from tkinter import *
>> >>> from tkinter import filedialog
>> >>>
>> >>> def on_openfile():
>> >>> print( "A" )
>> >>> global pic pic = filedialog.askopenfilename()
>> >>>
>> >>>
>> >>> root = Tk()
>> >>>
>> >>> menubar = Menu(root)
>> >>> root.config(menu=menubar)
>> >>> file_menu = Menu(menubar) file_menu.add_command(label="Open",
>> >>> command=on_openfile) file_menu.add_command(label="Exit",
>> >>> command=root.destroy)
>> >>> menubar.add_cascade(label="File", menu=file_menu)
>> >>>
>> >>>
>> >>> print( "B" )
>> >>> f = open(pic)
>> >>> print(f.read())
>> >>>
>> >>> root.mainloop()
>> >> Ok. I got the point. So what do I need to do access the variable?
>> >> How do I return a value from that function?
>> >>
>> >> Thanks.
>> > 
>> > The problem is that you are accessing the variable BEFORE the box has
>> > been put up and the user clicking on it. That doesn't happen until
>> > the mainloop() call. You need to delay the opening and reading of the
>> > file till the filedialog has been used and returned.
>> > 
>> > Perhaps on_openfile could open and read the file.
>> 
>> Ok. Then how do I access the content of the file from outside the
>> function? How can I access return value?
>> Thanks.
> What about
> 
> def on_printfilename():
> global pic try:
> print( f"C: {pic}" )
> except NameError:
> print( f"C! pic not set yet" )
> 
> together with
> 
> file_menu.add_command(label="Print filename", command=on_printfilename)
> 
> Or move your print("B") block behind the mainloop().
> 
> David

Thanks. That solved my problem.

Thanks.
-- 
https://mail.python.org/mailman/listinfo/python-list