Text Based Game, Drawing Unicode Python - python

I'm a starting programmer looking to make a simple text based RPG from scratch. I know there might be an easy tool to do this but I want as little handed to me as possible to use this project as a sort of learning possible. I've been using Python and so far I really like it (I'm willing to use Java or Javascript if absolutely necessary.)
My problem though is that right now I'm using the console to run the game but I'd prefer to run it as a standalone application (also so I can distribute it in like an .exe or similar). Is there some simple way I can do this? Everything is in Unicode, so it just needs to be able to display Unicode text (in-line preferably) and have some way to check for key presses (to type commands).
I've looked into Kivy, but it seems far beyond what I need and the text it displays is not in-line and must be displayed line by line. Plus it doesn't seem to be able to be exported to a single file.
Thanks for the help and remember I'm very much a newbie.

If you want a GUI in Python, you can use TkInter, which is fairly easy to learn (https://wiki.python.org/moin/TkInter).
However, if you want to make it an executable so you can share it then you have to use something like the following:
cx_freeze (http://cx-freeze.sourceforge.net/)
py2exe(http://www.py2exe.org)
PyInstaller(http://www.pyinstaller.org)
These will 'freeze' your Python scripts by including the interpreter and libraries in the .exe file. There's a lot of information in this previously asked question; How do i convert a Python program to a runnable .exe Windows program?

Here's a basic example of a text thing in tkinter:
from tkinter import *
root = Tk()
playerEntry = Entry(root)
textLabel = Label(root, justify=LEFT)
playerEntry.pack()
textLabel.pack()
def changeText(addText):
textLabel.config(text = textLabel["text"] + addText + "\n")
def get(event):
changeText(">>> %s" % playerEntry.get())
do_stuff()
playerEntry.delete(0, END)
def do_stuff():
changeText("Stuff is happening")
playerEntry.bind("<Return>", get)
root.mainloop()

Related

How do I clear text on shell?

I need to clear the IDLE shell, using code. The only way I know of to remove the text is closing the shell and reopening. I want this to be able to put into code that requires refreshing the shell, for example a memory game, giving a string of words, and then removing them, or some kind of animation made of text pictures.
Essentially I want to do something like shell.clear() or something similarly easy to use. It can be a function or whatever, but I'd like it to be easy to put into some preexisting code.
I do not know if such a thing is possible, but if you have any pointers, tips or code, I'd appreciate the help.
IDLE 3.7.3 on Mac.
You can use:
from os import system
#for windows
system('cls')
#for Unix based systems
system('clear')
Or if you are inside IDLE or Python interpreter you can use this function:
def cls(): print ("\n" * 100)
That you can call cls() whenever you need to clear your screen.

Ubuntu python: interface for running multiple script

I've created a few python scripts that I have to run every once in a while, but now I constantly have to type or search the location to the script in order to execute it. I have been looking for simple software to create an app with buttons, but without success. I have also seen "python?" scripts with an interface where you can select options with arrow keys, but also for this I have not found how it is done. Is there anyone who knows how I can make one of these that I can open the interface and select the script I want to execute? It would really save me a lot of time.
You can Tkinter to create the GUI, and for each button create a function that runs your python script like this:
# Create the button
button1 = Button \
(root, text='Start script1!', command=lambda: script1())
def script1():
os.system('python ~/path/to/script1.py')
def script2():
os.system('python ~/path/to/script2.py')
I recommend using PyQt because that framework is the powerful tool for building GUI on python and for running scripts from python code as system processes you can use subprocess module. I think you know how to use Google to find tutorials =) And also you can check the official page with GUI FAQ here. Thank you!

Different coloured print strings in python - IDLE

I'm using IDLE to just create a short, basic text adventure game. the methods I try work in other editors such as Repl.it or canopy, but I prefer the use of IDLE as I just like the simplicity of it, and that it doesn't require an Internet connection to access.
I won't mention the methods I tried, as I may have just been approaching them in the entirely wrong way, or maybe IDLE just simply doesn't have the functionality of other editors, but I just want to make the text in the game clearer to distinguish. For example, below is a short segment:
import time
print ("Welcome Traveller!")
time.sleep(1)
def name_identify():
print ()
name_input = input ("What is your name? ").lower()
name_input = name_input.title()
time.sleep(0.75)
def name_confirm():
print ()
print ("So %s is your name then?" % name_input)
Where the questions such as
name_input = input ("What is your name? ").lower()
appear in a different colour, or bold/italic even. As things similar to
print ("\x1B[3mHello World\x1B[23m")
and
print ("\033[1;31m""Hello world")
don't work, I assume this function is not naturally supported, so I ask, if it is possible to either A, make it supported, so this method, or similar work, or B, another option which does make changes at least similar to what I seek?
It would be very helpful if anyone could provide me with a way to do this, or at the very least an editor which is close in simplicity (design wise) to IDLE and does support the functions I seek. The version of python I am currently using is 3.6.1.
Thanks in advance, and I apologise if my wording confuses you, just ask about what confused you and I will attempt to clarify. Using Windows with the Idle Shell.
Due to the limitations of some python environments, I just used Tkinter to create an interactive procedurally generated script which you can scroll along as if it were the shell, with the added bonus of adding pictures for more depth.
I know what you are going through right now, but there is no benefit to make text colored or bold in python without GUI. If somehow you manage to do it it will turn back to normal text when you make the file to exe format. I have wasted a week on this.
It only makes sense when you are reach GUI programming. Tkinter would be a good place to start.
Its does not matter on the IDE but on the compiler.
In tkinter its a matter of seconds
lab_practice = label(text = "practice",fg = "red#the foreground color",bg = "yellow",font = ("Arial",40))

Drag drop file onto Python script

I'm looking for a cross-platform way of making my Python script process a file's path by implementing a drag n drop method. At the moment I manually go to the terminal and use the sys.argv method:
python myscript.py /Python/myfile.xls
However this is slow and "techy". Ideally I would a quick and interactive way of allowing a file be processed by my Python script. I primarily need this to work for Mac but cross-platform would be better.
If you want to use Tkinter, have a look at the Tkinter DnD binding from here http://klappnase.bubble.org/TkinterDnD/index.html
When run, the binding shows an example with a listbox that allows you to drag a file on to it.
Do you want to drag and drop the myfile.xls onto your python script within your file navigator ? Say Finder or whatever on Mac, Explorer on Win, Nautilus etc. ? In that case there will not be a simple cross-platform solution, given that you will have to hook into different software on different systems.
For a Mac specific solution try AppleScript - here is a sample
And for something Pythonic there is http://appscript.sourceforge.net/ , http://docs.python.org/library/macosa.html
Otherwise the solution is in the answer above. Use a custom GUI built in Tk, or wx or QT. You can look up their respective documentation for drag and drop, they do have cross-platform ways of doing it.
It'd be easiest to just write a small GUI with Tkinter or something similar and have the user select a file from within the GUI. Something along these lines:
import tkFileDialog
f = tkFileDialog.askopenfilename()
# Go on from there; f is a handle to the file that the user picked
I'm not aware of any cross platform methods to get a script to work with drag and drop, however. This is probably easier, though.

Python widget/cursor detection?

Beginner python learner here. I have a question that I have tried to Google but I just can't come up with the proper way to ask in just a few words (partly because I don't know the right terminology.)
How do I get python to detect other widgets? For example, if I wanted a script to check and see when I click my mouse if that click put focus on an entry widget on a (for example) website. I've been trying to get it to work in Tkinter and I can't figure out even where to begin.
I've seen this:
focus_displayof(self)
Return the widget which has currently the focus on the
display where this widget is located.
But the return value for that function seems to be some ambiguous long number I can't decipher, plus it only works in its own application.
Any direction would be much appreciated. :)
Do you mean inside your own GUI code, or some other application's/website's?
Sounds like you're looking for a GUI driver, or GUI test/automation driver. There are tons of these, some great, some awful, many abandoned. If you tell us more about what you want that will help narrow down the choices.
Is this for testing, or automation, or are you going to drive the mouse and button yourself and just want something to observe what is going on under the hood in the GUI?
>How do I get Python to detect other widgets?
On a machine, or in a browser? If in a machine, which platform: Linux/Windows (which)/Mac?
If in a browser, which browser (and major version)?
> But the return value for that function seems to be some ambiguous long number I can't decipher
Using longs as resource handles is par for the course, although good GUI drivers also work with string/regex matching on window and button names.
> plus it only works in its own application.
What do you mean, and what are you expecting it to return you? You should be able to look up that GUI object and access its title. Look for a GUI driver that works with window and button names.
Here is one list, read it through and see what sounds useful. I have used AutoIt under Win32, it's great, widely-used and actively-maintained; it can be called from Python (via subprocess).
Here are comparisons by the author of PyWinAuto on his and similar tools. Give a read to his criticisms of its structure from 2010. If none of these is what you want, at least you now have the vocabulary to tell us what would be...

Categories

Resources