Psychopy script freezes with window and dialogue guis - python

I want an experiment that displays a bunch of random dots, then asks the user to input the correct number of dots they saw. I want the experiment to loop. I can get this to work for 1 iteration, but something is wrong with the looping because the window and the dialogue are colliding, or the window isn't properly closing. When running this script Psychopy currently, the gui freezes. I've tried both python3 and python2 with my code.
import random
import psychopy.visual
import psychopy.event
import psychopy.core
from psychopy import gui
import time
while True:
win = psychopy.visual.Window(
size=[500, 500],
units="pix",
fullscr=False
)
myDlg = gui.Dlg(title="Response")
n_dots = random.randint(5, 200)
dot_xys = []
for dot in range(n_dots):
dot_x = random.uniform(-250, 250)
dot_y = random.uniform(-250, 250)
dot_xys.append([dot_x, dot_y])
dot_stim = psychopy.visual.ElementArrayStim(
win=win,
units="pix",
nElements=n_dots,
elementTex=None,
elementMask="circle",
xys=dot_xys,
sizes=10,
contrs=random.random(),
)
dot_stim.draw()
win.flip()
psychopy.event.clearEvents()
time.sleep(4)
win.close()
myDlg.addField('How many dots did you see?')
number = myDlg.show()
if myDlg.OK:
print(number)
myDlg.close()
psychopy.core.quit()
I'm using the latest version of Psychopy. Please let me know if you have any suggestions. Thanks!

Generally, you would not use a dialogue box to collect responses. Rather, you'd make something that works inside the window using psychopy stimuli. Here's a solution:
# Tidy 1: just import from psychopy
import random
from psychopy import visual, event, core
# Tidy 2: create a window once. Don't close it.
win = visual.Window(
size=[500, 500],
units="pix",
fullscr=False
)
instruction_text = visual.TextStim(win, text = u'How many dots did you see?', pos=(0, 100))
answer_text = visual.TextStim(win)
# Solution: a function to collect written responses
def get_typed_answer():
answer_text.text = ''
while True:
key = event.waitKeys()[0]
# Add a new number
if key in '1234567890':
answer_text.text += key
# Delete last character, if there are any chars at all
elif key == 'backspace' and len(answer_text.text) > 0:
answer_text.text = answer_text.text[:-1]
# Stop collecting response and return it
elif key == 'return':
return(answer_text.text)
# Show current answer state
instruction_text.draw()
answer_text.draw()
win.flip()
while True:
# Prepare dot specifications
n_dots = random.randint(5, 200)
dot_xys = []
for dot in range(n_dots):
dot_x = random.uniform(-250, 250)
dot_y = random.uniform(-250, 250)
dot_xys.append([dot_x, dot_y])
# This is extremely ugly! You should generally never create a new stimulus,
# but rather update an existing one. However, ElementArrayStim currently
# does not support changing the number of elements on the go.
dot_stim = visual.ElementArrayStim(
win=win,
units="pix",
elementTex=None,
elementMask="circle",
sizes=10,
contrs=random.random(),
nElements = n_dots,
xys = dot_xys,
)
# Show it
dot_stim.draw()
win.flip()
core.wait(4)
# Collect response
print(get_typed_answer())

Related

Tkinter GUI to be able to change variables of an separate continuous process

