How to place text boxes in Python Turtle? - python

I'm making a mock OS in python Turtle, and I want to make a username and password prompt. Thing is, I don't want a turtle text window, I want just the type box. and I want to be able to place it. Any suggestions?

Why would you want to use turtle?
Turtle was meant to help you get used to the language before you can build any full-fledged applications. This means it is extremely limited in what it can do.
If you are looking to build a GUI application then I recommend you check out TKinter
I should also point out that you will not be able to build an OS using just python as python is a high-level language. This means it cannot directly interact the hardware.
However, you could write the low-level memory management using C and assembly language and write the rest of your high-level code in python.

Related

How do programming languages draw GUI like Tkinter for example?

I'm a comp sci student studying some compiler design and I have a quick question that bugs me to no end.
I'm currently writing an Interpreter in JavaScript (run on nodeJS) that takes statements like:
x = 4
print x
Which would result in the console output:
4
I can parse these statements pretty easily and have them output stuff to the console window. But how would this process work with GUI applications?
The way I understand it is, let's take Python for example.
When you run python in the command line, it launches a console application which takes in python commands and interprets them. (I know it translates to bytecode first, but it does eventually get interpreted).
So like if it sees 1+1, I understand how it can parse this and return 2 to the console window that it is already running. Python in this case is itself is a console app, so it's intuitive that console output from user-inputted instructions can also be on the console.
I've been able to do THAT. But this ALSO works in python:
from tkinter import*
t = Tk()
How does THAT work? Does the Python Interpreter/VM somehow call a Win32 API function? Or does it draw its own window?
Thank you in advance, for any help given to clarify this.
tkinter is essentially just a Python interface to the Tk library. This is an open source library that runs on all popular operating systems and provides a consistent API for GUI elements.
I don't think Python has any built-in GUI functions, it relies on external libraries like this.
Somebody at some point long ago wrote a library that could directly access the screen and turn pixels on or off. Then, they wrote a function that takes two x,y pairs and computes all the pixels that need to be turned on to draw a line. The library would then call the function to turn pixels on or off.
Someone else then created a library that sits on top of that library, and uses it to not just draw lines, but draw boxes or circles and so on.
Someone else creates a library on top of that which can read font descriptions and turn that into text on the screen. And then someone creates a library that combines the font library and the line library to create a library that creates windows and checkbuttons. And then someone figures out how to add color, and object rotation, and 3d effects, and on and on.
Ultimate we end up with something like tkinter, which is a library that has functions for creating an input widget, which calls a tcl/tk library which converts the python to tcl, and which calls an X11 or DirectX or Win32 or Cocoa or OpenGL library which takes the input and calls some other function that ultimately turns a pixel on or off on the physical display.
When you deal with programming, A LOT of what you are able to do comes down to existing libraries and APIs. If you had to reinvent the wheel every time, you'd never get anything meaningful done.
A simpler example is your print() call. This is mearly a wrapper that writes to stdout. The bash shell / OS you're using handles what happens to stdout. GUIs are more or less the same thing, with just a slightly more complicated path.
tkinter is a python library for generating GUI interfaces. It itself, is nothing more than a wrapper for the more general, Tk library. Tk is a general purpose GUI library that works across platforms. It does this by creating utilizing code that's customized for each operating system's GUI library. It's the OS* itself that ends up creating the GUI.
*This is kind of a generalization as in some operating systems (such as those that utilize something such as Gnome) the GUI interface is more decoupled from the OS than one would often think.

Giving text based RPG in Python a GUI

I currently have a text based RPG that uses a large amount of print statements. These are only visible in my IDE, but I want others to be able to play my game in a more presentable way without needing an IDE. Is there a way to easily display my print statements in some sort of GUI? I was thinking of using PyGame, but I wanted to know if there were other options available, such as using HTML/CSS/JS or some kind of toolkit. Are there any tools to make this easier?
If you need very simple toolkit you could look at the Python bundled ones:
Text based
curses - https://docs.python.org/3/howto/curses.html
Graphical based
Tk - https://docs.python.org/3/library/tk.html
There are many external toolkits available, such as
PyQt - https://wiki.python.org/moin/PyQt
wxPython - https://www.wxpython.org/
PyGTK - https://python-gtk-3-tutorial.readthedocs.io/en/latest/
etc.
But they are really advanced ones.
Me personally I would stick with the bundled ones for now.
If your using console inputs, you can use Pyinstaller with "--onefile" to build it into an .exe with its own little console. without needing python or using command prompt on the host computer, just be aware that if you want text to remain displayed when your program is due to terminate, you will need to use "input(" ") as the last line of your program so the console stays open

Python compile a script within a GUI

