Convert Code from CodeSkulptor to PyCharm - python-2.7

I am learning python from coursera. They are using CodeSkulptor(http://www.codeskulptor.org/) as IDE.
I want to write the same program in Pycharm.
I am unaware of inbuilt GUI packages in PyCharm(Python 2.7).
I want to convert below code. Please provide me some useful URL's/Important info.
Here is my code:
# define event handlers for control panel
def foo():print "hello world!!!"
# create frame
f = simplegui.create_frame("Guess The Number",300,300)
f.add_button("Range [0,100)", foo, 150)
f.add_button("Range [0,1000)", foo, 150)
f.add_input("Enter a guess", foo, 150)

You can read the documentation here
https://pypi.python.org/pypi/SimpleGUITk
Instead of
import simplegui
you should use
import simpleguitk as simplegui
Reference: How to integrate SimpleGUI with Python 2.7 and 3.0 shell
Please check this link also

Related

Better looking Tkinter File Dialogs for Python 2.7

In my application, the user needs to browse for files. However, the askdirectory from tkFileDialog isn't really comfortable for using and browsing files since it's somewhat outdated.
It looks like this:
What I want to achieve should look like the "default" windows browse dialog. Like this:
(Source https://www.pythontutorial.net/tkinter/tkinter-open-file-dialog/)
I am not sure (since I couldn't find proof) but I remember someone telling me that it looks like this because I am using Python 2.7 and not 3+.
Is that true? Does an alternative exist?
It seems it has something to do with your version, as I have done a bit of research and I am using python 3 with the included tkinter and it shows a normal windows explorer popup. So, if you are using one of the up-to-date versions, it should be the OS' default. (I am unable to test that as python 2 will not work properly on my machine although I can confirm since you are using an older one)
I recreated your case with this code:
from tkinter import *
root = Tk()
filedialog.askdirectory()
root.mainloop()
You can try using askopenfilename(). It displays the standard Open File dialog box.
For example:
from tkinter import *
from tkinter import filedialog as fd
root = Tk()
root.title("Button to open files")
root.geometry("500x500")
def openfd(*args):
askk = askopenfilename()
btn = Button(root, text="Click to open file", command=openfd)
btn.place(x=200, y=200)
root.mainloop()
You can read more about it at https://www.pythontutorial.net/tkinter/tkinter-open-file-dialog/.

Auto-save function implementation with Python and Tkinter

This might be a general question. I'm modifying a Python code wrote by former colleague. The main purpose of the code is
Read some file from local
Pop out a GUI to do some modification
Save the file to local
The GUI is wrote with Python and Tkinter. I'm not very familiar with Tkinter actually. Right now, I want to implement an auto-save function, which runs alongside Tkinter's mainloop(), and save modified files automatically for every 5 minutes. I think I will need a second thread to do this. But I'm not sure how. Any ideas or examples will be much appreciated!! Thanks
Just like the comment says, use 'after' recursion.
import Tkinter
root = Tkinter.Tk()
def autosave():
# do something you want
root.after(60000 * 5, autosave) # time in milliseconds
autosave()
root.mainloop()
Threaded solution is possible too:
import threading
import time
import Tkinter
root = Tkinter.Tk()
def autosave():
while True:
# do something you want
time.sleep(60 * 5)
saver = threading.Thread(target=autosave)
saver.start()
root.mainloop()
before leaving I use sys.exit() to kill all running threads and gui. Not sure is it proper way to do it or not.

Is there a way to access python code from third party kodi/xbmc plugins in your plugin?

I have experience with python, but I just got started learning how to develop addons for Kodi. Having a bit of trouble understanding the docs.
Is it possible to import or otherwise access python code from another plugin or script?
For example if my addon was: script.hello.world and i wanted to use some_method from plugin.video.someplugin.
addon.xml imports the plugin i wish to access:
<requires>
<import addon="xbmc.python" version="2.14.0"/>
<import addon="plugin.video.plexbmc" version="3.4.5" optional="true"/>
</requires>
I was fairly sure this would not work, and i was correct:
from plugin.video.someplugin.default import some_method
The only thing in the docs that looked like it might work was this:
spi = xbmcaddon.Addon ('plugin.video.someplugin')
I can access the xbmc's built in methods of spi, but no way to get to the actual python objects.
Got it! Simply add the desired directory to the system's python path:
spi = xbmcaddon.Addon ('plugin.video.someplugin')
path = spi.getAddonInfo('path')
sys.path.append (xbmc.translatePath( os.path.join( path) ))
from default import some_method
some_method()

Issue with configure_traits when using Enthought Canopy

I was following the tutorial "Writing a graphical applications for scientific programming using TraitsUI
http://code.enthought.com/projects/traits/docs/html/tutorials/traits_ui_scientific_app.html
and tested the following code snippet:
from enthought.traits.api import *
from enthought.traits.ui.api import *
class Camera(HasTraits):
""" Camera object """
gain = Enum(1, 2, 3,
desc="the gain index of the camera",
label="gain", )
exposure = CInt(10,
desc="the exposure time, in ms",
label="Exposure", )
def capture(self):
""" Captures an image on the camera and returns it """
print "capturing an image at %i ms exposure, gain: %i" % (
self.exposure, self.gain )
if __name__ == "__main__":
camera = Camera()
camera.configure_traits()
camera.capture()
If I run this at the command line it works as advertised. A GUI pops up. You adjust the parameters, and when you click "OK" it returns the modified values. But when I run the same code from within the Canopy editor by clicking the run button the default parameters print immediately; then the window pops up. When you then adjust the parameters in the GUI and click "OK" the GUI exits but the new parameter values don't print.
It is as if somehow camera.capture() is running before camera.configure_traits.
First, I would suggest using this newer version of the tutorial: http://docs.enthought.com/traitsui/tutorials/traits_ui_scientific_app.html
The one you linked to references materials for TraitsUI version 3, whereas the one above is for the version you're likely using (version 4). The newer tutorial uses the newer module names, traitsui.api instead of enthought.traits.ui.api for example.
As to why Canopy displays the values immediately, this is the expected behavior when running the program:
if __name__ == "__main__":
camera = Camera()
camera.configure_traits()
camera.capture()
When run as __main__ (i.e., not imported as a module by another script), the script does these three things in order: creates an instance of Camera(), pops up the GUI (configure_traits), and then executes the capture method that prints the current values (which are "1" and "10" by default).
The OK/Cancel buttons are not hooked into setting these values. As a test, try changing the exposure or gain but instead of clicking the buttons, try inspecting these attributes from within Canopy's IPython prompt while the GUI is still open: camera.gain or camera.exposure should return the newly set values.

textinput() equivalent in python 2.x

I need to accept info from the user in python's turtle. however the turtle.textinput function only works in versions 3.x and upwards is there an alternative I can use in version 2.X ?
This is what textinput actually does:
import tkSimpleDialog
tkSimpleDialog.askstring('title', 'prompt')
It requires you to have a turtle screen open, of course.