Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » Python GUI programming tutorial?
- Magnie
-
Scratcher
100+ posts
Python GUI programming tutorial?
@Magnie: Tk is fine. Laying out things is a little fiddly, but it takes time to learn how any GUI toolkit works. I had a look at all of the options recently, including simply using Webkit to frame an HTML page that communicates with a Python backend. I concluded that Tk is about as good as the others, can have a native look-and-feel on all three platforms, and is the easiest and smallest to use (since there are no additional dependencies). It's a reasonable choice.Fair point.

- hedgehog2000
-
Scratcher
41 posts
Python GUI programming tutorial?
I use Tkinter. If you don't like Tkinter, use PyQt4 (or 5). PyQt4 is what i used before i used Tkinter, and it doesn't always work out that well. However if you want it, you can download it at this site. But I do highly recommend Tkinter over all other python GUI modules/toolkits.
- hedgehog2000
-
Scratcher
41 posts
Python GUI programming tutorial?
I don't think that's supported anymore… At least in python 2The only way to do that is with Tkinter. Why don't you want 3rd party libraries?
You could also use WxPython.
- hedgehog2000
-
Scratcher
41 posts
Python GUI programming tutorial?
No. Many modules for making GUIs definitely are included in python. Tkinter should also just be installed on python, so no downloading needed!Not always, as some modules/libraries use C as a backend.So the only way to make a GUI program with Python is with some C too?
pen up with <key [Python Tkinter library!] pressed?>
- hedgehog2000
-
Scratcher
41 posts
Python GUI programming tutorial?
The only way to do that is with Tkinter. Why don't you want 3rd party libraries?Yes. Tkinters amazing. Otherwise PyQt.
- navysmeagol_175
-
Scratcher
100+ posts
Python GUI programming tutorial?
if you have python 3 and used the standard installationBuilt in? So it's by Python, no third party? And I have it downloaded?The same is with Python.Uh-uh! There's a built-in library called Tkinter that allows you to do Tk GUI programming.
- Reyaansh_Spide
-
Scratcher
4 posts
Python GUI programming tutorial?
I found the steps on the scratch wiki
- Reyaansh_Spide
-
Scratcher
4 posts
Python GUI programming tutorial?
This is what the wiki says
Communicating to Scratch via Python with a GUI
SandCastleIcon.png This article has links to websites or programs not trusted by Scratch or hosted by Wikipedia. Remember to stay safe while using the internet, as we cannot guarantee the safety of other websites.
This is a tutorial on how to communicate with Scratch using Remote Sensor Connections through a Python program. It includes a minimal GUI window that prompts the user for a message to broadcast to Scratch. If the version in use is Python 3.x, see this article.
There are also a few Python libraries that can be used to interact with Scratch, which may be simpler to use.
Install Python
Mac OS X comes with Python built in. It can be run from the command line using the terminal program via the command python or python <file>, with the file's path.
The latest version of Python is available here.
Enabling Remote Sensor Connections
In a Scratch project, right click on one of the sensor tiles and click “enable remote sensor connections”. This is necessary for the connection to be made.
Sample Client Program
Below is a sample client program to send messages and update variable:
The first step is to create a new plaintext file. This can be done in IDLE, if it is available, or in the operating system's plaintext editor. Name the file SendBroadcast.py (not necessarily, but it's good practice to name Python files with a .py at the end). If using TextEdit, press ⌘ Cmd+⇧ Shift+T to convert the document to a plaintext document; a sign of this being successful is the formatting bar disappearing.
Start by importing all of the necessary python modules and initiating Tkinker:
Note Note: This tutorial only works with Python 2.x. Later versions do not have the modules.
from array import array
import socket
import time
import sys
from Tkinter import Tk
from tkSimpleDialog import askstring
root = Tk()
root.withdraw()
Next, add some text input to choose what IP to connect to:
PORT = 42001
HOST = askstring('Scratch Connector', ‘IP:’)
if not HOST:
sys.exit(1)
print “Connecting…”
scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scratchSock.connect((HOST, PORT))
print “Connected!”
Then, create the function that sends the broadcast to Scratch:
def sendScratchCommand(cmd):
n = len(cmd)
a = array('c')
a.append(chr((n >> 24) & 0xFF))
a.append(chr((n >> 16) & 0xFF))
a.append(chr((n >> 8) & 0xFF))
a.append(chr(n & 0xFF))
scratchSock.send(a.tostring() + cmd)
Lastly, create a user input to show what broadcast would be sent to Scratch:
while True:
msg = askstring('Scratch Connector', ‘Send Broadcast:’)
if msg:
sendScratchCommand('broadcast “' + msg + ‘”’)
The entire program:
from array import array
import socket
import time
import sys
from Tkinter import Tk
from tkSimpleDialog import askstring
root = Tk()
root.withdraw()
PORT = 42001
HOST = askstring('Scratch Connector', ‘IP:’)
if not HOST:
sys.exit(1)
print “Connecting…”
scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scratchSock.connect((HOST, PORT))
print “Connected!”
def sendScratchCommand(cmd):
n = len(cmd)
a = array('c')
a.append(chr((n >> 24) & 0xFF))
a.append(chr((n >> 16) & 0xFF))
a.append(chr((n >> 8) & 0xFF))
a.append(chr(n & 0xFF))
scratchSock.send(a.tostring() + cmd)
while True:
msg = askstring('Scratch Connector', ‘Send Broadcast:’)
if msg:
sendScratchCommand('broadcast “{}”'.format(msg))
Python should log that a successful connection has been made, it will then print the messages it is sending
In Scratch, create a script that starts with
when
I
receive
beat
. Running the Python program and typing in “beat” should cause Scratch to react.
The value of the note variable being changed by Python appears in the “sensor value” block's menu
Sending a broadcast from Scratch will be logged in the Python Console.
Creating or updating a global variable in Scratch will be logged in the Python Console
Communicating to Scratch via Python with a GUI
SandCastleIcon.png This article has links to websites or programs not trusted by Scratch or hosted by Wikipedia. Remember to stay safe while using the internet, as we cannot guarantee the safety of other websites.
This is a tutorial on how to communicate with Scratch using Remote Sensor Connections through a Python program. It includes a minimal GUI window that prompts the user for a message to broadcast to Scratch. If the version in use is Python 3.x, see this article.
There are also a few Python libraries that can be used to interact with Scratch, which may be simpler to use.
Install Python
Mac OS X comes with Python built in. It can be run from the command line using the terminal program via the command python or python <file>, with the file's path.
The latest version of Python is available here.
Enabling Remote Sensor Connections
In a Scratch project, right click on one of the sensor tiles and click “enable remote sensor connections”. This is necessary for the connection to be made.
Sample Client Program
Below is a sample client program to send messages and update variable:
The first step is to create a new plaintext file. This can be done in IDLE, if it is available, or in the operating system's plaintext editor. Name the file SendBroadcast.py (not necessarily, but it's good practice to name Python files with a .py at the end). If using TextEdit, press ⌘ Cmd+⇧ Shift+T to convert the document to a plaintext document; a sign of this being successful is the formatting bar disappearing.
Start by importing all of the necessary python modules and initiating Tkinker:
Note Note: This tutorial only works with Python 2.x. Later versions do not have the modules.
from array import array
import socket
import time
import sys
from Tkinter import Tk
from tkSimpleDialog import askstring
root = Tk()
root.withdraw()
Next, add some text input to choose what IP to connect to:
PORT = 42001
HOST = askstring('Scratch Connector', ‘IP:’)
if not HOST:
sys.exit(1)
print “Connecting…”
scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scratchSock.connect((HOST, PORT))
print “Connected!”
Then, create the function that sends the broadcast to Scratch:
def sendScratchCommand(cmd):
n = len(cmd)
a = array('c')
a.append(chr((n >> 24) & 0xFF))
a.append(chr((n >> 16) & 0xFF))
a.append(chr((n >> 8) & 0xFF))
a.append(chr(n & 0xFF))
scratchSock.send(a.tostring() + cmd)
Lastly, create a user input to show what broadcast would be sent to Scratch:
while True:
msg = askstring('Scratch Connector', ‘Send Broadcast:’)
if msg:
sendScratchCommand('broadcast “' + msg + ‘”’)
The entire program:
from array import array
import socket
import time
import sys
from Tkinter import Tk
from tkSimpleDialog import askstring
root = Tk()
root.withdraw()
PORT = 42001
HOST = askstring('Scratch Connector', ‘IP:’)
if not HOST:
sys.exit(1)
print “Connecting…”
scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scratchSock.connect((HOST, PORT))
print “Connected!”
def sendScratchCommand(cmd):
n = len(cmd)
a = array('c')
a.append(chr((n >> 24) & 0xFF))
a.append(chr((n >> 16) & 0xFF))
a.append(chr((n >> 8) & 0xFF))
a.append(chr(n & 0xFF))
scratchSock.send(a.tostring() + cmd)
while True:
msg = askstring('Scratch Connector', ‘Send Broadcast:’)
if msg:
sendScratchCommand('broadcast “{}”'.format(msg))
Python should log that a successful connection has been made, it will then print the messages it is sending
In Scratch, create a script that starts with
when
I
receive
beat
. Running the Python program and typing in “beat” should cause Scratch to react.
The value of the note variable being changed by Python appears in the “sensor value” block's menu
Sending a broadcast from Scratch will be logged in the Python Console.
Creating or updating a global variable in Scratch will be logged in the Python Console
- Discussion Forums
- » Advanced Topics
-
» Python GUI programming tutorial?