I am currently working on the final year project for my degree. I have chosen to research and develop a tool to aid the delivery of the new Computing curriculum that is coming to schools next year.
I am using a Raspberry Pi in my development, and I aim to teach extremely basic Python programming to children between the ages of 8 and 10. They are going to be able to control some hardware attached to the Pi using a simple API that I am going to create.
My question is: I would like to be able to create a GUI for the children to work in, which would allow them to write and compile scripts. This is mainly to get away from the unfamiliar interface of Linux and terminals etc, and put them in a friendly, basic interface which will pretty much just allow them to write their code and click a big red button to compile and run it to interact with the hardware. Is it possible to allow for text to be written in a GUI and then compiled when the button is pressed?
I am pretty new to Python myself so I am not as clued up as I'd like to be about the specifics of it. I know that it is possible to have the output of IDLE inside of a tkinter interface, and that it is possible to have text boxes for user input and stuff, but would it actually be possible to compile a script on button press and then run it? I have been thinking that maybe threading is the answer. Perhaps I could create a new thread to do it when the button is pressed?
My apologies if this is incredibly basic, but I am having no luck finding any answers about how I would do this. I think it's mainly because I am unsure on what exactly to ask for.
I appreciate any feedback/help, thank you very much.
Dell
Have your GUi write the Python code to a file, then dynamically import using the imp module. I actually do something similar :-)
import imp
hest = imp.load_source("Name", Path)

Python Image processing screenshots

I'm trying to write a program than will detect when my mouse pointer will change icon and automatically send out a mouse click. Is there a better way to do this than to take screenshots and parse the image for the mouse icon?
EDIT:
I'm running my program on windows 7.
I'm trying to learn some image processing and make a simple flash game i made automated.
Rules: when the curses changes shape, click to get a point.
Also what imaging modules for python will allow you to take a specific size screenshot not just the whole screen? This question has moved to a new thread: "Taking Screen shots of specific size"
The way to do this in Windows is to install either a global message hook with SetWindowsHookEx or SetWinEventHook. (Alternatively, you could build a DLL that embeds Python and hooks into the browser or its Flash wrapper app and do it less intrusively from within the app, but that's much more work.)
The message you want is WM_SETCURSOR. Note that this is the message sent by Windows to the app to ask whether it wants to change the cursor, not a message sent when the cursor changes. So, IIRC, you will want to put a WH_CALLWNDPROC and a WH_CALLWNDPROCRET and check GetCursorInfo before and after to see if the app has done so.
So, how do you do this from Python? Honestly, if you don't already know both win32api and friends from the pywin32 package, and how to write Windows message procs in some language, you probably don't want to. If you do want to, I'd start off with the (abandoned) pyHook project from UNC Assist. Even if you can't get it working, it's full of useful source code.
You should also search SO for [python] SetWinEventHook and [python] SetWindowsHookEx, and google around a bit; there are some examples out there (I even wrote one here somewhere…)
You can look at higher-level wrapper frameworks like pywinauto and winGuiAuto, but as far as I know, none of them has much help for capturing events.
I believe there are other tools, maybe AutoIt, that have all the functionality you need, but not in Python module. (AutoIt, for example, has its own VB-like scripting language instead.)

Using Perl, Python, or Ruby, how to write a program to "click" on the screen at scheduled time?

Using Perl, Python, or Ruby, can I write a program, probably calling Win32 API, to "click" on the screen at scheduled time, like every 1 hour?
Details:
This is for experimentation -- and can the clicking be effective on Flash content as well as any element on screen? It can be nice if the program can record where on screen the click needs to happen, or at least draw a red dot on the screen to show where it is clicking on.
Can the click be targeted towards a window or is it only a general pixel on the screen? What if some virus scanning program pops up covering up the place where the click should happen? (although if the program clicks on the white space of a window first, then it can bring that window to the foreground first).
By the way, can Grease Monkey or any Firefox add-on be used to do this too?
If you are trying to automate some task in a website you might want to look at WWW::Selenium. It, along with Selenium Remote Control, allows you to remote control a web browser.
In Python there is ctypes and in Perl there is Win32::API
ctypes Example
from ctypes import *
windll.user32.MessageBoxA(None, "Hey MessageBox", "ctypes", 0);
Win32::Api Example
use Win32::GUI qw( WM_CLOSE );
my $tray = Win32::GUI::FindWindow("WindowISearchFor","WindowISearchFor");
Win32::GUI::SendMessage($tray,WM_CLOSE,0,0);
To answer the actual question, in Perl, you would use the SendMouse (and the associated functions) provided by the Win32::GuiTest module.
#!/usr/bin/perl
use strict;
use warnings;
use Win32::GuiTest qw( MouseMoveAbsPix SendMouse );
MouseMoveAbsPix(640,400);
SendMouse "{LEFTCLICK}";
__END__
UPDATE:
What if some virus scanning program pops up covering up the place
where the click should happen?
In that case, you would use FindWindowLike to find the window and MouseClick to send a click to that specific window.
If using a different tool is allowed, you should take a look at AutoHotkey or AutoIt. These tools were made for this sort of thing, and I've always been keen on using the right tools for the right jobs.
AutoHotkey is based on AutoIt I believe, and it is my personal preference. You only really need 2 functions for what you're trying to achieve, MouseMove and MouseClick.
I find this is easier to approach in Java or C++. Java has a Robot class that allows you to just pass x, y coordinates and click somewhere. Using C++, you can achieve that same functionality using mouse_event() or SendMessage() with the WM_MOUSE_DOWN flag. SendMessage is more technical but it allows you to use FindWindow() and send mouse clicks to a specific window, even if it's minimized.
Using a scripting language like Python or Ruby, I'd guess that you'd end up hooking into one of these Windows API functions anyway.

Categories