I have designed an marine engine simulator - it's a program that sends some serial DATA using a certain protocol called "BlueVision". The data is encoded based on a header, a block number, a block type, actual data (indexed), a checksum and a footer. Due to the fact that if you want to change a value in the actual data - then the checksum changes - I designed an recalculation of the block with the correct checksum when the data is changed. I made a GUI that allows me to change the value of 2 data points in order to be able to test live. The problem is that I don't know how to use threads or subprocesses correctly and the window keeps on freezing. The application works - but works badly and I kept on searching for a similar issue - and did find some suggestions but I haven't been able to implement it. Can someone help? PS: the code is awkward at best - please don't judge - I know I could of made it way better - but this is the best I could do with the time I had. The issue might not be clear to you if you don't have a COM port where the program writes to.
# Blue Vision is a MTU serial communication protocol - based on HEX values grouped in BLOCKS
import tkinter as tk
from tkinter import ttk
import time
import serial
def split_by_n(seq, n):
while seq:
yield seq[:n]
seq= seq[n:]
def after(self, ms, func=None, *args):
"""Call function once after given time.
MS specifies the time in milliseconds. FUNC gives the
function which shall be called. Additional parameters
are given as parameters to the function call. Return
identifier to cancel scheduling with after_cancel."""
win = tk.Tk()
win.title("GUI - TEST VERSION")
win.geometry("750x250")
label = tk.Label(win, text = "Main Engine Overspeed")
label.pack()
v = tk.StringVar()
v.set('00')
c = tk.StringVar()
c.set("00")
def setText(word):
v.set(word)
a = ttk.Button(win, text ="01", command =lambda:setText("01"))
a.pack()
b = ttk.Button(win, text="00", command = lambda:setText("00"))
b.pack()
label1 = tk.Label(win, text ="Main Engine Speed")
label1.pack()
name_entry = tk.Entry(win, textvariable=c)
name_entry.pack()
def task():
MyVar = v.get()
priEngSp =c.get()
if len(priEngSp) == 0:
priEngSp = '00'
block_3 = 'FAF500030002000000290100000001000100000001000001000000000000000000000000000222AF5F'
block_4 = 'FAF500040003000001A000004650000047E00000000000000000000000000007EF4000083D6000000000000000000000000000000000000000000000012C000006D600000000000000000000278D00000000000000007FFFFFFF000000000001991500000000000000000016E36000000000000923D8000971F8000001F40000059F000026AC00002774000005800000251C00000580000027740000283C0000056200001D4C00001F400000061800000000000060FB00004650000036B000007D0000008CA0000006180000251C0000000000000000000000000000284800192D500017A6B00000051B0000251CFFFFFFA8000002580000044C000000FA0000000000000000000006770000CB200000D6D8000006770000CB200000D6D80000060600005DC000000000000027100000000000000000000000000000000000000000000003C2000061A8000000000000000000000000000000000000000000000000000000000000000000000000000000000000363300000EA6000249F0FFFFFB1E000F42400000000000000000000000000000000000000000000032D9AF5F'
block_5 = 'FAF5000500020000005600000000000000000000000000000000000000000000007F7F0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034AAF5F'
block_6 = 'FAF5000600020000003D00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000234AF5F'
block_7 = 'FAF5000700030000008C000006280000064A0000064A0000068D0000066B0000068D0000068D000006AE0000000000000000000006AE000006070000060700000607000005E5000005A2000006070000064A00000000000000000000062A000006AE000005A20000350EFFFFF07C00003CDEFFFFE8AC00000000000000000000000000000000000012DEAF5F'
block_8 = 'FAF50008000300000070000000000000112900000000000000000000059C000027740000283C000047E000000000000000000000000000000000000000000000000000000000000000007FFFFFFF7FFFFFFF0000055100002CEC0000000000000000000000000000000000000DD1AF5F'
block_9 = 'FAF50009000200000020000000000000000000000000000000000000021AAF5F'
block_10 = 'FAF5000A0002000000260000000000000000000000000000000000000000000000000221AF5F'
block_11 = 'FAF5000B0003000000EC00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002E9AF5F'
block_12 = 'FAF5000C000200000045000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000242AF5F'
block_2 = 'FAF50002000200000074010001000000000000000000000000000000000000000000007F0000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002E9AF5F'
checksum = hex(0x00)
block_2split = list(split_by_n(block_2,2))
block_2split[10] = MyVar.upper() #Overspeed Alarm Position
for i in range(len(block_2split)-6):
checksum = hex(int(checksum, 16) + int(block_2split[i], 16))
checksum_string = str(checksum)
checksum_actvalue = checksum_string[2:]
checksum_long = checksum_actvalue.rjust(8,'0').upper()
checksum_split = list(split_by_n(checksum_long,2))
block_2split[len(block_2split)-3] = checksum_split[3]
block_2split[len(block_2split)-4] = checksum_split[2]
block_2split[len(block_2split)-5] = checksum_split[1]
block_2split[len(block_2split)-6] = checksum_split[0]
Block_2Output = ''.join(str(item) for item in block_2split)
iEngineSpeed = int(priEngSp,10)
hEngineSpeed = hex(iEngineSpeed * 10)
sEngineSpeed = str(hEngineSpeed)[2:].upper()
while (len(sEngineSpeed)<8):
sEngineSpeed = '0' + sEngineSpeed
block_4split = list(split_by_n(block_4,4))
sEngineSpeed_split = list(split_by_n(sEngineSpeed,4))
block_4split[5] = sEngineSpeed_split[0]
block_4split[6] = sEngineSpeed_split[1]
Block_4joint = ''.join(str(item) for item in block_4split)
Block_4joint_sp = list(split_by_n(Block_4joint,2))
checksumb4 = hex(0x00)
for i in range(len(Block_4joint_sp)-6):
checksumb4 = hex(int(checksumb4, 16) + int(Block_4joint_sp[i], 16))
checksumb4_string = str(checksumb4)
checksumb4_actvalue = checksumb4_string[2:]
checksumb4_long = checksumb4_actvalue.rjust(8,'0').upper()
checksumb4_split = list(split_by_n(checksumb4_long,2))
Block_4joint_sp[len(Block_4joint_sp)-3] = checksumb4_split[3]
Block_4joint_sp[len(Block_4joint_sp)-4] = checksumb4_split[2]
Block_4joint_sp[len(Block_4joint_sp)-5] = checksumb4_split[1]
Block_4joint_sp[len(Block_4joint_sp)-6] = checksumb4_split[0]
Block_4Output = ''.join(str(item) for item in Block_4joint_sp)
blocks = [Block_2Output, block_3, Block_4Output, block_5, block_6, block_7, block_8, block_9, block_10, block_11, block_12]
with serial.Serial('COM5', '9600') as ser: #you might wanna comment this part out
for block in blocks:
print(block)
ser.write(bytes.fromhex(block.strip())) #you might wanna comment this part out
time.sleep(1)
win.after(200, task)
win.after(200, task)
win.mainloop()
To avoid freezing, one of the way is to use thread:
from threading import Thread
...
def task():
while True:
MyVar = v.get()
...
blocks = [Block_2Output, block_3, Block_4Output, block_5, block_6, block_7, block_8, block_9, block_10, block_11, block_12]
with serial.Serial('COM5', '9600') as ser: #you might wanna comment this part out
for block in blocks:
print(block)
ser.write(bytes.fromhex(block.strip())) #you might wanna comment this part out
time.sleep(1)
time.sleep(0.2)
# run task() in a thread
Thread(target=task, daemon=1).start()
win.mainloop()

