Re: [Tutor] sort() method and non-ASCII
On Sun, Feb 5, 2017 at 3:52 AM, boB Steppwrote: > Does the list sort() method (and other sort methods in Python) just go > by the hex value assigned to each symbol to determine sort order in > whichever Unicode encoding chart is being implemented? list.sort uses a less-than comparison. What you really want to know is how Python compares strings. They're compared by ordinal at corresponding indexes, i.e. ord(s1[i]) vs ord(s2[i]) for i less than min(len(s1), len(s2)). This gets a bit interesting when you're comparing characters that have composed and decomposed Unicode forms, i.e. a single code vs multiple combining codes. For example: >>> s1 = '\xc7' >>> s2 = 'C' + '\u0327' >>> print(s1, s2) Ç Ç >>> s2 < s1 True where U+0327 is a combining cedilla. As characters, s1 and s2 are the same. However, codewise s2 is less than s1 because 0x43 ("C") is less than 0xc7 ("Ç"). In this case you can first normalize the strings to either composed or decomposed form [1]. For example: >>> strings = ['\xc7', 'C\u0327', 'D'] >>> sorted(strings) ['Ç', 'D', 'Ç'] >>> norm_nfc = functools.partial(unicodedata.normalize, 'NFC') >>> sorted(strings, key=norm_nfc) ['D', 'Ç', 'Ç'] [1]: https://en.wikipedia.org/wiki/Unicode_equivalence#Normal_forms ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Function annotations
On Sat, Feb 4, 2017 at 9:23 PM, Steven D'Apranowrote: > On Sat, Feb 04, 2017 at 08:50:00PM -0600, boB Stepp wrote: >> Of course, these are >> apparently optional. I now wonder if I should be endeavoring to add >> these to my code? > > Do you run a linter? If not, there doesn't seem much point in adding > annotations. To date I have avoided adding a linter to vim/gvim as I believe I get more educational benefit from making and correcting mistakes that a linter would catch. But I have it in the back of mind if I ever reach a relative point of Python comfort to implement linting capabilities. Are the people making linters implementing checking function annotations? Or is this something only gradually being adopted? Steve, are you making use of function annotations? If yes, are you finding them worth the extra effort? Cheers! boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] sort() method and non-ASCII
Does the list sort() method (and other sort methods in Python) just go by the hex value assigned to each symbol to determine sort order in whichever Unicode encoding chart is being implemented? If yes, then my expectation would be that the French "á" would come after the "z" character. I am not ready to get into the guts of Unicode. I am quite happy now to leave Python 3 at its default UTF-8 and strictly type in the ASCII subset of UTF-8. But I know I will eventually have to get into this, so I thought I would ask about sorting so I don't get any evil surprises with some text file I might have to manipulate in the future. Thanks! -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Function annotations
On Sat, Feb 04, 2017 at 08:50:00PM -0600, boB Stepp wrote: > I just finished looking at > https://docs.python.org/3/tutorial/controlflow.html#function-annotations > and skimming through PEP 484--Type Hints > (https://www.python.org/dev/peps/pep-0484/). My initial impression is > that the purpose of function annotations is to enable static code > analysis tools like linters to be more effective. That's exactly what they're for. > Aesthetically, to > my eye it makes the function definition line more cluttered looking > and more difficult to interpret at a glance. Obviously they take up more room, but they also provide more information: the intended type of the argument, > Of course, these are > apparently optional. I now wonder if I should be endeavoring to add > these to my code? Do you run a linter? If not, there doesn't seem much point in adding annotations. > It is not clear to me how to implement function annotations in all > instances. Starting with a rather useless function: > > def prt_stupid_msg(): > print('Duh!') > > This takes no arguments and returns None. While this particular > function is rather useless, many others of some utility may take no > arguments and return None. How should these types of functions be > annotated? Since it take no arguments, you cannot annotate the arguments it doesn't have. You could annotate the return value: def prt_stupid_msg() -> None: print('Duh!') but that's unlikely to be useful. I wouldn't bother. > What about: > > def print_stuff(stuff): > print(stuff) > > Should this be annotated as: > > def print_stuff(stuff: Any) -> None: > > ? Probably not. If you don't annotate the function, the linter should just assume it takes anything as argument. > What about a function from the tutorial: > > def make_incrementor(n): > return lambda x: x + n > > n might always be an int or might always be a float or might be > either, depending on the author's intent. Therefore, how should n be > annotated? In accordance with the author's intentions? Of course. > And what > about the case where n is sometimes an integer and sometimes a float? That's a Union of two types. Think of Union as meaning "this OR that". > And the return in this instance is a function. Should the return be > annotated "function"? from typing import Union from types import FunctionType def make_incrementor(n: Union[int, float]) -> FunctionType: return lambda x: x + n > And what about functions that return different types depending on > conditional statements? Probably a bad design... > How would these varying return types be annotated? Say: > > def return_typed_value(desired_type, value): > if desired_type = 'string': > return str(value) > elif desired_type = 'integer': > return int(value) > elif desired_type = 'float': > return float(value) > > What should I do with this? Redesign it. def return_typed_value( desired_type: str, value: Any) -> Union[str, int, float]: ... > As written "desired_type" will always be > a string, but "value" will be potentially several different types. If you want to restrict the type of `value` depending on the value of `desired_type`, you cannot expect the compiler or linter or type-checker to do this at compile time. In general, it doesn't know what the value of `desired_type` is, so it can't tell whether the type of `value` is valid or not. You would have to use a *dynamic* type check, rather than static: def return_typed_value(desired_type, value): if desired_type == 'string': if isinstance(value, (int, float)): return str(value) elif desired_type == 'integer': if isinstance(value, (float, str)): return int(value) elif desired_type == 'float': if isinstance(value, (int, str)): return float(value) raise TypeError When your type checking rules become this intricate, it's probably a bad idea. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Function annotations
I just finished looking at https://docs.python.org/3/tutorial/controlflow.html#function-annotations and skimming through PEP 484--Type Hints (https://www.python.org/dev/peps/pep-0484/). My initial impression is that the purpose of function annotations is to enable static code analysis tools like linters to be more effective. Aesthetically, to my eye it makes the function definition line more cluttered looking and more difficult to interpret at a glance. Of course, these are apparently optional. I now wonder if I should be endeavoring to add these to my code? It is not clear to me how to implement function annotations in all instances. Starting with a rather useless function: def prt_stupid_msg(): print('Duh!') This takes no arguments and returns None. While this particular function is rather useless, many others of some utility may take no arguments and return None. How should these types of functions be annotated? What about: def print_stuff(stuff): print(stuff) Should this be annotated as: def print_stuff(stuff: Any) -> None: ? What about a function from the tutorial: def make_incrementor(n): return lambda x: x + n n might always be an int or might always be a float or might be either, depending on the author's intent. Therefore, how should n be annotated? In accordance with the author's intentions? And what about the case where n is sometimes an integer and sometimes a float? And the return in this instance is a function. Should the return be annotated "function"? And what about functions that return different types depending on conditional statements? How would these varying return types be annotated? Say: def return_typed_value(desired_type, value): if desired_type = 'string': return str(value) elif desired_type = 'integer': return int(value) elif desired_type = 'float': return float(value) What should I do with this? As written "desired_type" will always be a string, but "value" will be potentially several different types. And the return value can be one of three types (Assuming no errors occur as no exception handling is being done as written.). What to do about that? I have more questions, but these will do for a start. Just the long PEP 484 document has tons of meat in it to consider, most of which is dealing with aspects of Python I have not gotten to studying yet! -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Syntax error while attempting to type in multiline statements in the interactive interpreter
I'm beginning to believe I am being incredibly dense today ... On Sat, Feb 4, 2017 at 6:16 PM, Ben Finneywrote: > boB Stepp writes: > >> But would it not be more consistent to assume the user knows what he >> is doing based on the new (lack of) indentation being used, accept >> that a new section of code outside of the for loop is being typed in, >> and wait for the blank line before executing everything typed in? > > That's not the purpose of the REPL. It is to Read (one statement), > Evaluate the statement, Print the result, and Loop. > > You're wanting a different behaviour: wait until all the input has been > read, then evaluate it all. Ben, I read your answer, did some Googling on REPL, seemed satisfied, and consigned this thread of emails to the trash bin, believing it was all settled in my mind. But then I got to wondering again ... As already mentioned the interactive interpreter handles multiline statements. In the case of defining a function, the interpreter allows quite complex, nested code to be entered, only ending the input of code process when that blank line occurs in proper context. By this I mean that quotes have been properly closed, all parens matched, etc. But it seems to me on further thought that both REPL and what seems most consistent to me, "...wait until all the input has been read, then evaluate it all..." amounts to the same thing in the case of entering function definitions into the interpreter. The interpreter cannot know that the user has finished inputting code until it encounters that finalizing blank line. After all I can have in my function many for loops followed by unindented print functions in seeming as much complexity as I can bear to type in accurately. This is giving exactly the consistent behavior that I would expect. But if instead of starting with a for loop inside a function with following unindented other statements I instead start with *exactly the same sequence of statements* but without "def ...:", then I get a syntax error. I am not trying to be deliberately argumentative, but I do not see why the two cases are treated differently when the entered code is exactly the same between the two with the sole exception of one has an enclosing function definition line the other code does not. I am continuing to scratch my evermore balding head on this one! But it is not a big deal, and I do not want to blow things out of proportion. More practically, I wonder if it is all as simple as not having to hit the enter key twice for true one-liner statements. Such lines can truly be handled one line at a time per REPL. If I had things "my way", then that would make the more common situation of true one-line-at-a-time read-evaluate-print-loop more cumbersome. -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Syntax error while attempting to type in multiline statements in the interactive interpreter
boB Steppwrites: > But would it not be more consistent to assume the user knows what he > is doing based on the new (lack of) indentation being used, accept > that a new section of code outside of the for loop is being typed in, > and wait for the blank line before executing everything typed in? That's not the purpose of the REPL. It is to Read (one statement), Evaluate the statement, Print the result, and Loop. You're wanting a different behaviour: wait until all the input has been read, then evaluate it all. If that's what you want: just write it all into a text file, and execute that. -- \ “Nature is trying very hard to make us succeed, but nature does | `\ not depend on us. We are not the only experiment.” —Richard | _o__) Buckminster Fuller, 1978-04-30 | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Syntax error while attempting to type in multiline statements in the interactive interpreter
On Sat, Feb 4, 2017 at 5:44 PM, Alan Gauld via Tutorwrote: > On 04/02/17 22:56, boB Stepp wrote: >> On Sat, Feb 4, 2017 at 4:40 PM, David wrote: >>> On 5 February 2017 at 09:02, boB Stepp wrote: py3: a ['Mary', 'had', 'a', 'little', 'lamb', 'break'] py3: for w in a: ... print(w) ... print('Huh?') File "", line 3 print('Huh?') ^ SyntaxError: invalid syntax I don't understand why this throws a SyntaxError. If I wrap essentially the same code into a function it works: > > Because you never completed the for loop. The interactive > interpreter tries to exercise each Python statement as it > goes but it cannot exercise the loop until it sees the end > of the block. Your second print statement is outside the > block but the block has not yet been executed so it sees > an error. But would it not be more consistent to assume the user knows what he is doing based on the new (lack of) indentation being used, accept that a new section of code outside of the for loop is being typed in, and wait for the blank line before executing everything typed in? > > Alternatively if you put your code inside a function > definition it will understand it because it interprets > the full definition. Then when you call the function > it executes it as a whole. There is no discrepency in > what the interpreter is trying to interpret. But when > you do it at the top level the interpreter is still > waiting for the end of the loop. Again, the interpreter is waiting for that blank line before deciding the function definition is complete. Why cannot this be the key criteria for the for loop with additional code being added, wait for the blank line to be entered, and then execute the whole shebang? To me, this seems to be more consistent behavior. > Does that help? It confirms what I was concluding. This is what happens when I have some extra time on my hand, decide to carefully scrutinize the official Python 3 tutorial, and then think, wonder and play around in the interpreter. ~(:>)) -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Syntax error while attempting to type in multiline statements in the interactive interpreter
On 04/02/17 22:56, boB Stepp wrote: > On Sat, Feb 4, 2017 at 4:40 PM, Davidwrote: >> On 5 February 2017 at 09:02, boB Stepp wrote: >>> py3: a >>> ['Mary', 'had', 'a', 'little', 'lamb', 'break'] >>> py3: for w in a: >>> ... print(w) >>> ... print('Huh?') >>> File "", line 3 >>> print('Huh?') >>> ^ >>> SyntaxError: invalid syntax >>> >>> I don't understand why this throws a SyntaxError. If I wrap >>> essentially the same code into a function it works: Because you never completed the for loop. The interactive interpreter tries to exercise each Python statement as it goes but it cannot exercise the loop until it sees the end of the block. Your second print statement is outside the block but the block has not yet been executed so it sees an error. If you put a blank line it will work OK. Alternatively if you put your code inside a function definition it will understand it because it interprets the full definition. Then when you call the function it executes it as a whole. There is no discrepency in what the interpreter is trying to interpret. But when you do it at the top level the interpreter is still waiting for the end of the loop. Does that help? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Syntax error while attempting to type in multiline statements in the interactive interpreter
On 5 February 2017 at 09:56, boB Steppwrote: > On Sat, Feb 4, 2017 at 4:40 PM, David wrote: >>> >>> I don't understand why this throws a SyntaxError. If I wrap >>> essentially the same code into a function it works: >> >> From [1]: "When a compound statement is entered interactively, it must >> be followed by a blank line to indicate completion (since the parser >> cannot guess when you have typed the last line). Note that each line >> within a basic block must be indented by the same amount." >> >> Does that help? > > Not really. I do not understand why I can define a function in the > interactive interpreter and nest (apparently) any number of for loops, > if-elif-else constructs, etc., and as long as I get the indentation of > each block correct, it will accept it. But apparently if I start off > with something which is by definition multiline like typing in a for > loop, I cannot continue the multiline statements with something > outside the for loop even if the indentation indicates that the > following statement is not part of the for loop above. I can accept > this, but I do not understand why this can't be parsed correctly by > the interpreter solely based on the indentation and lack of > indentation being used. I am guessing it is because the interpreter implements only single level of auto-indent, for every top-level compound statement, and it requires that to be terminated with one blank line. It appears that all extra indenting must be done manually. I could be wrong, as I would not work this way. Instead I would use an editor to create the function, and then import it to create the desired initial state which could then be explored interactively. I just tried to give you a quick response. Maybe someone else will give you a more authoritative answer, if there is one. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Syntax error while attempting to type in multiline statements in the interactive interpreter
On Sat, Feb 4, 2017 at 4:40 PM, Davidwrote: > On 5 February 2017 at 09:02, boB Stepp wrote: >> py3: a >> ['Mary', 'had', 'a', 'little', 'lamb', 'break'] >> py3: for w in a: >> ... print(w) >> ... print('Huh?') >> File "", line 3 >> print('Huh?') >> ^ >> SyntaxError: invalid syntax >> >> I don't understand why this throws a SyntaxError. If I wrap >> essentially the same code into a function it works: > > From [1]: "When a compound statement is entered interactively, it must > be followed by a blank line to indicate completion (since the parser > cannot guess when you have typed the last line). Note that each line > within a basic block must be indented by the same amount." > > Does that help? Not really. I do not understand why I can define a function in the interactive interpreter and nest (apparently) any number of for loops, if-elif-else constructs, etc., and as long as I get the indentation of each block correct, it will accept it. But apparently if I start off with something which is by definition multiline like typing in a for loop, I cannot continue the multiline statements with something outside the for loop even if the indentation indicates that the following statement is not part of the for loop above. I can accept this, but I do not understand why this can't be parsed correctly by the interpreter solely based on the indentation and lack of indentation being used. C'est la vie Python! -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Syntax error while attempting to type in multiline statements in the interactive interpreter
On 5 February 2017 at 09:02, boB Steppwrote: > py3: a > ['Mary', 'had', 'a', 'little', 'lamb', 'break'] > py3: for w in a: > ... print(w) > ... print('Huh?') > File "", line 3 > print('Huh?') > ^ > SyntaxError: invalid syntax > > I don't understand why this throws a SyntaxError. If I wrap > essentially the same code into a function it works: >From [1]: "When a compound statement is entered interactively, it must be followed by a blank line to indicate completion (since the parser cannot guess when you have typed the last line). Note that each line within a basic block must be indented by the same amount." Does that help? [1] https://docs.python.org/3/tutorial/introduction.html ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Syntax error while attempting to type in multiline statements in the interactive interpreter
py3: a ['Mary', 'had', 'a', 'little', 'lamb', 'break'] py3: for w in a: ... print(w) ... print('Huh?') File "", line 3 print('Huh?') ^ SyntaxError: invalid syntax I don't understand why this throws a SyntaxError. If I wrap essentially the same code into a function it works: py3: def print_list(a_list): ... for item in a_list: ... print(item) ... print('Huh?') ... py3: print_list(a) Mary had a little lamb break What am I not understanding here? -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Copy/Paste in Tkinter
Hi Alan, Thank you so much for guiding me towards this. I am new to tkinter hence not aware of all these features. I am trying to find some simple examples, of how to use copy/paste/cut. I do not have a text box, but a GUI similar to a word editor. I was wondering if you could direct me towards or show me towards some simple examples of the same, so that it would be easier to understand. I was learning from youtube videos about this. It would be a huge help. Thank you so much Pooja On Sat, Feb 4, 2017 at 5:32 AM, Alan Gauld via Tutorwrote: > On 04/02/17 00:24, Pooja Bhalode wrote: > > > I am trying to write a simple Tkinter code in Python in order to > > demonstrate select/copy/paste/cut functionalities using keyboard keys and > > menu options. > > You are going a fairly long winded way about it. > Rather than generating events use the Text/Entry > widget methods directly. > > Check the documentation for the following methods: > > Text.get() # copy > Text.selection_get(...) # copy a selection > Text.delete(...) # cut > Text.insert(...) # paste > > To control selections look at the tag methods, > but that is more complex. > > > def show_menu(e): > > w = e.widget > > # the_menu.entryconfigure("Select",command=lambda: > > w.event_generate("<>")) > > the_menu.entryconfigure("Cut",command=lambda: > w.event_generate("<>")) > > the_menu.entryconfigure("Copy",command=lambda: > w.event_generate("<>")) > > the_menu.entryconfigure("Paste",command=lambda: > > These should be in the create function. You don't need > to configure the command every time you show the menu. > > > w.event_generate("<>")) > > the_menu.tk.call("tk_popup", the_menu, e.x_root, e.y_root) > > You should only need to call the tk.call() method very, > very rarely, there is usually a more direct way to do things. > > > root = Tkinter.Tk() > > make_menu(root) > > > > e1 = Tkinter.Entry(); e1.pack() > > e2 = Tkinter.Entry(); e2.pack() > > Shouldn't these Entries have a parent? > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using venv
On Fri, Jan 27, 2017 at 7:47 PM, Jimwrote: > > [...] This question seems a little dumb and maybe I am being a little dense, > but then what? Imagine that you are working on 5 different Python projects, each using different packages with different versions. We can break this down in two situations: 1. No virtualenv 1.a. You only have one Lib/site-packages to hold all your packages 1.b. Project A uses package-abc 1.2 while Project B uses package-abc 1.5 1.c. You go to work on Project B using package-abc 1.5, but later on you have to do some changes on Project A so you must downgrade package-abc to 1.2. Too bad you have to fix some bugs on Project B the next day, need to update package-abc back to 1.5 2. Virtualenv 2.a. You had multiple virtualenvs, each of them having their own Lib/site-packages 2.b. Project A uses one virtualenv with package-abc 1.2 while Project B uses another virtualenv with package-abc 1.5 2.c. You go to work on Project B using package-abc 1.5, so you activate its venv, but later on you have to do some changes on Project A so you activate the other venv. Too bad you have to fix some bugs on Project B the next day, activate Project B venv. It's a simple command to activate/deactivate a venv vs all the work you have to do when not using venv. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Copy/Paste in Tkinter
On 04/02/17 16:09, Pooja Bhalode wrote: > I do not have a text box, but a GUI similar to a word editor. That sounds like a text box to me!? What are you using to enter the text if not a Text widget? > I was learning from youtube videos about this. You can start with the GUI topic in my tutorial(see .sig) It contains links to a couple of other Tkinter tutorials on the web. The Python.org Tkinter page also has links. All of these cover text processing to some degree. Finally there are a couple of books to consider: 1) Python & Tkinter by Grayson on Manning Press 2) Programming Python 4th edition which has about 400 pages of Tkinter material, including a text editor. 3) And I can't resist a plug for my recent book "Python Projects" which has a more modest 30 page intro on Tkinter, but includes coverage of the get, delete and insert text methods, as well as how to set fonts and build menus :-) You may be able to get these via your local public/college library. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter Copy/Paste/Cut functionalities
On 04/02/17 02:34, Pooja Bhalode wrote: > I came across some snippets of the code to do this but using a self class. I'm not sure what you mean by a "self class"? > I was wondering if some one could help me create these functionalities in a > manner similar to the following code. (similar to the Quitcommand > function). See my reply to your other post. > Another question I had, was how to make the status bar text as a variable > such that it keeps on displaying what is going on in the program? Its always best to keep separate questions in separate posts - it makes searching the archives easier in the future. However,... That depends on what you want to show in the Status bar. A Status bar normally includes showing help text when the mouse moves over widgets and the status of any component that the cursor enters. That sounds different to what you want? You may be able to use a StriVar variable linked to an Entry on the status bar, or maybe even a Label(can a Label text link to a StrVar? I'm not sure). Alternatively ifv your code controls where the variable gets changed(as it should) then simply update the status bar every time you change that variable. This is one case where a setX type function becomes worthwhile. Then only change X by calling setX()... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Copy/Paste in Tkinter
On 04/02/17 00:24, Pooja Bhalode wrote: > I am trying to write a simple Tkinter code in Python in order to > demonstrate select/copy/paste/cut functionalities using keyboard keys and > menu options. You are going a fairly long winded way about it. Rather than generating events use the Text/Entry widget methods directly. Check the documentation for the following methods: Text.get() # copy Text.selection_get(...) # copy a selection Text.delete(...) # cut Text.insert(...) # paste To control selections look at the tag methods, but that is more complex. > def show_menu(e): > w = e.widget > # the_menu.entryconfigure("Select",command=lambda: > w.event_generate("<>")) > the_menu.entryconfigure("Cut",command=lambda: w.event_generate("<>")) > the_menu.entryconfigure("Copy",command=lambda: w.event_generate("<>")) > the_menu.entryconfigure("Paste",command=lambda: These should be in the create function. You don't need to configure the command every time you show the menu. > w.event_generate("<>")) > the_menu.tk.call("tk_popup", the_menu, e.x_root, e.y_root) You should only need to call the tk.call() method very, very rarely, there is usually a more direct way to do things. > root = Tkinter.Tk() > make_menu(root) > > e1 = Tkinter.Entry(); e1.pack() > e2 = Tkinter.Entry(); e2.pack() Shouldn't these Entries have a parent? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Tkinter Copy/Paste/Cut functionalities
Hi, I am new to tkinter and looking for some help with select/copy/paste functionalities. I wish to keep these tabs in the Edit menu and as well as be able to do it using the shortcut keys. Here, I am trying to create a GUI and needed some help with that. I came across some snippets of the code to do this but using a self class. I was wondering if some one could help me create these functionalities in a manner similar to the following code. (similar to the Quitcommand function). Another question I had, was how to make the status bar text as a variable such that it keeps on displaying what is going on in the program? I would really appreciate your help on this. Thank you so much. Pooja Code: from Tkinter import * import datetime import tkMessageBox root = Tk() root.title("Name of the interface") def Doit(): print "Inside the function" from tkMessageBox import * def Quitcommand(): if askyesno('Verify', 'Are you sure you want to quit?'): if askyesno('Save file', 'Do you want to save the current file?'): print 'Saving file' root.quit() else: showinfo('Quit not implemented.') def LoadNew(): print 'Loading new .gPROMS files' # User enters the name of the to be executed. if tkMessageBox.askyesno('Reading new file','Do you want to read in the new files'): print 'Reading new files. ' def show_about(): print 'HELP function' from tkFileDialog import askopenfilename def OpenFile(): name = askopenfilename() print name ### --- Menu bar menu = Menu(root) root.config(menu=menu) editmenu = Menu(menu, tearoff=0) menu.add_cascade(label="Edit", menu=editmenu) editmenu.add_command(label="Cut",accelerator="Ctrl+X",command=DoCut) editmenu.add_command(label="Copy",accelerator="Ctrl+C",command=DoCopy) editmenu.add_command(label="Paste", accelerator="Ctrl+V"command=DoPaste) editmenu.add_command(label="Make Changes", command=TimeAllowed) editmenu.add_command(label="Line", command=Doit) ### --- Status bar Status = Label(root, text = 'Static',bd=1, relief=SUNKEN, anchor=W) Status.pack(side=BOTTOM, fill=X) root.mainloop() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Copy/Paste in Tkinter
Hi, I am trying to write a simple Tkinter code in Python in order to demonstrate select/copy/paste/cut functionalities using keyboard keys and menu options. I tried looking up online, but I am finding examples of these in which they create an event and then it is used: eg.: import Tkinter def make_menu(w): global the_menu the_menu = Tkinter.Menu(w, tearoff=0) # the_menu.add_command(label="Select") the_menu.add_command(label="Cut") the_menu.add_command(label="Copy") the_menu.add_command(label="Paste") def show_menu(e): w = e.widget # the_menu.entryconfigure("Select",command=lambda: w.event_generate("<>")) the_menu.entryconfigure("Cut",command=lambda: w.event_generate("<>")) the_menu.entryconfigure("Copy",command=lambda: w.event_generate("<>")) the_menu.entryconfigure("Paste",command=lambda: w.event_generate("<>")) the_menu.tk.call("tk_popup", the_menu, e.x_root, e.y_root) root = Tkinter.Tk() make_menu(root) e1 = Tkinter.Entry(); e1.pack() e2 = Tkinter.Entry(); e2.pack() e1.bind_class("Entry", "", show_menu) root.mainloop() I was wondering if someone could please tell me a simplified different way of doing this, that would be great. Thank you. Pooja ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor