From bryan.oakley at gmail.com Thu Feb 1 17:35:08 2018 From: bryan.oakley at gmail.com (Bryan Oakley) Date: Thu, 1 Feb 2018 16:35:08 -0600 Subject: [Tkinter-discuss] Simulating keystrokes in a tkinter unit test In-Reply-To: <808e0a5f-f96e-d482-2311-9682f56eaf90@alandmoore.com> References: <808e0a5f-f96e-d482-2311-9682f56eaf90@alandmoore.com> Message-ID: You might need to also generate a event and/or move the mouse over the widget with the warp option (widget.event_generate(..., warp=True), but you may need to also specify an x and y coordinate. I don't remember). I think some widgets check to see if they have focus, and will ignore keyboard events if they aren't focused. At least, I vaguely recall having done that a decade or two ago when I tried writing a tcl/tk testing harness. On Wed, Jan 31, 2018 at 12:09 PM, alan moore wrote: > Hi all, > > I'm attempting to write a unit test for a custom widget using Python > unittest. I'm trying to simulate actual keystrokes in my unit test. > > Following the example from the tkinter test suite, I created a test that > is approximately this: > > from tkinter import Entry as MyWidget > from tkinter.test.support import AbstractTkTest > import unittest > > class TestMyWidget(AbstractTkTest, unittest.TestCase): > > def setUp(self): > super().setUp() > self.mywidget = self.create() > self.mywidget.wait_visibility() > > def tearDown(self): > super().tearDown() > self.mywidget.destroy() > > def create(self): > mw = MyWidget(self.root) > mw.pack() > return mw > > def _keystroke(self, char): > self.mywidget.event_generate(''.format(char)) > self.mywidget.event_generate(''.format(char)) > self.mywidget.update_idletasks() > > def test_key_entry(self): > self._keystroke('a') > self.assertEqual(self.mywidget.get(), 'a') > > if __name__ == '__main__': > unittest.main() > > > This doesn't work, though. The keypresses don't register and create any > text in the entry. I can add text using event_generate inside a mainloop, > but without mainloop it doesn't seem to work. > > > Interestingly, I can use the same method to generate mouse events and they > seem to be recognized. Is there a way to make keypresses work without > mainloop? > > _______________________________________________ > Tkinter-discuss mailing list > Tkinter-discuss at python.org > https://mail.python.org/mailman/listinfo/tkinter-discuss > -------------- next part -------------- An HTML attachment was scrubbed... URL: From me at alandmoore.com Fri Feb 2 00:52:34 2018 From: me at alandmoore.com (alan moore) Date: Thu, 1 Feb 2018 23:52:34 -0600 Subject: [Tkinter-discuss] Simulating keystrokes in a tkinter unit test In-Reply-To: References: <808e0a5f-f96e-d482-2311-9682f56eaf90@alandmoore.com> Message-ID: <08692649-fabd-4a23-c0eb-8ff893c8df18@alandmoore.com> Thanks.? I managed to work it out.? I think the problem was twofold: - I needed to call self.mywidget.focus_force(), because my test root wasn't getting focus - I needed to call self.root.update() before *and* after the key events This got it working. Thanks for your feedback! On 02/01/2018 04:35 PM, Bryan Oakley wrote: > You might need to also generate a event and/or move the > mouse over the widget with the warp option (widget.event_generate(..., > warp=True), but you may need to also specify an x and y coordinate. I > don't remember). I think some widgets check to see if they have focus, > and will ignore keyboard events if they aren't focused. At least, I > vaguely recall having done that a decade or two ago when I tried > writing a tcl/tk testing harness. > > On Wed, Jan 31, 2018 at 12:09 PM, alan moore > wrote: > > Hi all, > > I'm attempting to write a unit test for a custom widget using > Python unittest.? I'm trying to simulate actual keystrokes in my > unit test. > > Following the example from the tkinter test suite, I created a > test that is approximately this: > > from tkinter import Entry as MyWidget > from tkinter.test.support import AbstractTkTest > import unittest > > class TestMyWidget(AbstractTkTest, unittest.TestCase): > > ??? def setUp(self): > ??????? super().setUp() > ??????? self.mywidget = self.create() > ??????? self.mywidget.wait_visibility() > > ??? def tearDown(self): > ??????? super().tearDown() > ??????? self.mywidget.destroy() > > ??? def create(self): > ??????? mw = MyWidget(self.root) > ??????? mw.pack() > ??????? return mw > > ??? def _keystroke(self, char): > self.mywidget.event_generate(''.format(char)) > self.mywidget.event_generate(''.format(char)) > ??????? self.mywidget.update_idletasks() > > ??? def test_key_entry(self): > ??????? self._keystroke('a') > ??????? self.assertEqual(self.mywidget.get(), 'a') > > if __name__ == '__main__': > ??? unittest.main() > > > This doesn't work, though.? The keypresses don't register and > create any text in the entry.? I can add text using event_generate > inside a mainloop, but without mainloop it doesn't seem to work. > > > Interestingly, I can use the same method to generate mouse events > and they seem to be recognized.? Is there a way to make keypresses > work without mainloop? > > _______________________________________________ > Tkinter-discuss mailing list > Tkinter-discuss at python.org > https://mail.python.org/mailman/listinfo/tkinter-discuss > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From klappnase at web.de Fri Feb 2 04:47:55 2018 From: klappnase at web.de (Michael Lange) Date: Fri, 2 Feb 2018 10:47:55 +0100 Subject: [Tkinter-discuss] Simulating keystrokes in a tkinter unit test In-Reply-To: <08692649-fabd-4a23-c0eb-8ff893c8df18@alandmoore.com> References: <808e0a5f-f96e-d482-2311-9682f56eaf90@alandmoore.com> <08692649-fabd-4a23-c0eb-8ff893c8df18@alandmoore.com> Message-ID: <20180202104755.513e98cdf9f6d895de5a5ca6@web.de> Hi, On Thu, 1 Feb 2018 23:52:34 -0600 alan moore wrote: > Thanks.? I managed to work it out.? I think the problem was twofold: > > - I needed to call self.mywidget.focus_force(), because my test root > wasn't getting focus > - I needed to call self.root.update() before *and* after the key events > > This got it working. Here (debian linux) the test also works if I just change the setUp() method like this: def setUp(self): super().setUp() self.mywidget = self.create() self.mywidget.wait_visibility() self.mywidget.update_idletasks() self.mywidget.focus_set() Regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. Is not that the nature of men and women -- that the pleasure is in the learning of each other? -- Natira, the High Priestess of Yonada, "For the World is Hollow and I Have Touched the Sky", stardate 5476.3. From jrs.idx at ntlworld.com Sat Feb 3 09:53:47 2018 From: jrs.idx at ntlworld.com (John Sampson) Date: Sat, 3 Feb 2018 14:53:47 +0000 Subject: [Tkinter-discuss] Buttons: Order of execution Message-ID: <22a54552-ef7e-3edc-15c8-7a19e28cec1d@ntlworld.com> I have a method in in which I have the line: self.greenButton.configure(text="Loading",state="disabled", background="green") followed by other lines. Being a complete newbie, I would expect the green button to start showing the text "Loading" before execution of the lines following the abovementioned line. But it does so immediately afterwards. I am not looking for a direct solution, but this shows that I do not understand how to write tkinter code involving buttons. Is there a simple explanation how to go about writing code so as to make things happen, either at all, or when I want them to happen? Regards John Sampson From bha100710 at gmail.com Sun Feb 4 09:52:34 2018 From: bha100710 at gmail.com (Bhaskar Chaudhary) Date: Sun, 4 Feb 2018 20:22:34 +0530 Subject: [Tkinter-discuss] Buttons: Order of execution In-Reply-To: <22a54552-ef7e-3edc-15c8-7a19e28cec1d@ntlworld.com> References: <22a54552-ef7e-3edc-15c8-7a19e28cec1d@ntlworld.com> Message-ID: Hi Add this line self.greenButton.update_idletasks() immediately after self.greenButton.configure(text="Loading",state="disabled", background="green") Does this give you the expected behavior ? If yes, you need to read up on Event processing in Tkinter. Here's a good article that explains it: http://wiki.tcl.tk/1527 regards Bhaskar On 2/3/18, John Sampson via Tkinter-discuss wrote: > I have a method in in which I have the line: > > self.greenButton.configure(text="Loading",state="disabled", > background="green") > > followed by other lines. > > Being a complete newbie, I would expect the green button to start > showing the text "Loading" before execution of the lines following the > abovementioned line. > > But it does so immediately afterwards. > > I am not looking for a direct solution, but this shows that I do not > understand how to write tkinter code involving buttons. Is there a > simple explanation how to > > go about writing code so as to make things happen, either at all, or > when I want them to happen? > > Regards > > John Sampson > > _______________________________________________ > Tkinter-discuss mailing list > Tkinter-discuss at python.org > https://mail.python.org/mailman/listinfo/tkinter-discuss > From rikudou__sennin at outlook.com Sun Feb 4 19:17:29 2018 From: rikudou__sennin at outlook.com (adil gourinda) Date: Mon, 5 Feb 2018 00:17:29 +0000 Subject: [Tkinter-discuss] Contribution to Tkinter's Documentation In-Reply-To: References: , Message-ID: After months of work I made a translation of the "Tk toolkit" from "Tcl" to "Python", Please take a look an tell me what you think, The work is not yet complete. https://tk-to-tkinter.blogspot.com/p/tkinter-centents.html?m=1 Thanks ________________________________ From: Tkinter-discuss on behalf of R Sent: Tuesday, January 2, 2018 6:08:00 PM To: tkinter-discuss at python.org Subject: Re: [Tkinter-discuss] Contribution to Tkinter's Documentation alright Adil, I'll keep you posted! _______________________________________________ Tkinter-discuss mailing list Tkinter-discuss at python.org https://mail.python.org/mailman/listinfo/tkinter-discuss -------------- next part -------------- An HTML attachment was scrubbed... URL: From bob at mellowood.ca Tue Feb 13 13:11:26 2018 From: bob at mellowood.ca (Bob van der Poel) Date: Tue, 13 Feb 2018 11:11:26 -0700 Subject: [Tkinter-discuss] Listbox size Message-ID: Is there a way to get the current height (hopefully in lines) of a listbox? I'd like to center, vertically, the result of a search. -- **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars **** Bob van der Poel ** Wynndel, British Columbia, CANADA ** EMAIL: bob at mellowood.ca WWW: http://www.mellowood.ca -------------- next part -------------- An HTML attachment was scrubbed... URL: From bha100710 at gmail.com Tue Feb 13 16:49:29 2018 From: bha100710 at gmail.com (Bhaskar Chaudhary) Date: Wed, 14 Feb 2018 03:19:29 +0530 Subject: [Tkinter-discuss] Listbox size In-Reply-To: References: Message-ID: Hi Bob Are you looking for the number of items in the listbox ? If yes this should give you the number of items. print(len(listbox.get(0, END))) Given that each item occupies one line in the list box, i think it the same as giving the height in lines. regards Bhaskar On 2/13/18, Bob van der Poel wrote: > Is there a way to get the current height (hopefully in lines) of a listbox? > I'd like to center, vertically, the result of a search. > > -- > > **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars **** > Bob van der Poel ** Wynndel, British Columbia, CANADA ** > EMAIL: bob at mellowood.ca > WWW: http://www.mellowood.ca > From bob at mellowood.ca Tue Feb 13 17:36:55 2018 From: bob at mellowood.ca (Bob van der Poel) Date: Tue, 13 Feb 2018 15:36:55 -0700 Subject: [Tkinter-discuss] Listbox size In-Reply-To: References: Message-ID: I know how to find the number of items. Just like you show. No, this is not the size of the box. I have maybe 2000 entries. I can set the current selection, etc with code like this: self.lb.select_clear(ACTIVE) # needed to un-hilite existing selection self.lb.activate(x) # make new item active self.lb.see(x-10) # force display from 10 previous lines self.lb.select_set(x) # and select the new item If the listbox is 20 lines high, this works perfectly. But, if it is a different size, it doesn't center. In my case I don't know the size of box, so the "-10" is a not-to-intelligent guess. Thanks. On Tue, Feb 13, 2018 at 2:49 PM, Bhaskar Chaudhary wrote: > Hi Bob > > Are you looking for the number of items in the listbox ? > If yes this should give you the number of items. > > print(len(listbox.get(0, END))) > > Given that each item occupies one line in the list box, i think it the > same as giving the height in lines. > > regards > Bhaskar > > > On 2/13/18, Bob van der Poel wrote: > > Is there a way to get the current height (hopefully in lines) of a > listbox? > > I'd like to center, vertically, the result of a search. > > > > -- > > > > **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars **** > > Bob van der Poel ** Wynndel, British Columbia, CANADA ** > > EMAIL: bob at mellowood.ca > > WWW: http://www.mellowood.ca > > > -- **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars **** Bob van der Poel ** Wynndel, British Columbia, CANADA ** EMAIL: bob at mellowood.ca WWW: http://www.mellowood.ca -------------- next part -------------- An HTML attachment was scrubbed... URL: From bryan.oakley at gmail.com Tue Feb 13 18:09:30 2018 From: bryan.oakley at gmail.com (Bryan Oakley) Date: Tue, 13 Feb 2018 17:09:30 -0600 Subject: [Tkinter-discuss] Listbox size In-Reply-To: References: Message-ID: The actual height of any widget can be obtained with the winfo_height method. It returns the value in pixels. To get the height in number of lines you'll have to compute the height of your font and do a little math. On Tue, Feb 13, 2018 at 12:11 PM, Bob van der Poel wrote: > Is there a way to get the current height (hopefully in lines) of a > listbox? I'd like to center, vertically, the result of a search. > > -- > > **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars **** > Bob van der Poel ** Wynndel, British Columbia, CANADA ** > EMAIL: bob at mellowood.ca > WWW: http://www.mellowood.ca > > _______________________________________________ > Tkinter-discuss mailing list > Tkinter-discuss at python.org > https://mail.python.org/mailman/listinfo/tkinter-discuss > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bryan.oakley at gmail.com Tue Feb 13 18:19:44 2018 From: bryan.oakley at gmail.com (Bryan Oakley) Date: Tue, 13 Feb 2018 17:19:44 -0600 Subject: [Tkinter-discuss] Listbox size In-Reply-To: References: Message-ID: If your actual goal is to scroll the listbox so that an item is centered, you can first scroll the item as far as possible and then call the "see" method. For example, if the result is in the first half of the items, scroll all the way down (listbox.yview("end")). If it's in the second half, scroll all the way up (listbox.yview(0)). Then call "see" and tkinter will do its best to center it. The documentation for the "see" method says this: Adjust the view in the listbox so that the element given by *index* is > visible. If the element is already visible then the command has no effect; > if the element is near one edge of the window then the listbox scrolls to > bring the element into view at the edge; otherwise the listbox scrolls to > center the element. Another trick would be to get the index of the item at the 0th pixel location of the widget (eg: the_listbox.index("@0,0"), and another at the pixel location for the height of the widget (ie: by using the result of the winfo_height method of the widget). That will tell you how many visible lines there are. On Tue, Feb 13, 2018 at 4:36 PM, Bob van der Poel wrote: > I know how to find the number of items. Just like you show. > > No, this is not the size of the box. I have maybe 2000 entries. I can set > the current selection, etc with code like this: > > self.lb.select_clear(ACTIVE) # needed to un-hilite existing selection > self.lb.activate(x) # make new item active > self.lb.see(x-10) # force display from 10 > previous lines > self.lb.select_set(x) # and select the new item > > If the listbox is 20 lines high, this works perfectly. But, if it is a > different size, it doesn't center. In my case I don't know the size of box, > so the "-10" is a not-to-intelligent guess. > > Thanks. > > > > On Tue, Feb 13, 2018 at 2:49 PM, Bhaskar Chaudhary > wrote: > >> Hi Bob >> >> Are you looking for the number of items in the listbox ? >> If yes this should give you the number of items. >> >> print(len(listbox.get(0, END))) >> >> Given that each item occupies one line in the list box, i think it the >> same as giving the height in lines. >> >> regards >> Bhaskar >> >> >> On 2/13/18, Bob van der Poel wrote: >> > Is there a way to get the current height (hopefully in lines) of a >> listbox? >> > I'd like to center, vertically, the result of a search. >> > >> > -- >> > >> > **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars **** >> > Bob van der Poel ** Wynndel, British Columbia, CANADA ** >> > EMAIL: bob at mellowood.ca >> > WWW: http://www.mellowood.ca >> > >> > > > > -- > > **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars **** > Bob van der Poel ** Wynndel, British Columbia, CANADA ** > EMAIL: bob at mellowood.ca > WWW: http://www.mellowood.ca > > _______________________________________________ > Tkinter-discuss mailing list > Tkinter-discuss at python.org > https://mail.python.org/mailman/listinfo/tkinter-discuss > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From klappnase at web.de Tue Feb 13 18:32:09 2018 From: klappnase at web.de (Michael Lange) Date: Wed, 14 Feb 2018 00:32:09 +0100 Subject: [Tkinter-discuss] Listbox size In-Reply-To: References: Message-ID: <20180214003209.36b02d26d950221e4d32de30@web.de> Hi, On Tue, 13 Feb 2018 15:36:55 -0700 Bob van der Poel wrote: > I know how to find the number of items. Just like you show. > > No, this is not the size of the box. I have maybe 2000 entries. I can > set the current selection, etc with code like this: > > self.lb.select_clear(ACTIVE) # needed to un-hilite existing > selection self.lb.activate(x) # make new item active > self.lb.see(x-10) # force display from 10 > previous lines > self.lb.select_set(x) # and select the new item > > If the listbox is 20 lines high, this works perfectly. But, if it is a > different size, it doesn't center. In my case I don't know the size of > box, so the "-10" is a not-to-intelligent guess. you mean, the selected item should be vertically centered in the list? I believ there is no trivial way to do this, but probably more than one way with a little tinkering. I set up a simple example with yview() instead of see(), which seems more appropriate here. I guess this simple function is certainly not perfect (but maybe it is already good enough): ###################################################### from Tkinter import * root=Tk() l = Listbox(root) l.pack(side='left', fill='both', expand=1) s = Scrollbar(root, command=l.yview) s.pack(side='right', fill='y') l.configure(yscrollcommand=s.set) for i in range (2000): l.insert('end', 'Item %d' % i) def test(event): i = 597 wd = event.widget wd.selection_clear(0, 'end') wd.selection_set(i) wd.update_idletasks() h1 = wd.winfo_height() i1, i2 = wd.nearest(0), wd.nearest(h1) wd.yview(i - (i2-i1)/2) l.bind('', test) l.focus() root.mainloop() ##################################################### Best regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. "The combination of a number of things to make existence worthwhile." "Yes, the philosophy of 'none,' meaning 'all.'" -- Spock and Lincoln, "The Savage Curtain", stardate 5906.4 From klappnase at web.de Tue Feb 13 18:33:22 2018 From: klappnase at web.de (Michael Lange) Date: Wed, 14 Feb 2018 00:33:22 +0100 Subject: [Tkinter-discuss] Listbox size In-Reply-To: References: Message-ID: <20180214003322.395937fe9c3de7d3b8cfd701@web.de> Hi, On Tue, 13 Feb 2018 17:09:30 -0600 Bryan Oakley wrote: > The actual height of any widget can be obtained with the winfo_height > method. It returns the value in pixels. To get the height in number of > lines you'll have to compute the height of your font and do a little > math. huh, did I miss something here? Why not just use size() ? Best regards Michael > > > > On Tue, Feb 13, 2018 at 12:11 PM, Bob van der Poel > wrote: > > > Is there a way to get the current height (hopefully in lines) of a > > listbox? I'd like to center, vertically, the result of a search. > > > > -- > > > > **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars **** > > Bob van der Poel ** Wynndel, British Columbia, CANADA ** > > EMAIL: bob at mellowood.ca > > WWW: http://www.mellowood.ca > > > > _______________________________________________ > > Tkinter-discuss mailing list > > Tkinter-discuss at python.org > > https://mail.python.org/mailman/listinfo/tkinter-discuss > > > > .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. No one wants war. -- Kirk, "Errand of Mercy", stardate 3201.7 From klappnase at web.de Tue Feb 13 18:38:02 2018 From: klappnase at web.de (Michael Lange) Date: Wed, 14 Feb 2018 00:38:02 +0100 Subject: [Tkinter-discuss] Listbox size In-Reply-To: References: Message-ID: <20180214003802.c8aa47231b8a27da93cde052@web.de> Hi, On Wed, 14 Feb 2018 03:19:29 +0530 Bhaskar Chaudhary wrote: > Hi Bob > > Are you looking for the number of items in the listbox ? > If yes this should give you the number of items. > > print(len(listbox.get(0, END))) > > Given that each item occupies one line in the list box, i think it the > same as giving the height in lines. correct, this should work. However, life can be made a tiny bit easier with print(listbox.size()) Best regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. War isn't a good life, but it's life. -- Kirk, "A Private Little War", stardate 4211.8 From klappnase at web.de Tue Feb 13 18:35:55 2018 From: klappnase at web.de (Michael Lange) Date: Wed, 14 Feb 2018 00:35:55 +0100 Subject: [Tkinter-discuss] Listbox size In-Reply-To: References: Message-ID: <20180214003555.410010ea655de6f545ec58ca@web.de> On Tue, 13 Feb 2018 17:19:44 -0600 Bryan Oakley wrote: (...) > Another trick would be to get the index of the item at the 0th pixel > location of the widget (eg: the_listbox.index("@0,0"), and another at > the pixel location for the height of the widget (ie: by using the > result of the winfo_height method of the widget). That will tell you > how many visible lines there are. oh, I see now you had that idea first :) So if my snippet is of any use for the OP, the credit goes to you! Best regards Michael > > > On Tue, Feb 13, 2018 at 4:36 PM, Bob van der Poel > wrote: > > > I know how to find the number of items. Just like you show. > > > > No, this is not the size of the box. I have maybe 2000 entries. I can > > set the current selection, etc with code like this: > > > > self.lb.select_clear(ACTIVE) # needed to un-hilite existing > > selection self.lb.activate(x) # make new item > > active self.lb.see(x-10) # force display from 10 > > previous lines > > self.lb.select_set(x) # and select the new item > > > > If the listbox is 20 lines high, this works perfectly. But, if it is a > > different size, it doesn't center. In my case I don't know the size > > of box, so the "-10" is a not-to-intelligent guess. > > > > Thanks. > > > > > > > > On Tue, Feb 13, 2018 at 2:49 PM, Bhaskar Chaudhary > > wrote: > > > >> Hi Bob > >> > >> Are you looking for the number of items in the listbox ? > >> If yes this should give you the number of items. > >> > >> print(len(listbox.get(0, END))) > >> > >> Given that each item occupies one line in the list box, i think it > >> the same as giving the height in lines. > >> > >> regards > >> Bhaskar > >> > >> > >> On 2/13/18, Bob van der Poel wrote: > >> > Is there a way to get the current height (hopefully in lines) of a > >> listbox? > >> > I'd like to center, vertically, the result of a search. > >> > > >> > -- > >> > > >> > **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars > >> > **** Bob van der Poel ** Wynndel, British Columbia, CANADA ** > >> > EMAIL: bob at mellowood.ca > >> > WWW: http://www.mellowood.ca > >> > > >> > > > > > > > > -- > > > > **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars **** > > Bob van der Poel ** Wynndel, British Columbia, CANADA ** > > EMAIL: bob at mellowood.ca > > WWW: http://www.mellowood.ca > > > > _______________________________________________ > > Tkinter-discuss mailing list > > Tkinter-discuss at python.org > > https://mail.python.org/mailman/listinfo/tkinter-discuss > > > > .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. Humans do claim a great deal for that particular emotion (love). -- Spock, "The Lights of Zetar", stardate 5725.6 From klappnase at web.de Tue Feb 13 19:05:04 2018 From: klappnase at web.de (Michael Lange) Date: Wed, 14 Feb 2018 01:05:04 +0100 Subject: [Tkinter-discuss] Listbox size In-Reply-To: <20180214003209.36b02d26d950221e4d32de30@web.de> References: <20180214003209.36b02d26d950221e4d32de30@web.de> Message-ID: <20180214010504.192d945403c13576b9a98ff0@web.de> On Wed, 14 Feb 2018 00:32:09 +0100 Michael Lange wrote: (...) > I guess > this simple function is certainly not perfect (but maybe it is already > good enough): um, maybe that test() function should be slightly changed, like this (although yview() does not seem to complain about negative indices): def test(event): i = 597 wd = event.widget wd.selection_clear(0, 'end') wd.selection_set(i) wd.update_idletasks() h1 = wd.winfo_height() i1, i2 = wd.nearest(0), wd.nearest(h1) index = i - (i2-i1)/2 if index < 0: index = 0 wd.yview(index) Best regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. It is necessary to have purpose. -- Alice #1, "I, Mudd", stardate 4513.3 From bob at mellowood.ca Tue Feb 13 19:11:47 2018 From: bob at mellowood.ca (Bob van der Poel) Date: Tue, 13 Feb 2018 17:11:47 -0700 Subject: [Tkinter-discuss] Listbox size In-Reply-To: References: Message-ID: Getting closer :) I asked the question wrongly. I need to determine the number of VISIBLE lines in the listbox. Using the .winfo_height() does return the height in pixels. Note: You do have to make the widget visible before doing this, otherwise you just get "1" returned. widget.update() works for this. So, the next question is: what is the line size? Now, to make life much simpler :) I just played a bit and discovered that if I make the listbox visible BEFORE the selection, it automatically centers. So, my code now becomes: self.lb.update() self.lb.select_clear(ACTIVE) # needed to un-hilite existing selection self.lb.see(x) self.lb.activate(x) self.lb.select_set(x) Easy. And I don't need to worry about the size of the box! Well, mostly. If the selection I want is already on the screen, then the box doesn't scroll to center. I think I can live with that. Best, On Tue, Feb 13, 2018 at 4:09 PM, Bryan Oakley wrote: > The actual height of any widget can be obtained with the winfo_height > method. It returns the value in pixels. To get the height in number of > lines you'll have to compute the height of your font and do a little math. > > > > On Tue, Feb 13, 2018 at 12:11 PM, Bob van der Poel > wrote: > >> Is there a way to get the current height (hopefully in lines) of a >> listbox? I'd like to center, vertically, the result of a search. >> >> -- >> >> **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars **** >> Bob van der Poel ** Wynndel, British Columbia, CANADA ** >> EMAIL: bob at mellowood.ca >> WWW: http://www.mellowood.ca >> >> _______________________________________________ >> Tkinter-discuss mailing list >> Tkinter-discuss at python.org >> https://mail.python.org/mailman/listinfo/tkinter-discuss >> >> > -- **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars **** Bob van der Poel ** Wynndel, British Columbia, CANADA ** EMAIL: bob at mellowood.ca WWW: http://www.mellowood.ca -------------- next part -------------- An HTML attachment was scrubbed... URL: From klappnase at web.de Wed Feb 14 17:42:46 2018 From: klappnase at web.de (Michael Lange) Date: Wed, 14 Feb 2018 23:42:46 +0100 Subject: [Tkinter-discuss] Listbox size In-Reply-To: References: Message-ID: <20180214234246.ca57d9a128ad837646d114cc@web.de> Hi, On Tue, 13 Feb 2018 17:11:47 -0700 Bob van der Poel wrote: > Getting closer :) > > I asked the question wrongly. I need to determine the number of VISIBLE > lines in the listbox. Should be (as used in my previous example) visible_lines = lb.nearest(lb.winfo_height()) - lb.nearest(0) > > Using the .winfo_height() does return the height in pixels. Note: You do > have to make the widget visible before doing this, otherwise you just > get "1" returned. widget.update() works for this. I don't think the widget must actually be "visible"; it is only when the widget is initially created that the widget's dimensions are reported as 1*1 px. Calling update_idletasks() once after the widgets have been created should be sufficient to work around this. > > So, the next question is: what is the line size? That is definitely not needed for what you want to accomplish. > > Now, to make life much simpler :) I just played a bit and discovered > that if I make the listbox visible BEFORE the selection, it > automatically centers. So, my code now becomes: > > self.lb.update() > self.lb.select_clear(ACTIVE) # needed to un-hilite existing > selection self.lb.see(x) > self.lb.activate(x) > self.lb.select_set(x) > > Easy. And I don't need to worry about the size of the box! Here see() does not necessarily center the given index. It sometimes appears near the top or the bottom of the list. And that's just what the manpage promises: " if the element is near one edge of the window then the listbox scrolls to bring the element into view at the edge; otherwise the listbox scrolls to center the element. " > > Well, mostly. If the selection I want is already on the screen, then the > box doesn't scroll to center. I think I can live with that. Good, again that's exactly what the listbox's manpage promises :) If you want exactly predictable behavior, I think you will have to use yview() instead. Best regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. You humans have that emotional need to express gratitude. "You're welcome," I believe, is the correct response. -- Spock, "Bread and Circuses", stardate 4041.2 From c.buhtz at posteo.jp Tue Feb 13 16:00:48 2018 From: c.buhtz at posteo.jp (c.buhtz at posteo.jp) Date: Tue, 13 Feb 2018 22:00:48 +0100 Subject: [Tkinter-discuss] Get height of a row in Treeview in Tkinter with Python3 Message-ID: <3zgw1L3VC2z9rxL@submission02.posteo.de> X-Post: https://stackoverflow.com/q/48736168/4865723 I want to know how height (in pixel) is a row in a tkinter.Treeview. I know how I can manipulate the height (see example below) with the styling mechanism. But I want to read it first - because it depends on the operating system, fonts, etc. And I couldn't find something in the style object I could read for this. My goal is to modify the rowheight with a relative factor. e.g. 1,5 of the original height. #!/usr/bin/env python3 from tkinter import * from tkinter import ttk if __name__ == '__main__': root = Tk() style = ttk.Style() # here I set the `rowheight` style.configure('MyTreeView.Treeview', rowheight=45) tree = ttk.Treeview(root, style='MyTreeView.Treeview') tree.pack() for i in range(5): tree.insert(parent='', index=END, text='item {}'.format(i)) root.mainloop() From c.buhtz at posteo.jp Tue Feb 13 19:14:53 2018 From: c.buhtz at posteo.jp (c.buhtz at posteo.jp) Date: Wed, 14 Feb 2018 01:14:53 +0100 Subject: [Tkinter-discuss] multicolumn list/tree with images Message-ID: <3zh0KL3CBnz9rxH@submission02.posteo.de> I experimented with tkinter.ttk.Treeview and tkinter.Listbox on Python3. I need a list widget with multiple columns (this could be Treeview). But I need images as values. I don't want to use column '#0' for images. Images should appear in the second column for example. Tk itself has a treectrl. Maybe it is possible with it? But I don't know how to use this with Python3. Maybe there are other third party solutions around? From c.buhtz at posteo.jp Tue Feb 13 19:26:20 2018 From: c.buhtz at posteo.jp (c.buhtz at posteo.jp) Date: Wed, 14 Feb 2018 01:26:20 +0100 Subject: [Tkinter-discuss] multicolumn list/tree with images Message-ID: <3zh0ZP2sCYz9rxP@submission02.posteo.de> On 2018-02-14 01:14 wrote: > But I need images as values. I don't want to use column '#0' for > images. Images should appear in the second column for example. A workaround would be to keep the '#0' column with the image. But I need to remove the tree space (some pixel on the left of the picture) because I don't need a tree. But show='headings' would remove the complete '#0' column with the image. I need an image in each list row as small button. The user can use this as a flag to mark some rows. From bob at mellowood.ca Wed Feb 14 20:34:37 2018 From: bob at mellowood.ca (Bob van der Poel) Date: Wed, 14 Feb 2018 18:34:37 -0700 Subject: [Tkinter-discuss] Listbox size In-Reply-To: <20180214234246.ca57d9a128ad837646d114cc@web.de> References: <20180214234246.ca57d9a128ad837646d114cc@web.de> Message-ID: Thanks Micheal! I am going to save the "number of lines" snippet for future reference. Best, On Wed, Feb 14, 2018 at 3:42 PM, Michael Lange wrote: > Hi, > > On Tue, 13 Feb 2018 17:11:47 -0700 > Bob van der Poel wrote: > > > Getting closer :) > > > > I asked the question wrongly. I need to determine the number of VISIBLE > > lines in the listbox. > > Should be (as used in my previous example) > > visible_lines = lb.nearest(lb.winfo_height()) - lb.nearest(0) > > > > > Using the .winfo_height() does return the height in pixels. Note: You do > > have to make the widget visible before doing this, otherwise you just > > get "1" returned. widget.update() works for this. > > I don't think the widget must actually be "visible"; it is only when the > widget is initially created that the widget's dimensions are reported as > 1*1 px. Calling update_idletasks() once after the widgets have been > created should be sufficient to work around this. > > > > > So, the next question is: what is the line size? > > That is definitely not needed for what you want to accomplish. > > > > > Now, to make life much simpler :) I just played a bit and discovered > > that if I make the listbox visible BEFORE the selection, it > > automatically centers. So, my code now becomes: > > > > self.lb.update() > > self.lb.select_clear(ACTIVE) # needed to un-hilite existing > > selection self.lb.see(x) > > self.lb.activate(x) > > self.lb.select_set(x) > > > > Easy. And I don't need to worry about the size of the box! > > Here see() does not necessarily center the given index. It sometimes > appears near the top or the bottom of the list. > And that's just what the manpage promises: > > " if the element is near one edge of the window then the listbox scrolls > to bring the element into view at the edge; otherwise the listbox scrolls > to center the element. " > > > > > Well, mostly. If the selection I want is already on the screen, then the > > box doesn't scroll to center. I think I can live with that. > > Good, again that's exactly what the listbox's manpage promises :) > If you want exactly predictable behavior, I think you will have to use > yview() instead. > > Best regards > > Michael > > > .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. > > You humans have that emotional need to express gratitude. "You're > welcome," I believe, is the correct response. > -- Spock, "Bread and Circuses", stardate 4041.2 > _______________________________________________ > Tkinter-discuss mailing list > Tkinter-discuss at python.org > https://mail.python.org/mailman/listinfo/tkinter-discuss > -- **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars **** Bob van der Poel ** Wynndel, British Columbia, CANADA ** EMAIL: bob at mellowood.ca WWW: http://www.mellowood.ca -------------- next part -------------- An HTML attachment was scrubbed... URL: From klappnase at web.de Thu Feb 15 05:19:28 2018 From: klappnase at web.de (Michael Lange) Date: Thu, 15 Feb 2018 11:19:28 +0100 Subject: [Tkinter-discuss] multicolumn list/tree with images In-Reply-To: <3zh0KL3CBnz9rxH@submission02.posteo.de> References: <3zh0KL3CBnz9rxH@submission02.posteo.de> Message-ID: <20180215111928.73048f2ad7c86970135ee259@web.de> Hi, On Wed, 14 Feb 2018 01:14:53 +0100 wrote: > I experimented with tkinter.ttk.Treeview and tkinter.Listbox on Python3. > > I need a list widget with multiple columns (this could be Treeview). > But I need images as values. I don't want to use column '#0' for > images. Images should appear in the second column for example. > > Tk itself has a treectrl. Maybe it is possible with it? But I don't > know how to use this with Python3. > > Maybe there are other third party solutions around? with ttk.Treeview you can add images only to the first column. There are several third party solutions available, though. TkinterTreectrl is a wrapper for the tktreectrl tcl extension and contains a multi column listbox class that can be set up to use images in any column: https://sourceforge.net/projects/tktreectrl/ https://sourceforge.net/projects/tkintertreectrl/ (seems like sf's project pages are down today, but downloads are possible). There are some other alternatives that I (being somewhat biased towards the treectrl :) have never been using myself, which should be able to do this, like tablelist and maybe tktable, you can look at this archived page for more information and download links: https://web.archive.org/web/20140412080251/http://tkinter.unpythonic.net:80/wiki/Widgets Finally there seems to be a tix.TList widget which might be able to do what you want, though I never used it myself, see https://docs.python.org/3/library/tkinter.tix.html Best regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. You'll learn something about men and women -- the way they're supposed to be. Caring for each other, being happy with each other, being good to each other. That's what we call love. You'll like that a lot. -- Kirk, "The Apple", stardate 3715.6 From klappnase at web.de Thu Feb 15 05:32:30 2018 From: klappnase at web.de (Michael Lange) Date: Thu, 15 Feb 2018 11:32:30 +0100 Subject: [Tkinter-discuss] multicolumn list/tree with images In-Reply-To: <3zh0ZP2sCYz9rxP@submission02.posteo.de> References: <3zh0ZP2sCYz9rxP@submission02.posteo.de> Message-ID: <20180215113230.6f7ac67fd6e7f23a48ea60dd@web.de> Hi, On Wed, 14 Feb 2018 01:26:20 +0100 wrote: > On 2018-02-14 01:14 wrote: > > But I need images as values. I don't want to use column '#0' for > > images. Images should appear in the second column for example. > > A workaround would be to keep the '#0' column with the image. > But I need to remove the tree space (some pixel on the left of the > picture) because I don't need a tree. But show='headings' would remove > the complete '#0' column with the image. > > I need an image in each list row as small button. The user can use this > as a flag to mark some rows. I have a similar setup in one of my apps, where I use a "checkbutton" image in col #0 so the user can select or deselect a list entry. It works, but is not perfect, though: there does not seem to be a way to make only the "checkbutton" icon sensitive to mouse clicks, so the "checkbutton's" state changes even when the user clicks somewhere in col #0 next to the icon. And the "active rectangle" appears only as a tiny dotted rectangle between the icon and the text which is in col #1. The setup I use, which apart from the above issues I think looks good enough, is treeview.configure(show=('tree', ), columns=('foobar',)) treeview.column('#0', width=35, stretch=0) Best regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. Live long and prosper. -- Spock, "Amok Time", stardate 3372.7 From Vasilis.Vlachoudis at cern.ch Thu Feb 15 05:24:10 2018 From: Vasilis.Vlachoudis at cern.ch (Vasilis Vlachoudis) Date: Thu, 15 Feb 2018 10:24:10 +0000 Subject: [Tkinter-discuss] listdir Message-ID: <0BC70B5D93E054469872FFD0FE07220E01D0030B3D@CERNXCHG53.cern.ch> Hi all, I know that it it is not related to tkinter, but I had troubles in submitting in the general python list. I was trying to make my custom FileDialog to handle unicode filenames and I stuck with the following behavior (python v2.7) #path = "/home/bnv/Download" path = u"/home/bnv/Download" for fn in os.listdir(path): print(fn,type(fn)) fullpath = os.path.join(path, fn) If the "path" is defined as unicode I get the following output and error (u'foo.py', ) ('Gewu\xccrzhalter.Part1.nc', ) Traceback (most recent call last): File "foo.py", line 20, in filename = os.path.join(path, fn) File "/usr/lib/python2.7/posixpath.py", line 73, in join path += '/' + b UnicodeDecodeError: 'ascii' codec can't decode byte 0xcc in position 5: ordinal not in range(128) While if the "path" is defined as str. I get ('foo.py', ) ('Gewu\xccrzhalter.Part1.nc', ) I don't understand why if the path is unicode the output of listdir is unicode the filenames containing only ascii and str for the file with the non-ascii character. While if the path is str, all output is in str Thanks in advance Vasilis -------------- next part -------------- An HTML attachment was scrubbed... URL: From klappnase at web.de Thu Feb 15 14:32:11 2018 From: klappnase at web.de (Michael Lange) Date: Thu, 15 Feb 2018 20:32:11 +0100 Subject: [Tkinter-discuss] listdir In-Reply-To: <0BC70B5D93E054469872FFD0FE07220E01D0030B3D@CERNXCHG53.cern.ch> References: <0BC70B5D93E054469872FFD0FE07220E01D0030B3D@CERNXCHG53.cern.ch> Message-ID: <20180215203211.958526481ef7e862664f0d8f@web.de> Hi, On Thu, 15 Feb 2018 10:24:10 +0000 Vasilis Vlachoudis wrote: > Hi all, > > I know that it it is not related to tkinter, but I had troubles in > submitting in the general python list. > > I was trying to make my custom FileDialog to handle unicode filenames > and I stuck with the following behavior (python v2.7) > > #path = "/home/bnv/Download" > path = u"/home/bnv/Download" > for fn in os.listdir(path): > print(fn,type(fn)) > fullpath = os.path.join(path, fn) > > If the "path" is defined as unicode I get the following output and error > (u'foo.py', ) > ('Gewu\xccrzhalter.Part1.nc', ) > Traceback (most recent call last): > File "foo.py", line 20, in > filename = os.path.join(path, fn) > File "/usr/lib/python2.7/posixpath.py", line 73, in join > path += '/' + b > UnicodeDecodeError: 'ascii' codec can't decode byte 0xcc in position 5: > ordinal not in range(128) > > While if the "path" is defined as str. I get > ('foo.py', ) > ('Gewu\xccrzhalter.Part1.nc', ) > > I don't understand why if the path is unicode the output of listdir is > unicode the filenames containing only ascii and str for the file with > the non-ascii character. While if the path is str, all output is in str I think this is because with "foo.py" no UnicodeError occurs and so the str->unicode conversion succeeds. I can only guess why that UnicodeError occurs though, this doesn't happen here. When I do $ export LANG=C before starting the python interpreter however I get this same error as you (by default I have UTF-8 here). So I think maybe your system default encoding is set to something that does not support the non-ascii character in the filename which causes the problem. This might also explain why the default tk file dialogs do not seem to work for you (they work well here with unicode filenames). Just a guess, of course. Best regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. If a man had a child who'd gone anti-social, killed perhaps, he'd still tend to protect that child. -- McCoy, "The Ultimate Computer", stardate 4731.3 From klappnase at web.de Thu Feb 15 14:43:28 2018 From: klappnase at web.de (Michael Lange) Date: Thu, 15 Feb 2018 20:43:28 +0100 Subject: [Tkinter-discuss] Get height of a row in Treeview in Tkinter with Python3 In-Reply-To: <3zgw1L3VC2z9rxL@submission02.posteo.de> References: <3zgw1L3VC2z9rxL@submission02.posteo.de> Message-ID: <20180215204328.8079c49262406652f00485d8@web.de> Hi, On Tue, 13 Feb 2018 22:00:48 +0100 wrote: > X-Post: https://stackoverflow.com/q/48736168/4865723 > > I want to know how height (in pixel) is a row in a tkinter.Treeview. > > I know how I can manipulate the height (see example below) with the > styling mechanism. But I want to read it first - because it depends on > the operating system, fonts, etc. And I couldn't find something in the > style object I could read for this. > > My goal is to modify the rowheight with a relative factor. e.g. 1,5 of > the original height. I never used the rowheight option myself, so I don't know for sure. I figure that by default the rowheight is not set explicitely and thus the widget will assign the required height for each row individually (for example different sized icons might lead to different row heights); probably when rowheight is explicitely given all rows will be the same height, regardless of their contents (is this correct?). Now, if you assume that in your case all rows have the same height, I think using bbox(someitem)[3] should deliver the value you want (maybe with one or two extra pixels added for borders). However if the heights of the rows differ, this value might probably lead to rather disappointing results :) Best regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. Even historians fail to learn from history -- they repeat the same mistakes. -- John Gill, "Patterns of Force", stardate 2534.7 From Vasilis.Vlachoudis at cern.ch Fri Feb 16 03:49:01 2018 From: Vasilis.Vlachoudis at cern.ch (Vasilis Vlachoudis) Date: Fri, 16 Feb 2018 08:49:01 +0000 Subject: [Tkinter-discuss] listdir In-Reply-To: <20180215203211.958526481ef7e862664f0d8f@web.de> References: <0BC70B5D93E054469872FFD0FE07220E01D0030B3D@CERNXCHG53.cern.ch>, <20180215203211.958526481ef7e862664f0d8f@web.de> Message-ID: <0BC70B5D93E054469872FFD0FE07220E01D0031693@CERNXCHG53.cern.ch> Thank you Michael, my locale is UTF-8 when I get the error $ locale LANG=en_US.UTF-8 LANGUAGE=en_US.UTF-8 LC_CTYPE="en_US.UTF-8" ... LC_ALL=en_US.UTF-8 python v3 works ok though Vasilis ________________________________________ From: Tkinter-discuss [tkinter-discuss-bounces+vasilis.vlachoudis=cern.ch at python.org] on behalf of Michael Lange [klappnase at web.de] Sent: Thursday, February 15, 2018 20:32 To: tkinter-discuss at python.org Subject: Re: [Tkinter-discuss] listdir Hi, On Thu, 15 Feb 2018 10:24:10 +0000 Vasilis Vlachoudis wrote: > Hi all, > > I know that it it is not related to tkinter, but I had troubles in > submitting in the general python list. > > I was trying to make my custom FileDialog to handle unicode filenames > and I stuck with the following behavior (python v2.7) > > #path = "/home/bnv/Download" > path = u"/home/bnv/Download" > for fn in os.listdir(path): > print(fn,type(fn)) > fullpath = os.path.join(path, fn) > > If the "path" is defined as unicode I get the following output and error > (u'foo.py', ) > ('Gewu\xccrzhalter.Part1.nc', ) > Traceback (most recent call last): > File "foo.py", line 20, in > filename = os.path.join(path, fn) > File "/usr/lib/python2.7/posixpath.py", line 73, in join > path += '/' + b > UnicodeDecodeError: 'ascii' codec can't decode byte 0xcc in position 5: > ordinal not in range(128) > > While if the "path" is defined as str. I get > ('foo.py', ) > ('Gewu\xccrzhalter.Part1.nc', ) > > I don't understand why if the path is unicode the output of listdir is > unicode the filenames containing only ascii and str for the file with > the non-ascii character. While if the path is str, all output is in str I think this is because with "foo.py" no UnicodeError occurs and so the str->unicode conversion succeeds. I can only guess why that UnicodeError occurs though, this doesn't happen here. When I do $ export LANG=C before starting the python interpreter however I get this same error as you (by default I have UTF-8 here). So I think maybe your system default encoding is set to something that does not support the non-ascii character in the filename which causes the problem. This might also explain why the default tk file dialogs do not seem to work for you (they work well here with unicode filenames). Just a guess, of course. Best regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. If a man had a child who'd gone anti-social, killed perhaps, he'd still tend to protect that child. -- McCoy, "The Ultimate Computer", stardate 4731.3 _______________________________________________ Tkinter-discuss mailing list Tkinter-discuss at python.org https://mail.python.org/mailman/listinfo/tkinter-discuss From klappnase at web.de Fri Feb 16 04:24:59 2018 From: klappnase at web.de (Michael Lange) Date: Fri, 16 Feb 2018 10:24:59 +0100 Subject: [Tkinter-discuss] listdir In-Reply-To: <0BC70B5D93E054469872FFD0FE07220E01D0031693@CERNXCHG53.cern.ch> References: <0BC70B5D93E054469872FFD0FE07220E01D0030B3D@CERNXCHG53.cern.ch> <20180215203211.958526481ef7e862664f0d8f@web.de> <0BC70B5D93E054469872FFD0FE07220E01D0031693@CERNXCHG53.cern.ch> Message-ID: <20180216102459.0842feb3ddb03f688afdcc0a@web.de> Hi, On Fri, 16 Feb 2018 08:49:01 +0000 Vasilis Vlachoudis wrote: > Thank you Michael, > > my locale is UTF-8 when I get the error > > $ locale > LANG=en_US.UTF-8 > LANGUAGE=en_US.UTF-8 > LC_CTYPE="en_US.UTF-8" > ... > LC_ALL=en_US.UTF-8 ok. Don't know if it is possible that there is something in the configuration that stops python from properly detecting the system encoding? Some ten years ago (while using Python 2.3 :) I wrote myself a function that tries to detect the system encoding (see below); since it is so old some of the things that function does may be obsolete now, but I still use it today in a number of my projects and it still works. If you like you can run this snippet as a test script and see what it says. Of course it is also well possible that my guess was wrong and the problem lies somewhere else. Best regards Michael ################################################### import codecs import locale def _find_codec(encoding): # return True if the requested codec is available, else return False try: codecs.lookup(encoding) return 1 except (TypeError, LookupError): return 0 def _sysencoding(): # try to guess the system default encoding # this is mainly stolen from IDLE's IOBinding.py # thanks for guidance to Martin v. Loewis locale.setlocale(locale.LC_CTYPE, "")# don't mess the numeric types here, we might replace the decimal point with a comma and break Tk! try: enc = locale.getpreferredencoding() if enc and _find_codec(enc): return enc except AttributeError: # our python is too old, try something else pass try: enc = locale.nl_langinfo(locale.CODESET) if enc and _find_codec(enc): return enc except AttributeError: pass # the last try try: enc = locale.getdefaultlocale()[1] if enc and _find_codec(enc): return enc except ValueError: pass # aargh, nothing good found, fall back to ascii and hope for the best print 'warning: unable to find usable encoding, using ascii.' return 'ascii' print(_sysencoding().lower()) ################################################### .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. Vulcans believe peace should not depend on force. -- Amanda, "Journey to Babel", stardate 3842.3 From klappnase at web.de Fri Feb 16 12:30:08 2018 From: klappnase at web.de (Michael Lange) Date: Fri, 16 Feb 2018 18:30:08 +0100 Subject: [Tkinter-discuss] listdir In-Reply-To: <0BC70B5D93E054469872FFD0FE07220E01D00318FA@CERNXCHG53.cern.ch> References: <0BC70B5D93E054469872FFD0FE07220E01D0030B3D@CERNXCHG53.cern.ch> <20180215203211.958526481ef7e862664f0d8f@web.de> <0BC70B5D93E054469872FFD0FE07220E01D0031693@CERNXCHG53.cern.ch> <20180216102459.0842feb3ddb03f688afdcc0a@web.de> <0BC70B5D93E054469872FFD0FE07220E01D00318FA@CERNXCHG53.cern.ch> Message-ID: <20180216183008.4f555d34257be07c11e9559f@web.de> Hi, On Fri, 16 Feb 2018 09:51:33 +0000 Vasilis Vlachoudis wrote: > When I run your program > > Download$ python codec.py > utf-8 > Download$ LC_ALL=C python codec.py > ansi_x3.4-1968 > Download$ LC_ALL=en_US.UTF-8 python codec.py > utf-8 > > it seems that python properly recognizes the LC_ALL setting maybe some other environment variable causes confusion, it seems like LC_CTYPE can cause problems, AFAIR that's why it's overridden in my function call. The following issue I found at bugs.python.org talks about this (not the same as your problem, but somehow similar looking; seems like even a terminal program can be the culprit :) https://bugs.python.org/issue18378 Best regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. Sometimes a feeling is all we humans have to go on. -- Kirk, "A Taste of Armageddon", stardate 3193.9 From c.buhtz at posteo.jp Fri Feb 16 16:06:58 2018 From: c.buhtz at posteo.jp (c.buhtz at posteo.jp) Date: Fri, 16 Feb 2018 22:06:58 +0100 Subject: [Tkinter-discuss] multicolumn list/tree with images In-Reply-To: <20180215111928.73048f2ad7c86970135ee259@web.de> References: <3zh0KL3CBnz9rxH@submission02.posteo.de> <20180215111928.73048f2ad7c86970135ee259@web.de> Message-ID: <3zjm136bH1z9rxW@submission02.posteo.de> Dear Michael, thank you very much for your valuable reply. > there does not seem to be a way to > make only the "checkbutton" icon sensitive to mouse clicks The event object have the exact coordinates of the click position. Based on them you can identify the row and column. Quick & Dirty example def on_click(event): iid = event.widget.selection()[0] col = event.widget.identify(component='column', x=event.x, y=event.y) print('iid: {} column: {}'.format(iid, col)) > There are several third party solutions available, though. I know. But I had some problems with their documentation, missing support channels, inactive community, unclear project status, ... > TkinterTreectrl is a wrapper for the tktreectrl tcl extension > ... > https://sourceforge.net/projects/tktreectrl/ > https://sourceforge.net/projects/tkintertreectrl/ What is the difference compared to tkinter.Treeview? What is the difference between your two links? Which one is more active and up to date? There is also a fork on GitHub where I opened an Issue about project status https://github.com/apnadkarni/tktreectrl/issues/1 No examples no documentation. > able to do this, like tablelist and maybe tktable, you can look at > this archived page for more information and download links: > > https://web.archive.org/web/20140412080251/http://tkinter.unpythonic.net:80/wiki/Widgets Very important link - good to have the archive! I checked the links: Some dead, some unclear about project status, features, screenshots, docu. I wrote some mails, issues and bug-tickets to be clearer in this points. "TkTable" https://sourceforge.net/p/tktable/bugs/318/ Very interesting sounds the "The Multi-Column Listbox and Tree Widget Package Tablelist 6.0" where screenshots are available, too. But it is pure Tcl code. I don't know how to use that with Python3. There is some quick and dirty code around on the web. But no official supported or infos about how much it fits to the Tcl code. No repository or support channel. Pmw megawidgets Website currently offline. No docu, no examples, no screenshots. > Finally there seems to be a tix.TList widget which might be able to do > what you want, though I never used it myself, see > https://docs.python.org/3/library/tkinter.tix.html The docu is currently offline. Don't know how to use or if it fit my needs. The little words about tix in the official Python3 docu doesn't help. No code no examples no features or screenshots. That is the situation. From klappnase at web.de Fri Feb 16 17:49:09 2018 From: klappnase at web.de (Michael Lange) Date: Fri, 16 Feb 2018 23:49:09 +0100 Subject: [Tkinter-discuss] multicolumn list/tree with images In-Reply-To: <3zjm136bH1z9rxW@submission02.posteo.de> References: <3zh0KL3CBnz9rxH@submission02.posteo.de> <20180215111928.73048f2ad7c86970135ee259@web.de> <3zjm136bH1z9rxW@submission02.posteo.de> Message-ID: <20180216234909.9f83aef8f00e24748af1ab06@web.de> On Fri, 16 Feb 2018 22:06:58 +0100 wrote: > Dear Michael, > > thank you very much for your valuable reply. > > > there does not seem to be a way to > > make only the "checkbutton" icon sensitive to mouse clicks > > The event object have the exact coordinates of the click position. > Based on them you can identify the row and column. Quick & Dirty > example > > def on_click(event): > iid = event.widget.selection()[0] > col = event.widget.identify(component='column', > x=event.x, y=event.y) > print('iid: {} column: {}'.format(iid, col)) > Yes, but that column includes a few pixels of white space on the left of the checkbox and a few pixels of space (including the space the "active" rectangle takes) to the checkbox's right. At least I could not find a way to work around this without having to try something rather fancy with absolute screen coordinates which I thought would not be worth the effort and besides bear potential for bugs. > > > There are several third party solutions available, though. > > I know. But I had some problems with their documentation, missing > support channels, inactive community, unclear project status, ... > > > TkinterTreectrl is a wrapper for the tktreectrl tcl extension > > ... > > https://sourceforge.net/projects/tktreectrl/ > > https://sourceforge.net/projects/tkintertreectrl/ > > What is the difference compared to tkinter.Treeview? > What is the difference between your two links? Which one is more active > and up to date? There is also a fork on GitHub where I opened an Issue > about project status > https://github.com/apnadkarni/tktreectrl/issues/1 > No examples no documentation. To learn about the difference between the ttk.Treeview and the tktreectrl widget I would like to be able to point you to their web page, but unfortunately sf still appears to be in service. There is an archived page available at: https://web.archive.org/web/20170301130644/http://tktreectrl.sourceforge.net/ Just look at the screenshots and you will get an idea about how much more capable this widget is than the ttk Treeview. The difference between the two links is, that *tktreectrl* is the tk extension library (written in C, windows binaries are available on the sf download site, linux binaries included in most recent distributions) and *tkintertreectrl* is a Python wrapper module I wrote for it (both Python2 and -3). The docs on sf are of course currently down with the rest of their pages, but are included in the download, plus a few very simple demos. In case of questions you can always ask me :) > > > able to do this, like tablelist and maybe tktable, you can look at > > this archived page for more information and download links: > > > > https://web.archive.org/web/20140412080251/http://tkinter.unpythonic.net:80/wiki/Widgets > > Very important link - good to have the archive! > > I checked the links: Some dead, some unclear about project status, > features, screenshots, docu. I wrote some mails, issues and bug-tickets > to be clearer in this points. > > "TkTable" > https://sourceforge.net/p/tktable/bugs/318/ I guess I could answer what you asked there :) The last release was made in 2008. I bet it still works :) > > Very interesting sounds the "The Multi-Column Listbox and Tree Widget > Package Tablelist 6.0" where screenshots are available, too. > > But it is pure Tcl code. I don't know how to use that with Python3. > There is some quick and dirty code around on the web. But no official > supported or infos about how much it fits to the Tcl code. No > repository or support channel. Kevin Walzer's python wrapper for tablelist is available here: https://web.archive.org/web/20150920011432/http://tkinter.unpythonic.net/wiki/TableListWrapper Looks like Python2 only, but it shouldn't be a problem to adapt it to Python3. Personally I never used it, but I have been hearing good things about it. I think Kevin probably still reads the list, so if you ask questions about it here there might be a good chance to get help. > > Pmw megawidgets > > Website currently offline. No docu, no examples, no screenshots. There is actually excellent documentation about Pmw on their page and will hopefully soon be available again once sf have restored their content. Probably downloads will still work, and they include the documentation. However, unless something new showed up recently there is no multi colomn listbox widget with icon support included. > > > Finally there seems to be a tix.TList widget which might be able to do > > what you want, though I never used it myself, see > > https://docs.python.org/3/library/tkinter.tix.html > The docu is currently offline. Seems like right now we are learning something about the bad side effects of monopoly structures... :) > Don't know how to use or if it fit my > needs. The little words about tix in the official Python3 docu doesn't > help. No code no examples no features or screenshots. Probably the pydoc documentation is of more help. Or just look into tix.py :) Tix itself comes with a number of tcl demos, which maybe give you an impression. > > That is the situation. Agreed :) Best regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. The sooner our happiness together begins, the longer it will last. -- Miramanee, "The Paradise Syndrome", stardate 4842.6 From Vasilis.Vlachoudis at cern.ch Fri Feb 16 04:51:33 2018 From: Vasilis.Vlachoudis at cern.ch (Vasilis Vlachoudis) Date: Fri, 16 Feb 2018 09:51:33 +0000 Subject: [Tkinter-discuss] listdir In-Reply-To: <20180216102459.0842feb3ddb03f688afdcc0a@web.de> References: <0BC70B5D93E054469872FFD0FE07220E01D0030B3D@CERNXCHG53.cern.ch> <20180215203211.958526481ef7e862664f0d8f@web.de> <0BC70B5D93E054469872FFD0FE07220E01D0031693@CERNXCHG53.cern.ch>, <20180216102459.0842feb3ddb03f688afdcc0a@web.de> Message-ID: <0BC70B5D93E054469872FFD0FE07220E01D00318FA@CERNXCHG53.cern.ch> When I run your program Download$ python codec.py utf-8 Download$ LC_ALL=C python codec.py ansi_x3.4-1968 Download$ LC_ALL=en_US.UTF-8 python codec.py utf-8 it seems that python properly recognizes the LC_ALL setting Cheers Vasilis ________________________________________ From: Michael Lange [klappnase at web.de] Sent: Friday, February 16, 2018 10:24 To: Vasilis Vlachoudis Cc: tkinter-discuss at python.org Subject: Re: [Tkinter-discuss] listdir Hi, On Fri, 16 Feb 2018 08:49:01 +0000 Vasilis Vlachoudis wrote: > Thank you Michael, > > my locale is UTF-8 when I get the error > > $ locale > LANG=en_US.UTF-8 > LANGUAGE=en_US.UTF-8 > LC_CTYPE="en_US.UTF-8" > ... > LC_ALL=en_US.UTF-8 ok. Don't know if it is possible that there is something in the configuration that stops python from properly detecting the system encoding? Some ten years ago (while using Python 2.3 :) I wrote myself a function that tries to detect the system encoding (see below); since it is so old some of the things that function does may be obsolete now, but I still use it today in a number of my projects and it still works. If you like you can run this snippet as a test script and see what it says. Of course it is also well possible that my guess was wrong and the problem lies somewhere else. Best regards Michael ################################################### import codecs import locale def _find_codec(encoding): # return True if the requested codec is available, else return False try: codecs.lookup(encoding) return 1 except (TypeError, LookupError): return 0 def _sysencoding(): # try to guess the system default encoding # this is mainly stolen from IDLE's IOBinding.py # thanks for guidance to Martin v. Loewis locale.setlocale(locale.LC_CTYPE, "")# don't mess the numeric types here, we might replace the decimal point with a comma and break Tk! try: enc = locale.getpreferredencoding() if enc and _find_codec(enc): return enc except AttributeError: # our python is too old, try something else pass try: enc = locale.nl_langinfo(locale.CODESET) if enc and _find_codec(enc): return enc except AttributeError: pass # the last try try: enc = locale.getdefaultlocale()[1] if enc and _find_codec(enc): return enc except ValueError: pass # aargh, nothing good found, fall back to ascii and hope for the best print 'warning: unable to find usable encoding, using ascii.' return 'ascii' print(_sysencoding().lower()) ################################################### .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. Vulcans believe peace should not depend on force. -- Amanda, "Journey to Babel", stardate 3842.3 From c.buhtz at posteo.jp Sun Feb 18 07:53:27 2018 From: c.buhtz at posteo.jp (c.buhtz at posteo.jp) Date: Sun, 18 Feb 2018 13:53:27 +0100 Subject: [Tkinter-discuss] multicolumn list/tree with images In-Reply-To: <20180216234909.9f83aef8f00e24748af1ab06@web.de> References: <3zh0KL3CBnz9rxH@submission02.posteo.de> <20180215111928.73048f2ad7c86970135ee259@web.de> <3zjm136bH1z9rxW@submission02.posteo.de> <20180216234909.9f83aef8f00e24748af1ab06@web.de> Message-ID: <3zkmyg4THGz9rxX@submission02.posteo.de> On 2018-02-16 23:49 Michael Lange wrote: > > > Finally there seems to be a tix.TList widget which might be able > > > to do what you want, though I never used it myself, see > > > https://docs.python.org/3/library/tkinter.tix.html > > The docu is currently offline. > > Seems like right now we are learning something about the bad side > effects of monopoly structures... :) > > > Don't know how to use or if it fit my > > needs. The little words about tix in the official Python3 docu > > doesn't help. No code no examples no features or screenshots. > > Probably the pydoc documentation is of more help. Or just look into > tix.py :) > Tix itself comes with a number of tcl demos, which maybe give you > an impression. No docu about tix. There is only tcl code. No Python. The pydoc docu doesn't help, too. I don't see how to add columns to any of the listbox alike widgets or if is is even possible. From klappnase at web.de Sun Feb 18 08:59:02 2018 From: klappnase at web.de (Michael Lange) Date: Sun, 18 Feb 2018 14:59:02 +0100 Subject: [Tkinter-discuss] multicolumn list/tree with images In-Reply-To: <3zkmyg4THGz9rxX@submission02.posteo.de> References: <3zh0KL3CBnz9rxH@submission02.posteo.de> <20180215111928.73048f2ad7c86970135ee259@web.de> <3zjm136bH1z9rxW@submission02.posteo.de> <20180216234909.9f83aef8f00e24748af1ab06@web.de> <3zkmyg4THGz9rxX@submission02.posteo.de> Message-ID: <20180218145902.56a710f73ae9c4659eeecd3f@web.de> Hi, On Sun, 18 Feb 2018 13:53:27 +0100 wrote: (...) > No docu about tix. There is only tcl code. No Python. The pydoc docu > doesn't help, too. I don't see how to add columns to any of the listbox > alike widgets or if is is even possible. in debian there is a demo in the tix-dev package at /usr/share/doc/tix/examples/tixwidgets.tcl where you can have a look at the features of the tix widgets. But maybe you are right and tix simply does not offer something of the kind you are looking for. Best regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. Schshschshchsch. -- The Gorn, "Arena", stardate 3046.2 From Vasilis.Vlachoudis at cern.ch Wed Feb 28 05:59:31 2018 From: Vasilis.Vlachoudis at cern.ch (Vasilis Vlachoudis) Date: Wed, 28 Feb 2018 10:59:31 +0000 Subject: [Tkinter-discuss] clipboard and bytes Message-ID: <0BC70B5D93E054469872FFD0FE07220E01D003E49A@CERNXCHG53.cern.ch> Hi all, I took the decision to convert my program to python3. I am stuck with the clipboard handling str vs bytes How can I get/receive bytes instead of str with the clipboard? What I was doing up to now was opening a StringIO stream dump a header and the objects as a dump from Pickler Up to now in python V2. it was working like this Copy to clilpboard: target1 = "" sio = io.StringIO() sio.write(target1) pickler = Pickler(sio) pickler.dump(....) root.clipboard._clear() root.clipboard_append(sio.getvalue()) Paste from clipboard: clip = root.clipboard_get(selection='CLIPBOARD') if clip.startswith(target1): pickler = Unpickler(io.StringIO(clipboard[len(target1):])) ... elif... In python3 Pickler requires a byte stream so I replaced all StringIO() to BytesIO() and the targets with bytes target1 = b"" Copying to clipboard work ok (or I believe so) Pasting, clipboard_get() returns a str not bytes and the Unpickler fails. Also If I try to encode("UTF-8") the clip data also it fails. Thanks in advance Vasilis From klappnase at web.de Wed Feb 28 14:42:51 2018 From: klappnase at web.de (Michael Lange) Date: Wed, 28 Feb 2018 20:42:51 +0100 Subject: [Tkinter-discuss] clipboard and bytes In-Reply-To: <0BC70B5D93E054469872FFD0FE07220E01D003E49A@CERNXCHG53.cern.ch> References: <0BC70B5D93E054469872FFD0FE07220E01D003E49A@CERNXCHG53.cern.ch> Message-ID: <20180228204251.8597f08f59be517b00a7774a@web.de> Hi, On Wed, 28 Feb 2018 10:59:31 +0000 Vasilis Vlachoudis wrote: (...) > In python3 Pickler requires a byte stream so I replaced all StringIO() > to BytesIO() and the targets with bytes target1 = b"" > Copying to clipboard work ok (or I believe so) > Pasting, clipboard_get() returns a str not bytes and the Unpickler > fails. Also If I try to encode("UTF-8") the clip data also it fails. according to man clipboard it is possible create custom data types to be stored in the tk clipboard: "You can put custom data into the clipboard by using a custom -type option. This is not necessarily portable, but can be very useful. The method of passing Tcl scripts this way is effective, but should be mixed with safe interpreters in production code." I set up a (rather useless) example how this can be done: ################################## from tkinter import * root = Tk() def copy(string): def cp(): return 666 copyfunc = (root.register(cp)) return(copyfunc) root.clipboard_clear() root.clipboard_append(copy('foobar'), type='foo') def paste(ev): clip = root.clipboard_get(type='foo') res = root.tk.call(clip) print('->', res, type(res)) root.bind('', paste) root.mainloop() ################################# When I run this script and hit F1 I get the following output: $ python3 test5.py -> 666 So at least this primitive seems to work. Maybe you can use this technique to achieve what you want. Regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. Without followers, evil cannot spread. -- Spock, "And The Children Shall Lead", stardate 5029.5 From klappnase at web.de Wed Feb 28 18:12:13 2018 From: klappnase at web.de (Michael Lange) Date: Thu, 1 Mar 2018 00:12:13 +0100 Subject: [Tkinter-discuss] clipboard and bytes In-Reply-To: <20180228204251.8597f08f59be517b00a7774a@web.de> References: <0BC70B5D93E054469872FFD0FE07220E01D003E49A@CERNXCHG53.cern.ch> <20180228204251.8597f08f59be517b00a7774a@web.de> Message-ID: <20180301001213.338b98c503e4c7dc73afac5e@web.de> On Wed, 28 Feb 2018 20:42:51 +0100 Michael Lange wrote: (...) > So at least this primitive seems to work. Maybe you can use this > technique to achieve what you want. Or maybe this slightly modified example comes closer to what you are looking for: from tkinter import * root = Tk() def copy(string): def cp(): return string copyfunc = (root.register(cp)) return(copyfunc) root.clipboard_clear() root.clipboard_append(copy(b'foobar'), type='foo') def paste(ev): clip = root.clipboard_get(type='foo') res = root.tk.call(clip) print('->', res, type(res)) root.bind('', paste) root.mainloop() Regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. "... freedom ... is a worship word..." "It is our worship word too." -- Cloud William and Kirk, "The Omega Glory", stardate unknown