Can't seem to get uasyncio working in a micropython script for a PyBoard

I am designing a new time/score keeper for an air hockey table using a PyBoard as a base. My plan is to use a TM1627 (4x7seg) for time display, rotary encoder w/ button to set the time, IR and a couple 7segs for scoring, IR reflector sensors for goallines, and a relay to control the fan.
I'm getting hung up trying to separate the clock into its own thread while focusing on reading the sensors. Figured I could use uasyncio to split everything up nicely, but I can't figure out where to put the directives to spin off a thread for the clock and eventually the sensors.
On execution right now, it appears the rotary encoder is assigned the default value, no timer is started, the encoder doesn't set the time, and the program returns control to REPL rather quickly.
Prior to trying to async everything, I had the rotary encoder and timer working well. Now, not so much.
from rotary_irq_pyb import RotaryIRQ
from machine import Pin
import tm1637
import utime
import uasyncio
async def countdown(cntr):
# just init min/sec to any int > 0
min = sec = 99
enableColon = True
while True:
# update the 4x7seg with the time remaining
min = abs(int((cntr - utime.time()) / 60))
sec = (cntr - utime.time()) % 60
#print(str(), str(sec), sep=':' )
enableColon = not enableColon # alternately blink the colon
tm.numbers(min, sec, colon = enableColon)
if(min + sec == 0): # once both reach zero, break
break
await uasyncio.sleep(500)
X1 = pyb.Pin.board.X1
X2 = pyb.Pin.board.X2
Y1 = pyb.Pin.board.Y1
Y2 = pyb.Pin.board.Y2
button = pyb.Pin(pyb.Pin.board.X3, pyb.Pin.IN)
r = RotaryIRQ(pin_num_clk=X1,
pin_num_dt=X2,
min_val=3,
max_val=10,
reverse=False,
range_mode=RotaryIRQ.RANGE_BOUNDED)
tm = tm1637.TM1637(clk = Y1, dio = Y2)
val_old = val_new = 0
while True:
val_new = r.value()
if(val_old != val_new):
val_old = val_new
print(str(val_new))
if(button.value()): # save value as minutes
loop = uasyncio.get_event_loop()
endTime = utime.time() + (60 * val_new)
loop.create_task(countdown(endTime))
r.close() # Turn off Rotary Encoder
break
#loop = uasyncio.get_event_loop()
#loop.create_task(countdown(et))
#loop.run_until_complete(countdown(et))
I'm sure it's something simple, but this is the first non-CLI python script I've done, so I'm sure there are a bunch of silly mistakes. Any assistance would be appreciated.

