Editing Russ Python Tips and Techniques
Jump to navigation
Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
= What = | = What = | ||
− | These are Tips and Techniques I have found useful in my Python | + | These are Tips and Techniques I have found useful in my Python programing [[User:Russ_hensel]]. I would be interested in your feedback. Let me know if you think the tip is useful/wrong/something everyone knows. Most of this material relates to applications with a GUI. They are not beginner techniques. |
− | Just starting Feb 2017 check history | + | Just starting Feb 2017 check history to see if progress is being made. |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
= Parameters for Applications = | = Parameters for Applications = | ||
Line 161: | Line 61: | ||
</pre> | </pre> | ||
− | + | = Complex Tkinker Windows = | |
− | + | I found it really easy to get confused in coding Tkinter because it is not a visual interface. So here are some tips: | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | I found it really easy to get confused in coding Tkinter because it is not | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | So here are some tips: | ||
* I do a sketch of what I want on paper. | * I do a sketch of what I want on paper. | ||
* The GUI is built in a class. | * The GUI is built in a class. | ||
− | * I break the sketch into Tkinter frames each of which will layout into the window in some fairly simple scheme, often the pack layout will work | + | * I break the sketch into Tkinter frames each of which will layout into the window in some fairly simple scheme, often the pack layout will work. |
− | + | * Each individual frame is built in its own subroutine, it takes the parent frame as an argument and returns the frame that it builds. The returned frame is then placed in its parent. | |
− | * Each individual frame is built in its own | + | * References to the widgets are often not needed once the GUI is built, so local variable are often used. For widgets that need references you can use an instance variable of the class, or in some cases it it useful to store them in a dictionary. |
− | * References to the widgets are often not needed once the GUI is built, so local variable are often used | ||
* Building helper classes for either building the widgets, or placing them or both is often useful. | * Building helper classes for either building the widgets, or placing them or both is often useful. | ||
− | * You can see these techniques in GUI class | + | * You can see these techniques in my SmartTerminal GUI class. |
− | |||
− | + | = Threading with Tkinker = | |
Tkinker wants to own the processing of the application. If you make a button start a long running operation then the GUI blocks and becomes unresponsive. So you need some sort of threading. Here are some tips. | Tkinker wants to own the processing of the application. If you make a button start a long running operation then the GUI blocks and becomes unresponsive. So you need some sort of threading. Here are some tips. | ||
− | == | + | == Polling withing Tkinker == |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
You can poll in the Tkinter event loop and call any function ( no arguments I think ) using a Tkinter function. Use this to run something short. You can only run it once this way. | You can poll in the Tkinter event loop and call any function ( no arguments I think ) using a Tkinter function. Use this to run something short. You can only run it once this way. | ||
<pre> | <pre> | ||
Line 322: | Line 108: | ||
</pre> | </pre> | ||
− | + | == Use Threading == | |
Use real threading. I inherit from threading.Thread and implement a override of the run method. Use threading mechanisms to "message" back and fourth to the gui, Tkinter, thread. | Use real threading. I inherit from threading.Thread and implement a override of the run method. Use threading mechanisms to "message" back and fourth to the gui, Tkinter, thread. | ||
Seems to work fine. | Seems to work fine. | ||
− | + | = Calling across Threads = | |
If you have objects in 2 different threads calling across the threads can be very dangerous. I have worked out a method that I find useful/safe/easy. It does make some assumptions. | If you have objects in 2 different threads calling across the threads can be very dangerous. I have worked out a method that I find useful/safe/easy. It does make some assumptions. | ||
Line 333: | Line 119: | ||
*Both objects/threads have something resembling polling where checks can be made at a convient time to see if the other thread wants attention. | *Both objects/threads have something resembling polling where checks can be made at a convient time to see if the other thread wants attention. | ||
− | + | == Step 1, Set Up Queues == | |
Line 372: | Line 158: | ||
more coming...... | more coming...... | ||
− | + | = Starting a Tkinter App = | |
− | These applications start fine but often remain in the background with their window behind other windows. It would be good to bring it to the top of your windows. This code, may not be the best, seems to work at least on the | + | These applications start fine but often remain in the background with their window behind other windows. It would be good to bring it to the top of your windows. This code, may not be the best, seems to work at least on the arduino. |
<pre> | <pre> | ||
root.attributes("-topmost", True) | root.attributes("-topmost", True) | ||
− | root.attributes("-topmost", False) # if left to True window | + | root.attributes("-topmost", False) # if left to True window alway on top |
</pre> | </pre> | ||
You need to get this to run once after the mainloop is started. You can do this with '''root.after()''' Some details left to you. | You need to get this to run once after the mainloop is started. You can do this with '''root.after()''' Some details left to you. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
[[Category:Python]] | [[Category:Python]] |