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.
Related
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/.
I want to know which version of Tkintr my Python 2.7 is running...
Thanks for the support :)
I have already done:
import Tkinter
Tkinter.TclVersion
but it is not working
The module variable TkVersion will return a floating point number representing the version of the underlying tk library.
For example:
import tkinter as tk
the_version = tk.TkVersion
I'm new to python, and while it's a pretty simple language, I'm having a hard time finding a solid and easy to read language reference that lists all the supported build-in methods and libraries that come with the installation. The main documentation site is confusing. There's more info about what's deprecated than what's recommended. I tried using pydoc to find method usage. For example, I want to see a simple list of all the methods that are part of the string class (e.g. replace(), toupper(), etc). But I'm not sure how to use it to list the methods, or to list a method and its usage. What do people use for a quick reference that works?
When I do something like 'pydoc string', I see a message that says "Warning: most of the code you see here isn't normally used nowadays.
Beginning with Python 1.6, many of these functions are implemented as
methods on the standard string object. They used to be implemented by
a built-in module called strop, but strop is now obsolete itself."
So while there's info about the method replace() there, I'm worried that it's not the right info based on that warning. How can I see the methods of the standard string object?
Documentation about standard functions:
https://docs.python.org/2/library/functions.html
Documentation about standard libraries:
https://docs.python.org/2/library/
You could use dir() and help(). i.e. :
From python shell :
>>> import math
>>> dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
>>> help(math.tan)
Will print :
Help on built-in function tan in module math:
tan(...)
tan(x)
Return the tangent of x (measured in radians).
(press "q" to exit the help page)
Hope it helps.
EDIT
Another solution from the shell :
$ python -m pydoc sys
Then press "q" to exit.
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
I have a small project that uses matplotlib to display a wafer map of die. I am "compiling" the single-file Python (2.7) into an executable using PyInstaller with the --onefile option, so that non-Python users at the company can execute it in Windows.
The executable takes quite a while to load, up to 15s. As a workaround, I removed all the wafer-map plotting capabilities of the program and built a "Lite" version. This Lite version runs in <1s, as it should. In addition, the Lite version's .exe is 85% smaller (as expected).
So it looks like the Matplotlib stuff is bloating the exe and is making it take a long time to load.
Here's my thought process:
I should be able to get the file size down and decrease the load time if I only import the modules I use rather than all of matplotlib.pyplot. I assume that the import matplotlib.pyplot as pyplot line is importing a whole bunch of extra stuff that I'm not using, such as scatterplots.
Here's my question:
How can I only import the parts of matplotlib that I use?
Here's my (relevant) code, with a lot of the fluff (like line colors) removed. Also, please ignore the lack of PEP8 conformity - this was written before I decided to follow it :-)
from __future__ import print_function
import math
import matplotlib.pyplot as pyplot
import matplotlib.patches
fig = pyplot.figure(1)
ax = fig.add_subplot(111, aspect='equal')
ax.axis([xAxisMin, xAxisMax, yAxisMin, yAxisMax])
die = matplotlib.patches.Rectangle(coords, dieX, dieY)
ax.add_patch(die)
arc = matplotlib.patches.Arc((0, 0),
width=exclDia, height=exclDia, angle=-90,
theta1=ang, theta2=-ang)
flat = matplotlib.lines.Line2D([-flatX, flatX],
[flatY, flatY])
# Extra code that actually adds everything to the figure
fig.show()
So it looks like I'm using only:
matplotlib.pyplot.figure
matplotlib.patches.Rectangle
matplotlib.patches.Arc
matplotlib.lines.Line2D
However, those above are not individual modules in matplotlib (to my knowledge) - they are classes of their parent module (patches, lines, pyplot), so I can't just `import matplotlib.patches.Arc' or anything.
So. What's my next step?