How to determine an active screen (monitor) of my Application (window) using python PyQt5?

I am working on an application which is using many widgets (QGroupBox, QVBoxLayout, QHBoxLayout). Initially it was developed on normal HD monitors. But, recently many of us upgraded to 4K resolution monitors. Now some of the buttons and sliders are compressed so small that they are unusable.
Now I tried to make some changes so that the application can be used with both HD and 4K monitors.
I started reading the link below:
https://leomoon.com/journal/python/high-dpi-scaling-in-pyqt5/enter link description here
I thought whenever my window is opened in a particular monitor I can call the following code:
if pixel_x > 1920 and pixel_y > 1080:
Qapp.setAttribute(Qt.AA_EnableHighDpiScaling, True)
Qapp.setAttribute(Qt.AA_UseHighDpiPixmaps, True)
else:
Qapp.setAttribute(Qt.AA_EnableHighDpiScaling, False)
Qapp.setAttribute(Qt.AA_UseHighDpiPixmaps, False)
Then I tried to get the monitor resolution (pixel_x and pixel_y) using below code from using related post here.
import sys, ctypes
user32 = ctypes.windll.user32
user32.SetProcessDPIAware()
screen_width = 0 #78
screen_height = 1 #79
[pixel_x , pixel_y ] = [user32.GetSystemMetrics(screen_width), user32.GetSystemMetrics(screen_height)]
screen_width = 0, screen_height = 1 gives me the resolution of my primary monitor(mostly laptops in our case which are HD). screen_width = 78, screen_height = 79 gives me the combined resolution of virtual machines. But I do not understand how I can dynamically get these values depending upon where my application opened.
My application window is developed in such a way that it will open in the same monitor where it was closed last time. The problem is now I want to get the active monitor resolution whenever my GUI is called and adapt to that resolution. I would be glad if someone can help me out.
I am interested to know if I can call the screen resolution calculation every time that I drag my window from an HD monitor to a 4K monitor and Vice versa.
Edit: I have found something similar in this post here But I could not get much from this.
Edit2: Based on #Joe solution, Primary Screen Detection, Why is my primary screen always my laptop resolution even though I run the application on a 4K screen?
I just tried to get the dpi of all the screens using the code below:
def screen_selection():
app = QApplication(sys.argv)
valid_screens = []
for index, screen_no in enumerate(app.screens()):
screen = app.screens()[index]
dpi = screen.physicalDotsPerInch()
valid_screens.append(dpi)
return valid_screens
One solution I came across is to use a temporary QApplication():
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
# fire up a temporary QApplication
def get_resolution():
app = QtWidgets.QApplication(sys.argv)
print(app.primaryScreen())
d = app.desktop()
print(d.screenGeometry())
print(d.availableGeometry())
print(d.screenCount())
g = d.screenGeometry()
return (g.width(), g.height())
x, y = get_resolution()
if x > 1920 and y > 1080:
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
else:
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, False)
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, False)
# Now your code ...
This function will detect all attached screens:
# fire up a temporary QApplication
def get_resolution_multiple_screens():
app = QtGui.QGuiApplication(sys.argv)
#QtWidgets.QtGui
all_screens = app.screens()
for s in all_screens:
print()
print(s.name())
print(s.availableGeometry())
print(s.availableGeometry().width())
print(s.availableGeometry().height())
print(s.size())
print(s.size().width())
print(s.size().height())
print()
print('primary:', app.primaryScreen())
print('primary:', app.primaryScreen().availableGeometry().width())
print('primary:', app.primaryScreen().availableGeometry().height())
# now choose one
You can use the hints here and here to get the screen where the application is running.
But I think primaryScreen should also return this:
primaryScreen : QScreen* const
This property holds the primary (or default) screen of the
application.
This will be the screen where QWindows are initially shown, unless
otherwise specified.
(https://doc.qt.io/qt-5/qguiapplication.html#primaryScreen-prop)
Well, after creating the MainWindow, you can just call QMainWindow.screen(). This returns the current screen the MainWindow is on. This would at least allow you to check the screen resolution at the start of your application.
Right now there is no such thing as a screenChangeEvent. However i am sure you can create one by subclassing the MainWindow and overloading the QMainWindow.moveEvent
For example:
class MainWindow(QtWidgets.QMainWindow):
screenChanged = QtCore.pyqtSignal(QtGui.QScreen, QtGui.QScreen)
def moveEvent(self, event):
oldScreen = QtWidgets.QApplication.screenAt(event.oldPos())
newScreen = QtWidgets.QApplication.screenAt(event.pos())
if not oldScreen == newScreen:
self.screenChanged.emit(oldScreen, newScreen)
return super().moveEvent(event)
This checks if the screen has changed. If it has it emits a signal. Now you only need to connect this signal to a function that sets your dpi attributes. The event gives you access to the old and to the new screen.
Warning:
One of the screen can be None at the start of your application because there is no oldScreen when you first start your application. So please check this.
Though i could not get the direct solution I am able to develop a method to get what I was looking. With the help of few links and previous post I am able to achieve. with this post I got an idea of tracking the mouse event.
I developed a method to track all the monitors and respective staring positions. if my variable naming is not appropriate I am happy to accept the changes
def get_screen_resolution():
app = QApplication(sys.argv)
screen_count = QGuiApplication.screens()
resolutions_in_x = []
for index, screen_names in enumerate(screen_count):
resolution = screen_count[index].size()
height = resolution.height()
width = resolution.width()
resolutions_in_x.append(width)
low_resolution_monitors = {}
high_resolution_monitors = {}
for i, wid_res in enumerate(resolutions_in_x):
if wid_res > 1920:
high_resolution_monitors.update({i: wid_res})
else:
low_resolution_monitors.update({'L': wid_res})
temp_value = 0
high_res_monitors_x_position = []
low_res_monitors_x_position = []
for i in range(len(screen_count)):
temp_value = temp_value+resolutions_in_x[i]
if resolutions_in_x[i] in high_resolution_monitors.values():
high_res_monitors_x_position.append(temp_value-resolutions_in_x[i])
else:
low_res_monitors_x_position.append(temp_value-resolutions_in_x[i])
total_width_res = []
pixel_value = 0
first_pixel = 0
for i, wid_value in enumerate(resolutions_in_x):
pixel_value = pixel_value + wid_value
total_width_res.append(tuple((first_pixel, pixel_value-1)))
first_pixel = pixel_value
return high_res_monitors_x_position, low_res_monitors_x_position, total_width_res
def moveEvent(self, event):
screen_pos = self.pos()
screen_dimensions = [screen_pos.x(),screen_pos.y()]
super(MainWindow, self).moveEvent(event)
Window_starting_pt = screen_pos.x()
for i, value in enumerate(self.total_width_res):
if value[0]<=Window_starting_pt+30 <=value[1] or value[0]<=Window_starting_pt-30 <=value[1]: #taking 30pixels as tolerance since widgets are staring at some negative pixel values
if value[0] in self.high_res_monitors_x_position:
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
else:
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, False)
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, False)
With aboe two functions am able to track my application(window) position and also able to track when ever it is dragged among windows

'list' object has no attribute 'winType'

I'm trying to run a python script but I get this error. AttributeError: 'list' object has no attribute 'winType':
The actual contents of the file are:
import time
import pylsl
import bci.open_bci_v3 as bci
#from random import random as rand
from pylsl import StreamInfo, StreamOutlet
from psychopy import prefs
prefs.general['audioLib'] = ['pygame']
from psychopy import visual, core, sound
import esys_cfg
NUM_CHANNELS = 8
SAMP_RATE = 100
info = StreamInfo('OpenBCI', 'EEG', NUM_CHANNELS, SAMP_RATE, 'float32', 'myuid34234')
outlet = StreamOutlet(info)
#funtion call to start displaying images
#def displayStimuli
# for file in os.listdir('directory'):
# for i in range(0,len(images)):
# def display(files, .....):
# ex: file_name = ['/dir/dir2/img.png']
window = visual.Window([512, 512])
cfg = esys_cfg.create_config('../stimulus-config/test.yml')
print(cfg.trial_order)
#trial_order = ['one', 'two', 'one']
for element in cfg.trial_order: #loop through all elements in array trial_order
imageIndex = 0
for imageIndex in range(len(cfg.trials[element].files)):
stimulis = cfg.trials[element].stimuli_folder + '/' + cfg.trials[element].files[imageIndex]
showStim = visual.ImageStim(window, stimulis)
showStim.draw([window])
window.flip()
core.wait(2.0)
How can I correct this and run the program?
The error arises in the line
showStim.draw([window])
which should read
showStim.draw(window)
and if you only have one window, simply do
showStim.draw()
which draws in the window that showStim was given as argument at initialization. BTW, initializing a psychopy stimulus is computationally heavy (easily takes a few hundred milliseconds), so do it once at the beginning of the script and then update the relevant aspect during runtime. In your case, do this:
showStim = visual.ImageStim(window) # initialize the stimulus
for element in cfg.trial_order: #loop through all elements in array trial_order
imageIndex = 0
for imageIndex in range(len(cfg.trials[element].files)):
stimulis = cfg.trials[element].stimuli_folder + '/' + cfg.trials[element].files[imageIndex]
showStim.image = stimulus # update the image
As you have only one window you could use:
showStim.window = window # stimulus is now drawn to window
showStim.draw()
If you have more than one window you can pass the relevant window as a parameter (The brackets in your code are redundant).
# Note that this just changes **default** window for stimulus.
showStim.draw(win1)
showStim.draw(win2)

Why is the Windows prompt unresponsive after using a python curses window?

First of all, I'm a newby in python. Had to take a course of it in college and got hooked by its efficiency.
I have this sticky problem where the Windows 7 prompt becomes unresponsive after using a curses window. In Windows 10 it works well. Note that I'm using the Win7 terminal with its default settings. In my code I create a curses window to show 2 simultaneous progress bars, each for a file download. I implemented this by passing the curses window to a FileDownload class (one class instance for each download) that handles its progress bar inside this window. Oddly, in Windows 7 when the downloads are done and the control returns to the prompt, it becomes unresponsive to the keyboard. I worked around this by invoking curses.endwin() after using the window, but this causes the prompt to display all the way down the screen buffer, what hides the curses window.
Here is my code. Any ideas are greatly appreciated. Thanks!
# Skeleton version for simulations.
# Downloads 2 files simultaneously and shows a progress bar for each.
# Each file download is a FileDownload object that interacts with a
# common curses window passed as an argument.
import requests, math, threading, curses, datetime
class FileDownload:
def __init__(self, y_pos, window, url):
# Y position of the progress bar in the download queue window.
self.__bar_pos = int(y_pos)
self.__progress_window = window
self.__download_url = url
# Status of the file download object.
self.__status = "queued"
t = threading.Thread(target=self.__file_downloader)
t.start()
# Downloads selected file and handles its progress bar.
def __file_downloader(self):
file = requests.get(self.__download_url, stream=True)
self.__status = "downloading"
self.__progress_window.addstr(self.__bar_pos + 1, 1, "0%" + " " * 60 + "100%")
size = int(file.headers.get('content-length'))
win_prompt = "Downloading " + format(size, ",d") + " Bytes:"
self.__progress_window.addstr(self.__bar_pos, 1, win_prompt)
file_name = str(datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%d"))
dump = open(file_name, "wb")
# Progress bar length.
bar_space = 58
# Same as an index.
current_iteration = 0
# Beginning position of the progress bar.
progress_position = 4
# How many iterations will be needed (in chunks of 1 MB).
iterations = math.ceil(size / 1024 ** 2)
# Downloads the file in 1MB chunks.
for block in file.iter_content(1024 ** 2):
dump.write(block)
# Progress bar controller.
current_iteration += 1
step = math.floor(bar_space / iterations)
if current_iteration > 1:
progress_position += step
if current_iteration == iterations:
step = bar_space - step * (current_iteration - 1)
# Updates the progress bar.
self.__progress_window.addstr(self.__bar_pos + 1, progress_position,
"#" * step)
dump.close()
self.__status = "downloaded"
# Returns the current status of the file download ("queued", "downloading" or
# "downloaded").
def get_status(self):
return self.__status
# Instantiates each file download.
def files_downloader():
# Creates curses window.
curses.initscr()
win = curses.newwin(8, 70)
win.border(0)
win.immedok(True)
# Download URLs.
urls = ["http://ipv4.download.thinkbroadband.com/10MB.zip",
"http://ipv4.download.thinkbroadband.com/5MB.zip"]
downloads_dct = {}
for n in range(len(urls)):
# Progress bar position in the window for the file.
y_pos = n * 4 + 1
downloads_dct[n + 1] = FileDownload(y_pos, win, urls[n])
# Waits for all files to be downloaded before passing control of the terminal
# to the user.
all_downloaded = False
while not all_downloaded:
all_downloaded = True
for key, file_download in downloads_dct.items():
if file_download.get_status() != "downloaded":
all_downloaded = False
# Prevents the prompt from returning inside the curses window.
win.addstr(7, 1, "-")
# This solves the unresponsive prompt issue but hides the curses window if the screen buffer
# is higher than the window size.
# curses.endwin()
while input("\nEnter to continue: ") == "":
files_downloader()
Perhaps you're using cygwin (and ncurses): ncurses (like any other curses implementation) changes the terminal I/O mode when it is running. The changes that you probably are seeing is that
input characters are not echoed
you have to type controlJ to end an input line, rather than just Enter
output is not flushed automatically at the end of each line
It makes those changes to allow it to read single characters and also to use the terminal more efficiently.
To change back to the terminal's normal I/O mode, you would use the endwin function. The reset_shell_mode function also would be useful.
Further reading:
endwin (ncurses manual)
reset_shell_mode (ncurses manual)

Categories

Resources